Spring AOP @args,@annotation和bean使用示例
@args:使用“@args(注解列表)”匹配當(dāng)前執(zhí)行的方法傳入的參數(shù)持有指定注解的執(zhí)行
注解類型也必須是全限定類型名
模式 | 描述 |
---|---|
@args(com.learn.annotation.Secure) |
任何一個(gè)只接受一個(gè)參數(shù)的方法,且方法運(yùn)行時(shí)傳入的參數(shù)持有注解com.learn.annotation.Secure 婚瓜;動(dòng)態(tài)切入點(diǎn),類似于arg指示符 |
@annotation:使用“@annotation(注解類型)”匹配當(dāng)前執(zhí)行方法持有指定注解的方法
注解類型也必須是全限定類型名
模式 | 描述 |
---|---|
@annotation(com.learn.annotation.Secure) |
當(dāng)前執(zhí)行方法上持有注解com.learn.annotation.Secure 將被匹配 |
bean:使用“bean(Bean id或名字通配符)”匹配特定名稱的Bean對象的執(zhí)行方法
Spring ASP擴(kuò)展的精耐,在AspectJ中無相應(yīng)概念
模式 | 描述 |
---|---|
bean(*Service) |
匹配所有以Service命名(id或name)結(jié)尾的Bean |
示例
@args示例
-
創(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í)體類
創(chuàng)建package命名為
com.learn.model
(根據(jù)實(shí)際情況修改)-
創(chuàng)建類
User
,內(nèi)容如下public class User { public String getName() { return "user"; } }
-
創(chuàng)建
Member
繼承User
,并加上注解@Secure
,內(nèi)容如下@Secure public class Member extends User{ @Override public String getName() { return "member"; } }
-
創(chuàng)建
Leader
繼承Member
慢叨,內(nèi)容如下public class Leader extends Member{ @Override public String getName() { return "leader"; } }
-
創(chuàng)建服務(wù)
創(chuàng)建package命名為
com.learn.service
(根據(jù)實(shí)際情況修改)-
創(chuàng)建接口
IHelloService
,內(nèi)容如下public interface IHelloService { void sayHello(User user); void secureSay(); }
創(chuàng)建package命名為
com.learn.service.impl
(根據(jù)實(shí)際情況修改)-
創(chuàng)建接口
IHelloService
的實(shí)現(xiàn)類HelloServiceImpl
群凶,內(nèi)容如下@Service public class HelloServiceImpl implements IHelloService { @Override public void sayHello(User user) { System.out.println(user.getName() + ":hello"); } }
-
創(chuàng)建AOP
- 創(chuàng)建package命名為
com.learn.aop
(根據(jù)實(shí)際情況修改) - 配置AOP插爹,新建
ExecutionAOP
,內(nèi)容如下@Aspect @Component public class ExecutionAop { @Before("execution(* com.learn..*(..)) && @args(com.learn.annotation.Secure)") public void execute1(){ System.out.println("@args(com.learn.annotation.Secure)"); } }
- 創(chuàng)建package命名為
-
創(chuàng)建測試用例
@RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTests { @Resource private IHelloService helloService; @Test public void test1() { System.out.println("------------------User-----------------"); helloService.sayHello(new User()); System.out.println("------------------Member-----------------"); helloService.sayHello(new Member()); System.out.println("------------------Leader-----------------"); helloService.sayHello(new Leader()); } }
運(yùn)行測試用例可得到結(jié)果
------------------User----------------- user:hello ------------------Member----------------- @args(com.learn.annotation.Secure) member:hello ------------------Leader----------------- leader:hello
由此可知请梢,@args會(huì)根據(jù)傳入的參數(shù)取動(dòng)態(tài)的匹配方法。
@annotation示例
-
添加方法
-
在接口
IHelloService
中新增方法力穗,內(nèi)容如下public interface IHelloService { void secureSay(); }
-
在其實(shí)現(xiàn)類中實(shí)現(xiàn)該方法
@Service public class HelloServiceImpl implements IHelloService { @Secure @Override public void secureSay() { System.out.println("hello"); } }
-
-
新增AOP監(jiān)聽
- 在
ExecutionAOP
中新增監(jiān)聽毅弧,內(nèi)容如下@Aspect @Component public class ExecutionAop { @Before("@annotation(com.learn.annotation.Secure)") public void execute2(){ System.out.println("@annotation(com.learn.annotation.Secure)"); } }
- 在
-
創(chuàng)建測試用例
@RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTests { @Resource private IHelloService helloService; @Test public void test2() { helloService.secureSay(); } }
運(yùn)行測試用例可得到結(jié)果
@annotation(com.learn.annotation.Secure) hello
可以看出加了注解的方法確實(shí)被AOP匹配到了。
@bean示例
-
創(chuàng)建服務(wù)
-
在
com.learn.service
包中創(chuàng)建接口IExceptService
public interface IExceptService { void sayHello(); }
-
在
com.learn.service.impl
包中創(chuàng)建其實(shí)現(xiàn)類ExceptService
@Service public class ExceptService implements IExceptService { @Override public void sayHello() { System.out.println("hello"); } }
-
-
新增AOP監(jiān)聽
- 在
ExecutionAOP
中新增監(jiān)聽当窗,內(nèi)容如下@Aspect @Component public class ExecutionAop { @Before("bean(*Service)") public void execute3(){ System.out.println("bean(*Service)"); } }
- 在
-
創(chuàng)建測試用例
@RunWith(SpringRunner.class) @SpringBootTest public class ApplicationTests { @Resource private IExceptService exceptService; @Test public void test3() { exceptService.sayHello(); } }
運(yùn)行測試用例可得到結(jié)果
bean(*Service) hello
可以看出命名以Service結(jié)尾的服務(wù)中的方法確實(shí)被AOP匹配到了够坐。