02-06 AOP學(xué)習(xí)之@args,@annotation,bean

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示例

  1. 創(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 {
      }
      
  2. 創(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";
          }
      }
      
  3. 創(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");
          }
      
      }
      
  4. 創(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)");
          }
      }
      
  5. 創(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示例

  1. 添加方法

    • 在接口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");
          }
      }
      
  2. 新增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)");
          }
      
      }
      
  3. 創(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示例

  1. 創(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");
          }
      
      }
      
  2. 新增AOP監(jiān)聽

    • ExecutionAOP中新增監(jiān)聽当窗,內(nèi)容如下
      @Aspect
      @Component
      public class ExecutionAop {
          @Before("bean(*Service)")
          public void execute3(){
              System.out.println("bean(*Service)");
          }
      }
      
  3. 創(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匹配到了够坐。

目錄
源碼鏈接

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市崖面,隨后出現(xiàn)的幾起案子元咙,更是在濱河造成了極大的恐慌,老刑警劉巖巫员,帶你破解...
    沈念sama閱讀 216,496評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件庶香,死亡現(xiàn)場離奇詭異,居然都是意外死亡简识,警方通過查閱死者的電腦和手機(jī)赶掖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來七扰,“玉大人奢赂,你說我怎么就攤上這事【弊撸” “怎么了膳灶?”我有些...
    開封第一講書人閱讀 162,632評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長立由。 經(jīng)常有香客問我轧钓,道長序厉,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,180評(píng)論 1 292
  • 正文 為了忘掉前任聋迎,我火速辦了婚禮脂矫,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘霉晕。我一直安慰自己庭再,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,198評(píng)論 6 388
  • 文/花漫 我一把揭開白布牺堰。 她就那樣靜靜地躺著拄轻,像睡著了一般。 火紅的嫁衣襯著肌膚如雪伟葫。 梳的紋絲不亂的頭發(fā)上恨搓,一...
    開封第一講書人閱讀 51,165評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音筏养,去河邊找鬼斧抱。 笑死,一個(gè)胖子當(dāng)著我的面吹牛渐溶,可吹牛的內(nèi)容都是我干的辉浦。 我是一名探鬼主播,決...
    沈念sama閱讀 40,052評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼茎辐,長吁一口氣:“原來是場噩夢啊……” “哼宪郊!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起拖陆,我...
    開封第一講書人閱讀 38,910評(píng)論 0 274
  • 序言:老撾萬榮一對情侶失蹤弛槐,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后依啰,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體乎串,經(jīng)...
    沈念sama閱讀 45,324評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,542評(píng)論 2 332
  • 正文 我和宋清朗相戀三年孔飒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了灌闺。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,711評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡坏瞄,死狀恐怖桂对,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情鸠匀,我是刑警寧澤蕉斜,帶...
    沈念sama閱讀 35,424評(píng)論 5 343
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響宅此,放射性物質(zhì)發(fā)生泄漏机错。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,017評(píng)論 3 326
  • 文/蒙蒙 一父腕、第九天 我趴在偏房一處隱蔽的房頂上張望弱匪。 院中可真熱鬧,春花似錦璧亮、人聲如沸萧诫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽帘饶。三九已至,卻和暖如春群扶,著一層夾襖步出監(jiān)牢的瞬間及刻,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評(píng)論 1 269
  • 我被黑心中介騙來泰國打工竞阐, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留缴饭,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,722評(píng)論 2 368
  • 正文 我出身青樓骆莹,卻偏偏與公主長得像茴扁,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子汪疮,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,611評(píng)論 2 353