02-06 AOP學(xué)習(xí)之@args,@annotation,bean

Spring AOP @args,@annotation和bean使用示例

@args:使用“@args(注解列表)”匹配當(dāng)前執(zhí)行的方法傳入的參數(shù)持有指定注解的執(zhí)行

注解類型也必須是全限定類型名

模式 描述
@args(com.learn.annotation.Secure) 任何一個(gè)只接受一個(gè)參數(shù)的方法,且方法運(yùn)行時(shí)傳入的參數(shù)持有注解com.learn.annotation.Secure;動(dòng)態(tài)切入點(diǎn),類似于arg指示符

@annotation:使用“@annotation(注解類型)”匹配當(dāng)前執(zhí)行方法持有指定注解的方法

注解類型也必須是全限定類型名

模式 描述
@annotation(com.learn.annotation.Secure) 當(dāng)前執(zhí)行方法上持有注解com.learn.annotation.Secure將被匹配

bean:使用“bean(Bean id或名字通配符)”匹配特定名稱的Bean對(duì)象的執(zhí)行方法

Spring ASP擴(kuò)展的,在AspectJ中無相應(yīng)概念

模式 描述
bean(*Service) 匹配所有以Service命名(id或name)結(jié)尾的Bean

示例

@args示例

  1. 創(chuàng)建注解

    • 創(chuàng)建package命名為com.learn.annotation(根據(jù)實(shí)際情況修改)
    • 創(chuàng)建注解Secure,內(nèi)容如下
      @Documented
      @Retention(RetentionPolicy.RUNTIME)
      @Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
      public @interface Secure {
      }
      
  2. 創(chuàng)建實(shí)體類

    • 創(chuàng)建package命名為com.learn.model(根據(jù)實(shí)際情況修改)

    • 創(chuàng)建類User,內(nèi)容如下

      public class User {
          public String getName() {
              return "user";
          }
      }
      
    • 創(chuàng)建Member繼承User,并加上注解@Secure,內(nèi)容如下

      @Secure
      public class Member extends User{
          @Override
          public String getName() {
              return "member";
          }
      }
      
    • 創(chuàng)建Leader繼承Member,內(nèi)容如下

      public class Leader extends Member{
          @Override
          public String getName() {
              return "leader";
          }
      }
      
  3. 創(chuàng)建服務(wù)

    • 創(chuàng)建package命名為com.learn.service(根據(jù)實(shí)際情況修改)

    • 創(chuàng)建接口IHelloService,內(nèi)容如下

      public interface IHelloService {
      
          void sayHello(User user);
      
          void secureSay();
      }
      
    • 創(chuàng)建package命名為com.learn.service.impl(根據(jù)實(shí)際情況修改)

    • 創(chuàng)建接口IHelloService的實(shí)現(xiàn)類HelloServiceImpl,內(nèi)容如下

      @Service
      public class HelloServiceImpl implements IHelloService {
      
          @Override
          public void sayHello(User user) {
              System.out.println(user.getName() + ":hello");
          }
      
      }
      
  4. 創(chuàng)建AOP

    • 創(chuàng)建package命名為com.learn.aop(根據(jù)實(shí)際情況修改)
    • 配置AOP,新建ExecutionAOP,內(nèi)容如下
      @Aspect
      @Component
      public class ExecutionAop {
      
          @Before("execution(* com.learn..*(..)) && @args(com.learn.annotation.Secure)")
          public void execute1(){
              System.out.println("@args(com.learn.annotation.Secure)");
          }
      }
      
  5. 創(chuàng)建測(cè)試用例

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ApplicationTests {
    
        @Resource
        private IHelloService helloService;
    
        @Test
        public void test1() {
            System.out.println("------------------User-----------------");
            helloService.sayHello(new User());
            System.out.println("------------------Member-----------------");
            helloService.sayHello(new Member());
            System.out.println("------------------Leader-----------------");
            helloService.sayHello(new Leader());
        }
        
    }
    

    運(yùn)行測(cè)試用例可得到結(jié)果

    ------------------User-----------------
    user:hello
    ------------------Member-----------------
    @args(com.learn.annotation.Secure)
    member:hello
    ------------------Leader-----------------
    leader:hello
    

    由此可知,@args會(huì)根據(jù)傳入的參數(shù)取動(dòng)態(tài)的匹配方法。

@annotation示例

  1. 添加方法

    • 在接口IHelloService中新增方法,內(nèi)容如下

      public interface IHelloService {
          void secureSay();
      }
      
    • 在其實(shí)現(xiàn)類中實(shí)現(xiàn)該方法

      @Service
      public class HelloServiceImpl implements IHelloService {
          @Secure
          @Override
          public void secureSay() {
              System.out.println("hello");
          }
      }
      
  2. 新增AOP監(jiān)聽

    • ExecutionAOP中新增監(jiān)聽,內(nèi)容如下
      @Aspect
      @Component
      public class ExecutionAop {
      
          @Before("@annotation(com.learn.annotation.Secure)")
          public void execute2(){
              System.out.println("@annotation(com.learn.annotation.Secure)");
          }
      
      }
      
  3. 創(chuàng)建測(cè)試用例

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ApplicationTests {
    
        @Resource
        private IHelloService helloService;
    
        @Test
        public void test2() {
            helloService.secureSay();
        }
    
    }
    

    運(yùn)行測(cè)試用例可得到結(jié)果

    @annotation(com.learn.annotation.Secure)
    hello
    

    可以看出加了注解的方法確實(shí)被AOP匹配到了。

@bean示例

  1. 創(chuàng)建服務(wù)

    • com.learn.service包中創(chuàng)建接口IExceptService

      public interface IExceptService {
          void sayHello();
      }
      
    • com.learn.service.impl包中創(chuàng)建其實(shí)現(xiàn)類ExceptService

      @Service
      public class ExceptService implements IExceptService {
      
          @Override
          public void sayHello() {
              System.out.println("hello");
          }
      
      }
      
  2. 新增AOP監(jiān)聽

    • ExecutionAOP中新增監(jiān)聽,內(nèi)容如下
      @Aspect
      @Component
      public class ExecutionAop {
          @Before("bean(*Service)")
          public void execute3(){
              System.out.println("bean(*Service)");
          }
      }
      
  3. 創(chuàng)建測(cè)試用例

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ApplicationTests {
    
        @Resource
        private IExceptService exceptService;
    
        @Test
        public void test3() {
            exceptService.sayHello();
        }
    }
    

    運(yùn)行測(cè)試用例可得到結(jié)果

    bean(*Service)
    hello
    

    可以看出命名以Service結(jié)尾的服務(wù)中的方法確實(shí)被AOP匹配到了。

目錄
源碼鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容