Spring-AOP @AspectJ進(jìn)階之綁定類注解對象
文章目錄
概述
實(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
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)容。