Spring Learning (16)-----Spring Auto Proxy Creator

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.