In the previous example, we created the proxyfactorybean of the weaving plane by using the, each bean that needs to be proxied needs to be configured with a Proxyfactorybean, although the parent-child bean can be used to transform it, But it's still a lot of trouble. For small systems, this approach can be used, but for many of the systems that require agent beans, this approach is cumbersome.
Fortunately, Spring provides us with the mechanism to automatically create proxies that automatically generate proxies for us, freeing us from tedious configuration work. Internally, Spring uses beanpostfactory to do the work.
1. Introduction of Implementation classThese implementation classes of the Beanpostprocessor-based automatic proxy creator will automatically generate proxy instances for the matching bean when the container instantiates the bean based on some rules. 1) automatic proxy creator based on bean configuration name rule: Allows the proxy creator of the proxy instance to be created automatically for a specific set of configuration names, implementing class Beannameautoproxycreator;2) automatic proxy creator based on the advisor matching mechanism: It scans all the advisors in the container, automatically applies them to the matching bean (that is, creates a proxy instance for the target bean), The implementation class is defaultadvisorautoproxycreator;
3) automatic proxy creator based on ASPJECTJ annotation tags in beans: automatically creates proxy instances for beans that contain ASPECTJ annotations, and its implementation class is Annotationawareaspectjautoproxycreator, which is the spring 2.0 of new classes.
2, Beannameautoproxycreator
Automatic proxy configuration using the bean name:
class class class= "Com.bao.bao.advisor.GreetingBeforeAdvice"/>
<bean class= "Org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >
<property name= "Beannames" value= "*target"/>
<property name= "Interceptornames" value= "Greetadvice"/>
<property name= "Proxytargetclass" value= "true"/>
</bean>
Beannameautoproxycreator has a Beannames property that allows the user to specify a set of bean names that require an automatic proxy, and the bean name can use the * wildcard character.
Test code:
PackageCom.bao.bao.advisor;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;/*** Created by Xinfengyao on 16-10-22.*/ Public classTestadvisor { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Spring/spring-base.xml"); Waiter Waiter= (Waiter) Context.getbean ("Waitertarget"); Seller Seller= (Seller) context.getbean ("Sellertarget"); Waiter.greetto ("Tom"); Waiter.serveto ("Tom"); Seller.greetto ("Tom"); }}
Operation Result:
Here is you! tomwaiter Greetto Tom ... here is you! Tomwaiter serving Tom ... here is you! Tomseller greet to Tom ...
As you can see, it's automatically woven into the plane.
3, DefaultadvisorautoproxycreatorAdvisor is a pointcut and an enhanced complex, and the advisor itself already contains enough information: the crosscutting logic (what to weave) and the connection point (where to weave into). The Defaultadvisorautoproxycreator is able to scan the advisor in the container and automatically weaves the advisor into the matching target bean, which automatically creates the proxy for the matching target bean.
class class class= "Com.bao.bao.advisor.GreetingBeforeAdvice"/>
<bean id= "Regexpadvisor" class= "Org.springframework.aop.support.RegexpMethodPointcutAdvisor" >
<property name= "Advice" ref= "Greetadvice"/>
<property name= "Patterns" >
<list>
<value>.*greet.*</value>
</list>
</property>
</bean>
<bean class= "Org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
Test code:
PackageCom.bao.bao.advisor;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;/*** Created by Xinfengyao on 16-10-22.*/ Public classTestadvisor { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Spring/spring-base.xml"); Waiter Waiter= (Waiter) Context.getbean ("Waitertarget"); Seller Seller= (Seller) context.getbean ("Sellertarget"); Waiter.greetto ("Tom"); Waiter.serveto ("Tom"); Seller.greetto ("Tom"); }}
Operation Result:
Here is you! Tomwaiter Greetto tom...waiter serving Tom ... here is you! Tomseller greet to Tom ...
Automatically create agents