02-02 AOP学习之within

Spring AOP within使用示例

within:使用“within(类型表达式)”匹配指定类型内的任何方法执行;

模式 描述
within(com.learn.all..*) com.learn.all包及子包下的任何方法执行
within(com.learn.service..IHelloService+) com.learn.service包或所有子包下IHelloService类型及子类型的任何方法
within(@com.learn..Secure *) 持有com.learn..Secure注解的类的任何方法,注解必须是在目标对象上声明,对在接口上声明的不起作用

within(com.learn.all..*)

  1. 创建组件

    • 创建package命名为com.learn.all(根据实际情况修改)

    • 创建组件Any,内容如下

      @Component
      public class Any {
          public void say() {
              System.out.println("Any");
          }
      }
      
    • 创建package命名为com.learn.all.child(根据实际情况修改)

    • 创建组件ChildAny,内容如下

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

    • 创建package命名为com.learn.aop(根据实际情况修改)
    • 配置AOP,新建ExecutionAOP,内容如下
      @Aspect
      @Component
      public class ExecutionAop {
      
          @Before("within(com.learn.all..*))")
          public void execute1(){
              System.out.println("within(com.learn.all..*))");
          }
      }
      
  3. 创建测试用例

    @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();
        }
    }
    

    运行可得到结果

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

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

  1. 创建接口及其实现类

    • 创建package命名为com.learn.service(根据实际情况修改)

    • 创建接口IHelloService

      public interface IHelloService {
          void sayHello();
      }
      
      
    • 创建接口IExtendHelloService继承IHelloService

      public interface IExtendHelloService extends IHelloService {
      }
      
    • 创建package命名为com.learn.service.impl(根据实际情况修改)

    • 创建实现类HelloServiceImpl

      @Service("HelloService")
      @Primary
      public class HelloServiceImpl implements IHelloService {
      
          public void sayHello() {
              System.out.println("hello 1");
          }
      
      }
      
    • 创建实现类HelloServiceImpl2

      @Service("HelloService2")
      public class HelloServiceImpl2 implements IHelloService {
      
          public void sayHello() {
              System.out.println("hello 2");
          }
      
      }
      
    • 创建实现类ExtendHelloServiceImpl

      @Service
      public class ExtendHelloServiceImpl implements IExtendHelloService {
          @Override
          public void sayHello() {
              System.out.println("hello IExtendHelloService");
          }
      }
      
  2. 创建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. 创建测试用例

    @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();
        }
    }
    

    运行可得到结果

    --------------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. 创建注解

    • 创建package命名为com.learn.annotation(根据实际情况修改)
    • 创建注解Secure,内容如下
      @Documented
      @Retention(RetentionPolicy.RUNTIME)
      @Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
      public @interface Secure {
      }
      
  2. 创建接口及其实现类

    • com.learn.service包中创建接口ISecureService,内容如下

      public interface ISecureService {
          void sayHello();
      }
      
    • com.learn.service.impl包中创建其实现类SecureServiceImpl,内容如下

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

    • 配置AOP,向ExecutionAOP加入:
      @Aspect
      @Component
      public class ExecutionAop {
          @Before("within(@com.learn..Secure *)")
          public void execute3(){
              System.out.println("within(@com.learn..Secure *)");
          }
      }
      
  4. 创建测试用例

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

    运行可得到结果

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

目录
源码链接

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。