Spring Basic Series 11--Auto Create proxy reprint: http://www.cnblogs.com/leiOOlei/p/3557964.html
In the example in the Spring3 series 9-spring Aop--advice and the Spring3 series 10-spring aop--pointcut,advisor interception designation method, in the configuration file, You must manually create a proxy bean (proxyfactorybean) for each bean that requires AOP.
This is not a good experience, for example, if you want all beans in the DAO layer to support AOP in order to write SQL logs, then you have to create a lot of Proxyfactorybeanmanually, which will directly result in a geometric multiplication of your XML configuration file content. Not conducive to XML configuration maintenance.
Fortunately, Spring has two ways to automatically create a proxy for you.
1. Automatically create proxy using Beannameautoproxycreator
Create the Proxyfactorybean manually as follows:
<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.O Rg/schema/beans/spring-beans-2.5.xsd "> <bean id=" customerservice "class=" Com.lei.demo.aop.advice.CustomerService "> <property name=" name "value=" Leioolei "/> <property na Me= "url" value= "http://www.cnblogs.com/leiOOlei/"/> </bean> <bean id= "Hijackaroundmethodbean" class= " Com.lei.demo.aop.advice.HijackAroundMethod "/> <bean id=" Customerserviceproxy "class=" Org.springframewor K.aop.framework.proxyfactorybean "> <property name=" target "ref=" customerservice "/> <property na Me= "Interceptornames" > <list> <value>customerAdvisor</value> < ;/list> </property> </bean> <bean id= "Customeradvisor"class=" Org.springframework.aop.support.NameMatchMethodPointcutAdvisor "> <property name=" mappedname "Valu E= "Printname"/> <property name= "Advice" ref= "Hijackaroundmethodbean"/> </bean></beans>
To get customerserviceproxyafter configuration, you need the following code
CustomerService cust = (customerservice) appcontext.getbean ("Customerserviceproxy");
In auto mode, you need to create a beannameautoproxycreatorthat will make all the beans (by name or regular expression) and the Advisor form a separate unit, configured as follows:
<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.O Rg/schema/beans/spring-beans-2.5.xsd "> <bean id=" customerservice "class=" Com.lei.demo.aop.advice.CustomerService "> <property name=" name "value=" Leioolei "/> <property na Me= "url" value= "http://www.cnblogs.com/leiOOlei/"/> </bean> <bean id= "Hijackaroundmethodbeanadvice" cl ass= "Com.lei.demo.aop.advice.HijackAroundMethod"/> <bean class= " Org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator "> <property name=" Beannames "> <list> <value>*Service</value> </list> </property> <property name= "Interceptornames" > <list> <value>customeradvisor</value& Gt </list> </property> </bean><bean id= "customeradvisor" class= "org.springframework. Aop.support.NameMatchMethodPointcutAdvisor "> <property name=" mappedname "value=" Printname "/> <p Roperty name= "Advice" ref= "Hijackaroundmethodbeanadvice"/></bean></beans>
As long as the bean ID conforms to *service in the above configuration, proxy will be created automatically, so you can get the proxy with the following code.
CustomerService cust = (customerservice) appcontext.getbean ("CustomerService");
2. Create a proxy with Defaultadvisorautoproxycreator
This way the use of defaultadvisorautoproxycreator implementation of automatic proxy creation, this way is very powerful, any matching advisor of the bean, will automatically create a proxy implementation of AOP, so use caution.
<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.O Rg/schema/beans/spring-beans-2.5.xsd "> <bean id=" customerservice "class=" Com.lei.demo.aop.advice.CustomerService "> <property name=" name "value=" Leioolei "/> <property na Me= "url" value= "http://www.cnblogs.com/leiOOlei/"/> </bean> <bean id= "Hijackaroundmethodbeanadvice" cl ass= "Com.lei.demo.aop.advice.HijackAroundMethod"/> <bean id= "customeradvisor" class= "Org.springframework.ao P.support.namematchmethodpointcutadvisor "> <property name=" mappedname "value=" Printname "/> <pro Perty name= "Advice" ref= "Hijackaroundmethodbeanadvice"/> </bean> <bean class= "org.springframework.a Op.framework.autoproxy.DefaultAdvisorAutoProxyCreator "/> </beans>
In the example above, any bean in the XML, as long as the method name is printname, uses the following code to automatically create a proxy to support AOP.
CustomerService cust = (customerservice) appcontext.getbean ("CustomerService");
Spring Basic Series 11--Automatic proxy creation