spring aop注解表达式


spring aop注解表达式

        

             

                                   

注解表达式

                     

@within:当前调用对象标注了指定注解时,切点匹配(需要指定具体的注解类,不能使用通配符匹配包下的多个注解类)

@Pointcut("@within(com.example.demo.annotation.CustomAnnotation)")
* 当前调用对象的目标对象上标注有CustomAnnotation注解,当前对象的所有方法都会匹配切点
* 当前调用对象的父类标注CustomAnnotation注解,当前对象的目标类没有重写的方法均会匹配切点

       

@target:当前调用对象的目标对象标注了指定注解,切点匹配

@Pointcut("within(com.example.demo.service..*)" + 
        "&& @target(com.example.demo.annotation.CustomAnnotation4)")
* 当前调用对象的目标对象在com.example.demo.service包下,
* 并且被CustomAnnotation注解修饰时,切点匹配
说明:@target不能单独使用,会报错

          

@annotation:调用的方法上标注有指定注解时,切点匹配

@Pointcut("@annotation(com.example.demo.annotation.CustomAnnotation)")
* 调用的方法标注有注解CustomAnnotation时,切点匹配

             

@args:调用的方法参数被指定注解修饰时,切点匹配(需与其他切点表达式配合使用,不可单独使用)

@Pointcut("within(com.example.demo.service..*) "+
          "&& @args(com.example.demo.annotation.CustomAnnotation)")
* 包com.example.demo.service及子包下所有的类,
* 且方法只有一个参数,参数对应的类标注注解CustomAnnotation时,切点匹配

       

                   

                                   

使用示例

       

                         

          

************

pojo 层

     

Person

@Data
@CustomAnnotation
public class Person {

    private String name;
    private Integer age;
}

        

User

@Data
@CustomAnnotation2
public class User {

    private String name;
    private Integer age;
}

        

************

annotation 层

   

CustomAnnotation

@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation {
}

      

CustomAnnotation2

@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation2 {
}

       

CustomAnnotation3

@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation3 {
}

      

CustomAnnotation4

@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnotation4 {
}

       

************

service 层

  

HelloService

public interface HelloService {

    String hello();
    String hello(Person person);
    String hello(User user);
}

       

HelloService2

public interface HelloService2 {

    String hello();
}

       

************

service.impl 层

    

HelloServiceImpl

@Service
@CustomAnnotation3
public class HelloServiceImpl implements HelloService {

    @Override
    public String hello() {
        return "hello";
    }

    @Override
    public String hello(Person person) {
        System.out.println("HelloServiceImpl hello(Person):" + person);

        return "hello" + person;
    }

    @Override
    public String hello(User user) {
        System.out.println("HelloServiceImpl hello(User):" + user);

        return "hello" + user;
    }
}

       

HelloService2Impl

@Service
@CustomAnnotation4
public class HelloService2Impl implements HelloService2 {

    @Override
    public String hello() {
        return "hello2";
    }
}

      

************

aspect 层

  

CustomAspect

@Aspect
@Component
public class CustomAspect {

    @Pointcut("within(com.example.demo.service..*)" +
            "&& @args(com.example.demo.annotation.CustomAnnotation)")
    public void fun(){

    }

    @Pointcut("within(com.example.demo.service..*)" +
            "&& @args(com.example.demo.annotation.CustomAnnotation2)")
    public void fun2(){

    }

    @Pointcut("@within(com.example.demo.annotation.CustomAnnotation3)")
    public void fun3(){

    }

    @Pointcut("within(com.example.demo.service..*)" +
            "&& @target(com.example.demo.annotation.CustomAnnotation4)")
    public void fun4(){

    }

    @Before("fun()")
    public void before(JoinPoint joinPoint){
        System.out.print("before ==> ");
        process(joinPoint);
    }

    @Before("fun2()")
    public void before2(JoinPoint joinPoint){
        System.out.print("before2 ==> ");
        process(joinPoint);
    }

    @Before("fun3()")
    public void after3(JoinPoint joinPoint){
        System.out.print("after3 ==> ");
        process(joinPoint);
    }

    @After("fun4()")
    public void after4(JoinPoint joinPoint){
        System.out.print("after4 ==> ");
        process(joinPoint);
    }

    public void process(JoinPoint joinPoint){
        MethodSignature signature =(MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        System.out.println(method.getDeclaringClass().getName()+"."+method.getName()+":被调用");
    }
}

          

************

controller 层

   

HelloContoller

@RestController
public class HelloController {

    @Resource
    private HelloService helloService;

    @Resource
    private HelloService2 helloService2;

    @RequestMapping("/hello")
    public String hello(){
        return helloService.hello();
    }

    @RequestMapping("/hello2")
    public String hello2(){
        Person person = new Person();
        person.setName("瓜田李下");
        person.setAge(20);

        return helloService.hello(person);
    }

    @RequestMapping("/hello3")
    public String hello3(){
        User user = new User();
        user.setName("海贼王");
        user.setAge(20);

        return helloService.hello(user);
    }

    @RequestMapping("/hello4")
    public String hello4(){
        return helloService2.hello();
    }
}

     

             

                                   

使用测试

      

localhost:8080/hello,控制台输出:

after3 ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用

       

localhost:8080/hello2,控制台输出:

after3 ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用
before ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用
HelloServiceImpl hello(Person):Person(name=瓜田李下, age=20)

        

localhost:8080/hello3,控制台输出:

after3 ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用
before2 ==> com.example.demo.service.impl.HelloServiceImpl.hello:被调用
HelloServiceImpl hello(User):User(name=海贼王, age=20)

        

localhost:8080/hello4,控制台输出:

after4 ==> com.example.demo.service.impl.HelloService2Impl.hello:被调用

       

            

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值