spring aop 注解指示器
注解指示器:@within、@target()、@annotation、@args
**********************
示例
****************
annotation 层
A1
public @interface A1 {
}
A3
public @interface A3 {
}
A4
public @interface A4 {
}
****************
pojo 层
A:@A4标注在类上
@A4
public class A {
}
****************
service 层
HelloService
@A1
@Service
public class HelloService {
public void test(){
System.out.println("hello test ");
}
@A3
public void test2(){
System.out.println("hello test 2");
}
public void test3(A a){
System.out.println("hello test 3");
}
}
****************
aspect 层
HelloAspect
@Aspect
@Component
public class HelloAspect {
@Pointcut("@within(com.example.demo.annotation.A1)")
public void fun(){
}
@Pointcut("@annotation(com.example.demo.annotation.A3)")
public void fun3(){
}
@Pointcut("@args(com.example.demo.annotation.A4)&&within(com.example.demo.serviceImpl.HelloService)")
public void fun4(){
}
@Before("fun()")
public void before(){
System.out.println("前置通知 A1");
}
@Before("fun3()")
public void after3(){
System.out.println("后置通知 A3");
}
@After("fun4()")
public void after4(){
System.out.println("后置通知 A4");
}
}
****************
controller 层
HelloController
@RestController
public class HelloController {
@Autowired
private HelloService helloService;
@RequestMapping("/test")
public String test(){
helloService.test();
return "success 1";
}
@RequestMapping("/test2")
public String test2(){
helloService.test2();
return "success 2";
}
@RequestMapping("/test3")
public String test3(){
helloService.test3(new A());
return "success 3";
}
}