Spring Automatic Agent Creator
Preface:
In the classic spring Aop, you can manually create proxy beans for the target beans. The configuration file must declare a proxy for each bean that needs to be enhanced. As a result, a large number of proxy beans are declared in the configuration file.
In the classic Spring Aop, Spring provides an automatic proxy creator. With the automatic proxy creator, you no longer need to use ProxyFactoryBean to manually create proxy.
Interfaces Animal and Book:
package ; public interface Animal { public void eat(); public void drink(); }
package ; public interface Book { public void read(); }
Target Class:
package ; public class Human implements Animal, Book{ @Override public void eat() { ("eat..."); } @Override public void drink() { ("drink..."); } @Override public void read() { ("read..."); } }
Pre- and post-notifications:
package ; import ; import ; public class MethodBefore implements MethodBeforeAdvice { public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { ("before " + ()); } }
package ; import ; import ; public class MethodAfter implements AfterReturningAdvice { public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { ( "after " + ()); } }
Spring configuration file:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="/schema/beans" xmlns:xsi="http:///2001/XMLSchema-instance" xsi:schemaLocation="/schema/beans /schema/beans/"> <!-- Define the target object --> <bean class=""></bean> <!-- Define notifications --> <bean class=""></bean> <bean class=""></bean> <!-- Define entry point --> <bean class=""> <property name="mappedNames"> <list> <value>eat</value> <value>read</value> </list> </property> </bean> <!-- Define post-enhancing(Related notifications and entry points) --> <bean class=""> <property name="advice" ref="afterAdvice"></property> <property name="pointcut" ref="methodNamePointcut"></property> </bean> <!-- Define a pre-enhancing device(Related notifications and entry points) --> <bean class=""> <property name="advice" ref="beforeAdvice"></property> <property name="expression"> <value>execution(* *.*in*(..))</value><!-- Matchabledrink --> </property> </bean> <!-- Define an automatic proxy creator --> <bean class=""> <property name="beanNames"> <list> <value>*human</value> </list> </property> <property name="interceptorNames"> <list> <value>AfterMethodNameAdvisor</value> <value>BeforeMethodNameAdvisor</value> </list> </property> </bean> </beans>
The above automatic proxy can create proxy for beans ending in human.
test:
package ; import ; import ; public class Test { /** * @param args */ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( ""); Animal animal = (Animal) ("human"); Book book = (Book) animal; (); (); (); } }
Output:
eat... after eat before drink drink... read... after read
Spring also provides another automatic proxy creator: DefaultAdvisorAutoProxyCreator. This automatic proxy creator does not require any configuration, it will automatically check every enhancer and bean declared in the Ioc container. If there is a bean that matches the enhancer entry point, the DefaultAdvisorAutoProxyCreator will automatically create a proxy for it.
<bean class=""/>
It should be noted that DefaultAdvisorAutoProxyCreator may proxie target beans that do not want to be proxyed, so be extra careful when using them.
Thank you for reading, I hope it can help you. Thank you for your support for this site!