We have been using Proxyfactorybean to explicitly create an AOP agent. But on many occasions, this approach will increase the workload of writing configuration files, and the coupling between applications and spring will increase as the proxy object is obtained from Proxyfactorybean. The following is an introduction to using the automatic proxy mechanism provided by spring to solve this type of problem.
1. Use Beannameautoproxycreator
The Beannameautoproxycreator class provided by spring allows us to specify the agent's bean through the bean's Name property. It exposes the Beannames and Interceptornames properties of the java.lang.string[] type. Beannames can specify a list of bean names to be represented and support "*" wildcard characters, such as "*dao" to represent all beans whose names end with "DAO". INTERCEPTORNAMES Specifies the list of notifications (Advice) or the list of notifications (Advisor).
Here's an example that shows how to use Beannameautoproxycreator. In the example, there are two Bean:testbeana and beanb, and the MyMethod () method is called in the Main method in the Testmain class. The automatic agent will automatically execute the configured predecessor notification before the method call, outputting the hint information.
Create a new project named aop_test4.10, add the spring IOC and the AOP Library, build a new Aop.test package, add the two classes Testbeana and BEANB separately, adding the MyMethod () method with the following code:
Code
/**
*
*/
package aop.test;
/**
* @author zhangyong
*
*/
public class TestBeanA {
public void MyMethod() {
System.out.println(this.getClass().getName()
+ ".MyMethod() is run!");
}
}
Code
/**
*
*/
package aop.test;
/**
* @author zhangyong
*
*/
public class BeanB {
public void MyMethod() {
System.out.println(this.getClass().getName()
+ ".MyMethod() is run!");
}
}