1、切入指定方法
切入方法
@RestController
@RequestMapping("/")
public class AopDemo {
@GetMapping("/test")
public void test(){
System.err.println("执行内容。。。。。。");
}
}
@Aspect配置类
@Component
@Aspect
public class AopConfig {
@Before(value = "execution(* com.king.demo.web.AopDemo.test(..))")
public void before() {
System.out.println("before.....");
}
@After(value = "execution(* com.king.demo.web.AopDemo.test(..))")
public void after() {
System.out.println("after.....");
}
@AfterReturning(value = "execution(* com.king.demo.web.AopDemo.test(..))")
public void afterReturning() {
System.out.println("afterReturning.....");
}
@AfterThrowing(value = "execution(* com.king.demo.web.AopDemo.test(..))")
public void afterThrowing() {
System.out.println("afterThrowing.....");
}
@Around(value = "execution(* com.king.demo.web.AopDemo.test(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("around.....环绕之前....");
proceedingJoinPoint.proceed();
System.out.println("around.....环绕之后.....");
}
}
执行结果
around.....环绕之前....
before.....
执行内容。。。。。。
afterReturning.....
after.....
around.....环绕之后.....
2、切入指定方法-自定义注解
切入方法
@RestController
@RequestMapping("/")
public class AopDemo {
@GetMapping("/test")
@King(value = "king")
public void test(){
System.err.println("执行内容。。。。。。");
}
}
自定义注解类
@Documented
@Target(value={ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface King {
String value() default "";
}
具体切入逻辑
@Component
@Aspect
public class KingAno {
@Pointcut("@annotation(com.king.demo.config.King)")
private void pointcut(){
}
@Around(value = "pointcut() && @annotation(king)")
public void around(ProceedingJoinPoint proceedingJoinPoint, King king) throws Throwable {
System.err.println("注解value字段值:"+king.value());
proceedingJoinPoint.proceed();
}
}
执行结果
注解value字段值:king
执行内容。。。。。。
3、切入指定方法-自定义注解-指定参数
切入方法
@RestController
@RequestMapping("/")
public class AopDemo {
@GetMapping("/test")
@King(value = "king")
public void test(String str){
System.err.println("执行内容。。。。。。");
}
}
具体切入逻辑代码
@Component
@Aspect
public class KingAno {
@Pointcut("@annotation(com.king.demo.config.King) && args(java.lang.String)")
private void pointcut(){
}
@Around(value = "pointcut() && @annotation(king)")
public void around(ProceedingJoinPoint proceedingJoinPoint, King king) throws Throwable {
System.err.println("注解value字段值:"+king.value());
proceedingJoinPoint.proceed();
}
}