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..*)
-
创建组件
-
创建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"); } }
-
-
创建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..*))"); } }
- 创建package命名为
-
创建测试用例
@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+)
-
创建接口及其实现类
-
创建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"); } }
-
-
创建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+)"); } }
- 配置AOP,向
-
创建测试用例
@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 *)
-
创建注解
- 创建package命名为
com.learn.annotation
(根据实际情况修改) - 创建注解
Secure
,内容如下@Documented @Retention(RetentionPolicy.RUNTIME) @Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE}) public @interface Secure { }
- 创建package命名为
-
创建接口及其实现类
-
在
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"); } }
-
-
创建AOP
- 配置AOP,向
ExecutionAOP
加入:@Aspect @Component public class ExecutionAop { @Before("within(@com.learn..Secure *)") public void execute3(){ System.out.println("within(@com.learn..Secure *)"); } }
- 配置AOP,向
-
创建测试用例
@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
网友评论