1. beannameautoproxycreator Example
Before you do this, you must manually create a proxy bean (Proxyfactrybean).
<beans xmlns= "Http://www.springframework.org/schema/beans"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-2.5.xsd "><bean id= "CustomerService"class= "Com.yiibai.customer.services.CustomerService" > <property name= "name" value= "Yiibai Mook Kim"/> & Lt;property name= "url" value= "http://www.yiibai.com"/> </bean> <bean id= "Hijackaroundmethodbeanadvice"class= "Com.yiibai.aop.HijackAroundMethod"/> <bean id= "Customerserviceproxy"class= "Org.springframework.aop.framework.ProxyFactoryBean" > <property name= "target" ref= "CustomerService"/> <property name= "Interceptornames" > <list> <value>customeradvisor</valu e> </list> </property> </bean> <bean id= "Customeradvisor"class= "Org.springframework.aop.support.NameMatchMethodYiibaicutAdvisor" > <property name= "mappedname" value= " Printname "/> <property name=" Advice "ref=" Hijackaroundmethodbeanadvice "/> </bean></beans>
Xml
Use the proxy name "Customerserviceproxy" to get the bean.
= (CustomerService)appContext.getBean("customerServiceProxy");
Java
In the auto-proxy mechanism, you only need to create one beannameautoproxycreator and include all your beans (by the bean's name, or the regular expression name) and "advisor" as a unit.
<beans xmlns= "Http://www.springframework.org/schema/beans"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-2.5.xsd "><bean id= "CustomerService"class= "Com.yiibai.customer.services.CustomerService" > <property name= "name" value= "Yiibai Mook Kim"/> & Lt;property name= "url" value= "http://www.yiibai.com"/> </bean> <bean id= "Hijackaroundmethodbeanadvice"class= "Com.yiibai.aop.HijackAroundMethod"/> <bean id= "Customeradvisor"class= "Org.springframework.aop.support.NameMatchMethodYiibaicutAdvisor" > <property name= "mappedname" value= " Printname "/> <property name=" Advice "ref=" Hijackaroundmethodbeanadvice "/> </bean> <Beanclass= "Org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" > <property name= "Beannames" > <list> <value>*Service</value> </list> </property> <property name= "Interceptornames" > <list> <value>customeradvisor</valu e> </list> </property> </bean></beans>
Xml
Now, you can get the bean by the original name of "CustomerService", if you know that the bean has been proxied.
= (CustomerService)appContext.getBean("customerService");
Java2. defaultadvisorautoproxycreator Example
This DefaultAdvisorAutoProxyCreator
is very powerful, and if there is a bean associated with it, spring will automatically create a proxy.
<beans xmlns= "Http://www.springframework.org/schema/beans"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-2.5.xsd "><bean id= "CustomerService"class= "Com.yiibai.customer.services.CustomerService" > <property name= "name" value= "Yiibai Mook Kim"/> & Lt;property name= "url" value= "http://www.yiibai.com"/> </bean> <bean id= "Hijackaroundmethodbeanadvice"class= "Com.yiibai.aop.HijackAroundMethod"/> <bean id= "Customeradvisor"class= "Org.springframework.aop.support.NameMatchMethodYiibaicutAdvisor" > <property name= "mappedname" value= " Printname "/> <property name=" Advice "ref=" Hijackaroundmethodbeanadvice "/> </bean> <bea Nclass= "Org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/></beans>
Xml
Instead of using any proxy method, Spring will have the most appropriate approach.
Download code –HTTP://PAN.BAIDU.COM/S/1PKDQTJT
Spring Learning (16)-----Spring Auto Proxy Creator