spring aop 示例(注解指示器)


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";
    }
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值