實例
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster
在 Spring-AOP 靜態普通方法名匹配切面 案例中,我們通過配置兩個ProxyFactoryBean分別為waiter和seller的Bean創建代理對象,
如下
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
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:p = "http://www.springframework.org/schema/p" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <!-- 配置切面:靜態方法匹配切面 --> <!-- Waiter目標類 --> < bean id = "waiterTarget" class = "com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.Waiter" /> <!-- Seller目標類 --> < bean id = "sellerTarget" class = "com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.Seller" /> <!-- 前置增強 --> < bean id = "greetBeforeAdvice" class = "com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.GreetBeforeAdivce" /> <!-- 切面 --> < bean id = "greetAdvicesor" class = "com.xgj.aop.spring.advisor.StaticMethodMatcherPointcutAdvisor.GreetingAdvisor" p:advice-ref = "greetBeforeAdvice" /> <!-- 向切面注入一個前置增強 --> <!-- 通過父bean,配置公共的信息 --> < bean id = "parent" abstract = "true" class = "org.springframework.aop.framework.ProxyFactoryBean" p:interceptorNames = "greetAdvicesor" p:proxyTargetClass = "true" /> <!-- waiter代理 --> < bean id = "waiter" parent = "parent" p:target-ref = "waiterTarget" /> <!-- seller代理 --> < bean id = "seller" parent = "parent" p:target-ref = "sellerTarget" /> </ beans > |
下面我們通過BeanNameAtuoProxyCreator以更優雅更快捷的方式完成相同的功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:p = "http://www.springframework.org/schema/p" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <!-- 通過Bean名稱自動創建代理 --> <!-- 目標Bean --> < bean id = "waiter" class = "com.xgj.aop.spring.advisor.autoCreateProxy.BeanNameAutoProxyCreator.Waiter" /> < bean id = "seller" class = "com.xgj.aop.spring.advisor.autoCreateProxy.BeanNameAutoProxyCreator.Seller" /> <!-- 增強 --> < bean id = "greetingBeforeAdvice" class = "com.xgj.aop.spring.advisor.autoCreateProxy.BeanNameAutoProxyCreator.GreetingBeforeAdvice" /> <!-- 代理 p:beanNames="waiter,seller" --> < bean class = "org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" p:beanNames = "*er" p:interceptorNames = "greetingBeforeAdvice" p:optimize = "true" /> </ beans > |
BeanNameAutoProxyCreator有一個beanNames屬性,它允許用戶指定一組需要自動代理的Bean名稱,Bean名稱可以使用*通配符。
假設Spring容器中有waiter和seller外還有其他的bean, 就可以通過beanNames屬性設定為“*er” 使wiater和seller這兩個bean被自動代理。 當然,如果還有其他以er結尾的bean也會被自動代理器創建代理,為了保險起見,可以使用
<property name="beanNames" value="waiter,seller">的方式限定范圍。
一般不會為FactoryBean的Bean創建代理,如果剛好有這樣一個需求,這需要在beanNames中指定添加 的Bean 名 稱 , 如 ‘ <property name="beanNames"value" 的Bean名稱,如`<property name="beanNames" value=" 的Bean名稱,如‘<propertyname="beanNames"value="waiter">`
BeanNameAutoProxyCreator的interceptorNames屬性指定一個或者多個Bean的名稱。
此外還有一個常用的optimize屬性,如果將此屬性設置為true,則將強制使用CGLib動態代理技術。
通過這樣的配置后,容器在創建waiter和seller Bean的實例是,就會自動為他們創建代理對象,而這一操作對使用者來講完全是透明的。
測試類如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
package com.xgj.aop.spring.advisor.autoCreateProxy.BeanNameAutoProxyCreator; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanNameAutoProxyCreatorTest { @Test public void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext( "classpath:com/xgj/aop/spring/advisor/autoCreateProxy/BeanNameAutoProxyCreator/conf-beanNameAutoProxy.xml" ); Waiter waiter = ctx.getBean( "waiter" , Waiter. class ); waiter.greetTo( "XiaoGongJiang" ); waiter.serverTo( "XiaoGongJiang" ); System.out.println( "\n" ); Seller seller = ctx.getBean( "seller" , Seller. class ); seller.greetTo( "XiaoGongJiang" ); seller.serverTo( "XiaoGongJiang" ); } } |
運行結果如下:
2017-08-21 16:12:48,086 INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5f0101fb: startup date [Mon Aug 21 16:12:48 BOT 2017]; root of context hierarchy
2017-08-21 16:12:48,204 INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/autoCreateProxy/BeanNameAutoProxyCreator/conf-beanNameAutoProxy.xml]
Pointcut:com.xgj.aop.spring.advisor.autoCreateProxy.BeanNameAutoProxyCreator.Waiter.greetTo
How are you XiaoGongJiang ?
Waiter Greet To XiaoGongJiang
Pointcut:com.xgj.aop.spring.advisor.autoCreateProxy.BeanNameAutoProxyCreator.Waiter.serverTo
How are you XiaoGongJiang ?
Waiter Server To XiaoGongJiang
Pointcut:com.xgj.aop.spring.advisor.autoCreateProxy.BeanNameAutoProxyCreator.Seller.greetTo
How are you XiaoGongJiang ?
Seller Greet To XiaoGongJiang
Pointcut:com.xgj.aop.spring.advisor.autoCreateProxy.BeanNameAutoProxyCreator.Seller.serverTo
How are you XiaoGongJiang ?
Seller Server To XiaoGongJiang
通過輸出信息可以得知,從容器返回的Bean的 全部方法都被織入了增強。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://artisan.blog.csdn.net/article/details/77466466