Spring BeanNameAutoProxyCreator and ProxyFactoryBean

Source: Internet
Author: User
Generally, we can use ProxyBeanFactory and configure proxyInterfaces, target, and interceptorNames. However, if many beans need to be proxies, it will undoubtedly bring heavy work to the compilation of spring configuration files, beanNameAutoProxyCreator is now available.

(1) Introduction to ProxyFactoryBean attributes

Target: The target class of the proxy.
ProxyInterfaces: List of interfaces that the proxy class should implement
InterceptorNames: the name of the notification Bean to be applied to the target object. It can be the interceptor, advisor, or other notification type names. This attribute must be set in the Order in BeanFactory
Singleton: singleton
AopProxyFactory: Use the ProxyFactoryBean implementation. Spring has two implementations (JDK dynamic proxy and CGLIB ). This attribute is usually not required
ExposeProxy: whether the target object needs to obtain the current proxy. Call AopContext. getCurrentProxy.
Frozen: whether the agent notification can be modified once the factory is created. If it is set to true, the ProxyFactoryBean cannot be modified at runtime. This attribute is usually not required.
Optimize: whether to optimize the created proxy (only applicable to CGLIB)
ProxyTargetClass: whether to proxy the target class, rather than implementing the interface. Only when CGLIB is used

 

 

 

Now let's talk about the usage and configuration of ProxyBeanFactory:

ITest. class

Public interface ITest {
Void tst ();
Void tst (int status, String name );
}

 

 

TestProxyFactoryBean. class

Public class TestProxyFactoryBean implements ITest {

@ Override
Public void tst (){
// TODO Auto-generated method stub
System. out. println ("execution method .");
}

@ Override
Public void tst (int status, String name ){
System. out. println ("tst (int status, String name )");

}

}

LoggerAdvice. class

 

/**
* Log agent
* @ Author Administrator
*
*/
Public class LoggerAdvice implements MethodBeforeAdvice, AfterReturningAdvice {

@ Override
Public void afterReturning (Object returnValue, Method method,
Object [] args, Object target) throws Throwable {
Logger logger = Logger. getLogger (target. getClass ());
If (returnValue! = Null ){
Logger. debug ("+ ------- Return:" + returnValue. toString ());
}

}

@ Override
Public void before (Method method, Object [] args, Object target)
Throws Throwable {
// TODO Auto-generated method stub
Logger logger = Logger. getLogger (target. getClass ());
Logger. debug ("+ Class:" + target. getClass (). getName ());
Logger. debug ("+ ------- Method:" + method. getName ());
For (int I = 0; I <args. length; I ++ ){
Logger. debug ("+-arg" + I + ":" + args [I]. toString ());
}

}

}

 

 

 

Xml configuration:

<Bean class = "spring. aop. TestProxyFactoryBean" id = "testproxyfactorybean"/>

<Bean class = "org. springframework. aop. framework. ProxyFactoryBean">
<Property name = "proxyInterfaces" value = "spring. aop. ITest"> </property>
<Property name = "interceptorNames">

<List>
<Value> loggerAdvice </value>
</List>

</Property>

<Property name = "target" ref = "testproxyfactorybean"> </property>
</Bean>

 

 

Program. class
Public static void main (String [] args ){
Context. getBean ("proxyBean", ITest. class). tst ();
Context. getBean ("proxyBean", ITest. class). tst (1, "123 ");
}

Execution result:

 

14:57:30 [org. springframework. beans. factory. support. DefaultListableBeanFactory]-[DEBUG] Returning cached instance of singleton bean 'proxybe'
14:57:30 [org. springframework. beans. factory. support. DefaultListableBeanFactory]-[DEBUG] Returning cached instance of singleton bean 'loggeradvice'
14:57:30 [org. springframework. aop. framework. ProxyFactoryBean]-[DEBUG] Advice has changed; recaching singleton instance
14:57:30 [org. springframework. aop. framework. JdkDynamicAopProxy]-[DEBUG] Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [spring. aop. TestProxyFactoryBean @ 11978b]
14:57:30 [spring. aop. TestProxyFactoryBean]-[DEBUG] + Class: spring. aop. TestProxyFactoryBean
14:57:30 [spring. aop. TestProxyFactoryBean]-[DEBUG] + ------- Method: tst
Execution method.
14:57:30 [org. springframework. beans. factory. support. DefaultListableBeanFactory]-[DEBUG] Returning cached instance of singleton bean 'proxybe'
14:57:30 [spring. aop. TestProxyFactoryBean]-[DEBUG] + Class: spring. aop. TestProxyFactoryBean
14:57:30 [spring. aop. TestProxyFactoryBean]-[DEBUG] + ------- Method: tst
14:57:30 [spring. aop. TestProxyFactoryBean]-[DEBUG] +-arg0: 1
14:57:30 [spring. aop. TestProxyFactoryBean]-[DEBUG] +-arg1: 123
Tst (int status, String name)

 

 

(2) BeanNameAutoProxyCreator attributes

Target: proxy target class beanNames: List of beans to be proxy

InterceptorNames: the name of the notification Bean to be applied to the target object. It can be the interceptor, advisor, or other notification type names. This attribute must be set in the Order in BeanFactory

<Bean class = "org. springframework. aop. framework. autoproxy. BeanNameAutoProxyCreator">
<Property name = "interceptorNames">
<List>
<Value> loggerAdvice </value>
</List>
</Property>
<Property name = "beanNames">
<List>
<Idref local = "testproxyfactorybean"/>
</List>
</Property>
</Bean>

 

Call code:

Public static void main (String [] args ){

Context. getBean ("testfunc", TestFunc. class). test ("project", 100 );
Context. getBean ("testfunc", TestFunc. class). test1 ("project1 ");
}

 

 

 

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.