02-02 AOP學(xué)習(xí)之within

Spring AOP within使用示例

within:使用“within(類型表達(dá)式)”匹配指定類型內(nèi)的任何方法執(zhí)行;

模式 描述
within(com.learn.all..*) com.learn.all包及子包下的任何方法執(zhí)行
within(com.learn.service..IHelloService+) com.learn.service包或所有子包下IHelloService類型及子類型的任何方法
within(@com.learn..Secure *) 持有com.learn..Secure注解的類的任何方法,注解必須是在目標(biāo)對(duì)象上聲明,對(duì)在接口上聲明的不起作用

within(com.learn.all..*)

  1. 創(chuàng)建組件

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

    • 創(chuàng)建組件Any,內(nèi)容如下

      @Component
      public class Any {
          public void say() {
              System.out.println("Any");
          }
      }
      
    • 創(chuàng)建package命名為com.learn.all.child(根據(jù)實(shí)際情況修改)

    • 創(chuàng)建組件ChildAny,內(nèi)容如下

      @Component
      public class ChildAny {
          public void say() {
              System.out.println("ChildAny");
          }
      }
      
  2. 創(chuàng)建AOP

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

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ApplicationTests {
    
        @Resource
        private Any any;
        @Resource
        private ChildAny childAny;
    
        @Test
        public void test1() {
            System.out.println("--------------com.learn.all-------------");
            any.say();
            System.out.println("--------------com.learn.all-------------");
            childAny.say();
        }
    }
    

    運(yùn)行可得到結(jié)果

    --------------com.learn.all-------------
    within(com.learn.all..*))
    Any
    --------------com.learn.all-------------
    within(com.learn.all..*))
    ChildAny
    

within(com.learn.service..IHelloService+)

  1. 創(chuàng)建接口及其實(shí)現(xiàn)類

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

    • 創(chuàng)建接口IHelloService

      public interface IHelloService {
          void sayHello();
      }
      
      
    • 創(chuàng)建接口IExtendHelloService繼承IHelloService

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

    • 創(chuàng)建實(shí)現(xiàn)類HelloServiceImpl

      @Service("HelloService")
      @Primary
      public class HelloServiceImpl implements IHelloService {
      
          public void sayHello() {
              System.out.println("hello 1");
          }
      
      }
      
    • 創(chuàng)建實(shí)現(xiàn)類HelloServiceImpl2

      @Service("HelloService2")
      public class HelloServiceImpl2 implements IHelloService {
      
          public void sayHello() {
              System.out.println("hello 2");
          }
      
      }
      
    • 創(chuàng)建實(shí)現(xiàn)類ExtendHelloServiceImpl

      @Service
      public class ExtendHelloServiceImpl implements IExtendHelloService {
          @Override
          public void sayHello() {
              System.out.println("hello IExtendHelloService");
          }
      }
      
  2. 創(chuàng)建AOP

    • 配置AOP,向ExecutionAOP加入:
      @Aspect
      @Component
      public class ExecutionAop {
          @Before("within(com.learn.service..IHelloService+)")
          public void execute2(){
              System.out.println("within(com.learn.service..IHelloService+)");
          }
      }
      
  3. 創(chuàng)建測(cè)試用例

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ApplicationTests {
    
        @Resource(name = "HelloService")
        private IHelloService helloService;
        @Resource(name = "HelloService2")
        private IHelloService helloService2;
        @Resource
        private IExtendHelloService extendHelloService;
    
        @Test
        public void test2() {
            System.out.println("--------------helloService-------------");
            helloService.sayHello();
            System.out.println("--------------helloService2-------------");
            helloService2.sayHello();
            System.out.println("--------------extendHelloService-------------");
            extendHelloService.sayHello();
        }
    }
    

    運(yùn)行可得到結(jié)果

    --------------helloService-------------
    within(com.learn.service..IHelloService+)
    hello 1
    --------------helloService2-------------
    within(com.learn.service..IHelloService+)
    hello 2
    --------------extendHelloService-------------
    within(com.learn.service..IHelloService+)
    hello IExtendHelloService
    

within(@com.learn..Secure *)

  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í)現(xiàn)類

    • com.learn.service包中創(chuàng)建接口ISecureService,內(nèi)容如下

      public interface ISecureService {
          void sayHello();
      }
      
    • com.learn.service.impl包中創(chuàng)建其實(shí)現(xiàn)類SecureServiceImpl,內(nèi)容如下

      @Service
      @Secure
      public class SecureServiceImpl implements ISecureService{
          @Override
          public void sayHello(){
              System.out.println("hello secure");
          }
      }
      
  3. 創(chuàng)建AOP

    • 配置AOP,向ExecutionAOP加入:
      @Aspect
      @Component
      public class ExecutionAop {
          @Before("within(@com.learn..Secure *)")
          public void execute3(){
              System.out.println("within(@com.learn..Secure *)");
          }
      }
      
  4. 創(chuàng)建測(cè)試用例

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class ApplicationTests {
    
        private ISecureService secureService;
    
        @Test
        public void test3() {
            System.out.println("--------------secureService-------------");
            secureService.sayHello();
        }
    }
    

    運(yùn)行可得到結(jié)果

    --------------secureService-------------
    within(@com.learn..Secure *)
    hello secure
    

目錄
源碼鏈接

最后編輯于
?著作權(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)容