AOP能够用于日志的设计,这样话就少不了要获取上下文的信息,博主在设计日志模块时考虑了一下此法,整理了一下如何用AOP来拦截你自定义的注解。spring
首先先自定义一个注解springboot
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Axin { /** * 所属模块 * @return */ String module() default "日志模块"; /** * 动做描述 * @return */ String desc() default "无动做"; }
/** * @author Axin */ @Aspect @Component public class AxinAspect { /** * 这里定义了一个总的匹配规则,之后拦截的时候直接拦截log()方法便可,无须去重复写execution表达式 */ @Pointcut("@annotation(Axin)") public void log() { } @Before("log()&&@annotation(axin)") public void doBefore(JoinPoint joinPoint,Axin axin) { System.out.println("******拦截前的逻辑******"); System.out.println("目标方法名为:" + joinPoint.getSignature().getName()); System.out.println("目标方法所属类的简单类名:" + joinPoint.getSignature().getDeclaringType().getSimpleName()); System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName()); System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers())); //获取传入目标方法的参数 Object[] args = joinPoint.getArgs(); for (int i = 0; i < args.length; i++) { System.out.println("第" + (i + 1) + "个参数为:" + args[i]); } System.out.println("被代理的对象:" + joinPoint.getTarget()); System.out.println("代理对象本身:" + joinPoint.getThis()); System.out.println("拦截的注解的参数:"); System.out.println(axin.module()); System.out.println(axin.desc()); } @Around("log()&&@annotation(axin)") public Object doAround(ProceedingJoinPoint proceedingJoinPoint,Axin axin) throws Throwable { System.out.println("环绕通知:"); System.out.println(axin.module()); System.out.println(axin.desc()); Object result = null; result = proceedingJoinPoint.proceed(); return result; } @After("log()") public void doAfter() { System.out.println("******拦截后的逻辑******"); } }
匹配规则:app
//匹配AOP对象的目标对象为指定类型的方法,即DemoDao的aop的代理对象 @Pointcut("this(com.hhu.DemaoDao)") public void thisDemo() { ... }
通知类别:ide
JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就能够获取到封装了该方法信息的JoinPoint对象. 注意:这用于非环绕通知函数
方法名 | 功能 |
---|---|
Signature getSignature(); | 获取封装了署名信息的对象,在该对象中能够获取到目标方法名,所属类的Class等信息 |
Object[] getArgs(); | 获取传入目标方法的参数对象 |
Object getTarget(); | 获取被代理的对象 |
Object getThis(); | 获取代理对象 |
方法使用模板:测试
public void doBefore(JoinPoint joinPoint) { System.out.println("******拦截前的逻辑******"); System.out.println("目标方法名为:" + joinPoint.getSignature().getName()); System.out.println("目标方法所属类的简单类名:" + joinPoint.getSignature().getDeclaringType().getSimpleName()); System.out.println("目标方法所属类的类名:" + joinPoint.getSignature().getDeclaringTypeName()); System.out.println("目标方法声明类型:" + Modifier.toString(joinPoint.getSignature().getModifiers())); //获取传入目标方法的参数 Object[] args = joinPoint.getArgs(); for (int i = 0; i < args.length; i++) { System.out.println("第" + (i + 1) + "个参数为:" + args[i]); } System.out.println("被代理的对象:" + joinPoint.getTarget()); System.out.println("代理对象本身:" + joinPoint.getThis()); }
ProceedingJoinPoint对象是JoinPoint的子接口,该对象只用在@Around的切面方法中this
方法名 | 功能 |
---|---|
Object proceed() throws Throwable | 执行目标方法 |
Object proceed(Object[] var1) throws Throwable | 传入的新的参数去执行目标方法 |
//Service接口 public interface AxinService { String axinRun(String arg1, User user); } //实现类 /** * @author Axin */ @Component public class AxinServiceImpl implements AxinService { @Axin(module = "print",desc = "打印") @Override public String axinRun(String arg1, User user) { String res = arg1 + user.getName() + user.getAge(); return res; } public String axinRun(String arg1, Person person) { String res = arg1 + person.getName() + person.getAge(); return res; } } //控制类 /** * @author Axin */ @RestController public class HelloController { @Autowired AxinService axinService; @RequestMapping("/hello") public String hello() { User user = new User(); user.setAge(10); user.setName("张三"); String res = axinService.axinRun("Test:", user); return "Hello Spring Boot!<br>"+res; } }
环绕通知: print 打印 ******拦截前的逻辑****** 目标方法名为:axinRun 目标方法所属类的简单类名:AxinService 目标方法所属类的类名:com.axin.springboot.service.AxinService 目标方法声明类型:public abstract 第1个参数为:Test: 第2个参数为:User(id=null, name=张三, age=10, date=null) 被代理的对象:com.axin.springboot.service.AxinServiceImpl@ac2ddcc 代理对象本身:com.axin.springboot.service.AxinServiceImpl@ac2ddcc 拦截的注解的参数: print 打印 ******拦截后的逻辑******
经过上述的代码演示,咱们能够自定义一个注解,而后配置切面来拦截有注解的方法,同时也能够得到方法传入的参数来完成你的业务需求。设计