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..*)
-
創(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"); } }
-
創(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..*))"); } }
- 創(chuàng)建package命名為
-
創(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+)
-
創(chuàng)建接口及其實(shí)現(xiàn)類
創(chuàng)建package命名為
com.learn.service(根據(jù)實(shí)際情況修改)-
創(chuàng)建接口
IHelloServicepublic 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"); } }
-
創(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+)"); } }
- 配置AOP,向
-
創(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 *)
-
創(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 { }
- 創(chuàng)建package命名為
-
創(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"); } }
-
-
創(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 *)"); } }
- 配置AOP,向
-
創(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