Spring Automatic proxy mechanism

Source: Internet
Author: User
Tags aop

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!");
}
}

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.