Spring-AOP @AspectJ進(jìn)階之綁定類注解對象

      網(wǎng)友投稿 644 2025-04-02

      文章目錄

      概述

      實(shí)例

      概述

      @within()和@target()函數(shù)可以將目標(biāo)類的注解對象綁定到增強(qiáng)方法中。

      我們通過@within()演示注解綁定的操作

      實(shí)例

      代碼已托管到Github—> https://github.com/yangshangwei/springMaster

      注解(使用的是自定義注解,也可以使用框架提供的注解)

      package com.xgj.aop.spring.advisor.aspectJAdvance.bindTypeAnnoObj; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; //聲明注解的保留期限 @Retention(RetentionPolicy.RUNTIME) // 聲明可以使用該注解的目標(biāo)類型 @Target(ElementType.TYPE) // 可以被javadoc此類的工具文檔化 @Documented public @interface Monitor { // 定義注解 // 聲明注解成員 boolean value() default false; }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      業(yè)務(wù)類

      package com.xgj.aop.spring.advisor.aspectJAdvance.bindTypeAnnoObj; import org.springframework.stereotype.Component; /** * * * @ClassName: Bussiness * * @Description: bean使用@Component注解, * * 同時標(biāo)注了@@Monitor注解,所有Bussiness Bean匹配切點(diǎn), 其@Monitor注解對象將綁定到增強(qiáng)方法中 * * @author: Mr.Yang * * @date: 2017年9月12日 下午4:32:23 */ @Component @Monitor public class Bussiness { public void dealBussinessOne() { System.out.println("dealBussinessOne executed"); } public void dealBussinessTwo() { System.out.println("dealBussinessTwo executed"); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      Spring-AOP @AspectJ進(jìn)階之綁定類注解對象

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      切面

      package com.xgj.aop.spring.advisor.aspectJAdvance.bindTypeAnnoObj; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; /** * * * @ClassName: BindTypeAnnoObjectAspect * * @Description: @Aspect標(biāo)注的切面 * * (1)通過(2)處查找出m對應(yīng)Monitor類型的注解, 因而真實(shí)的切點(diǎn)表達(dá)式為@within * (Monitor),當(dāng)增強(qiáng)方法織入目標(biāo) 連接點(diǎn)時,增強(qiáng)方法通過m入?yún)⒖梢砸玫竭B接點(diǎn)處的注解對象。 * * @author: Mr.Yang * * @date: 2017年9月12日 下午4:27:55 */ @Aspect public class BindTypeAnnoObjectAspect { // (1) @Before("@within(m)") public void bindTypeAnno(Monitor m) { // (2) System.out.println("----bindTypeAnnoObject()----"); System.out.println(m.getClass().getName()); System.out.println("----bindTypeAnnoObject()----"); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      (1)通過(2)處查找出m對應(yīng)Monitor類型的注解, 因而真實(shí)的切點(diǎn)表達(dá)式為@within(Monitor),當(dāng)增強(qiáng)方法織入目標(biāo) 連接點(diǎn)時,增強(qiáng)方法通過m入?yún)⒖梢砸玫竭B接點(diǎn)處的注解對象。

      配置文件

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      測試類

      package com.xgj.aop.spring.advisor.aspectJAdvance.bindTypeAnnoObj; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BindTypeAnnoObjectAspectTest { @Test public void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext( "classpath:com/xgj/aop/spring/advisor/aspectJAdvance/bindTypeAnnoObj/conf-bindTypeAnnoObj.xml"); Bussiness bussiness = ctx.getBean("bussiness", Bussiness.class); bussiness.dealBussinessOne(); bussiness.dealBussinessTwo(); } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      輸出結(jié)果

      2017-09-12 16:58:15,464 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@292898f5: startup date [Tue Sep 12 16:58:15 BOT 2017]; root of context hierarchy 2017-09-12 16:58:15,684 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJAdvance/bindTypeAnnoObj/conf-bindTypeAnnoObj.xml] ----bindTypeAnnoObject()---- com.sun.proxy.$Proxy6 ----bindTypeAnnoObject()---- dealBussinessOne executed ----bindTypeAnnoObject()---- com.sun.proxy.$Proxy6 ----bindTypeAnnoObject()---- dealBussinessTwo executed

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      從輸出信息中,com.sun.proxy.$Proxy6,即使用CGLib代理NaiveWaiter時,其類的注解Monitorable對象也被代理了.

      AOP Spring

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:能做成幻燈片嗎(制作幻燈片不可以做什么)
      下一篇:如何在Excel單元格中的每個單詞之前添加字符?
      相關(guān)文章
      亚洲国产一二三精品无码| 亚洲AV无码一区二三区| 亚洲AV电影天堂男人的天堂| 亚洲一区二区三区乱码在线欧洲| 亚洲视频中文字幕在线| 久久久久亚洲av无码专区 | 久久精品国产亚洲综合色| 国产亚洲美女精品久久久2020| 亚洲av无码国产精品色在线看不卡| 亚洲国产美女精品久久久| 亚洲一级特黄特黄的大片| 亚洲一级毛片免费看| 亚洲成AV人综合在线观看| 亚洲综合久久1区2区3区| 精品亚洲A∨无码一区二区三区| 亚洲高清专区日韩精品| 国产V亚洲V天堂A无码| 亚洲av无码一区二区三区乱子伦| 亚洲精品线在线观看| 亚洲成A人片在线观看无码不卡| 亚洲成A人片在线观看无码不卡 | 亚洲人成在线播放网站| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 亚洲午夜福利在线观看| 久久精品国产亚洲综合色| 337p日本欧洲亚洲大胆人人| 国产91在线|亚洲| 亚洲欧洲免费无码| 亚洲另类无码专区首页| 亚洲色大网站WWW永久网站| 亚洲av日韩综合一区久热| 国产AV日韩A∨亚洲AV电影| 亚洲天堂中文字幕在线| 亚洲人成人77777网站| 亚洲AV人无码激艳猛片| 亚洲福利一区二区精品秒拍| 亚洲综合色7777情网站777| 亚洲精华国产精华精华液| 亚洲国产成人精品久久久国产成人一区二区三区综 | 亚洲伊人久久成综合人影院| 亚洲精品乱码久久久久久自慰|