美文网首页
02-02 AOP学习之within

02-02 AOP学习之within

作者: 幽暗金 | 来源:发表于2018-01-11 14:07 被阅读0次

    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
      

    目录
    源码链接

    相关文章

      网友评论

          本文标题:02-02 AOP学习之within

          本文链接:https://www.haomeiwen.com/subject/qdgznxtx.html