标签:targe 配置 bwt jta mod smm ble 单例 before
一般我们可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames实现,但如果需要代理的bean很多,无疑会对spring配置文件的编写带来繁重的工作,这时就该BeanNameAutoProxyCreator出场了。
(一)ProxyFactoryBean属性介绍
target:代理的目标类
proxyInterfaces:代理类应该实现的接口列表
interceptorNames:需要应用到目标对象上的通知Bean的名字.可以是拦截器,advisor和其他通知类型的名字。这个属性必须按照在BeanFactory中的顺序设置
singleton:单例
aopProxyFactory:使用的ProxyFactoryBean实现。Spring带有两种实现(JDK动态代理和CGLIB)。通常不需要使用这个属性
exposeProxy:目标对象是否需要得到当前的代理。通过调用AopContext.getCurrentProxy实现。
frozen:一旦工厂被创建,是否可以修改代理的通知。当设置为true时,在运行时就不能修改ProxyFactoryBean了。通常不需要使用这个属性。
optimize:是否对创建的代理进行优化(只适用于CGLIB)
ProxyTargetClass:是否代理目标类,而不是实现接口。只能在使用CGLIB时使用
现在说下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("执行方法.");
}
@Override
public void tst(int status, String name) {
System.out.println("tst(int status, String name)" );
}
}
LoggerAdvice.class
/**
* 日志代理
* @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配置:
<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");
}
执行结果:
2012-08-14 14:57:30 [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[DEBUG] Returning cached instance of singleton bean ‘proxyBean‘
2012-08-14 14:57:30 [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[DEBUG] Returning cached instance of singleton bean ‘loggerAdvice‘
2012-08-14 14:57:30 [org.springframework.aop.framework.ProxyFactoryBean]-[DEBUG] Advice has changed; recaching singleton instance
2012-08-14 14:57:30 [org.springframework.aop.framework.JdkDynamicAopProxy]-[DEBUG] Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [spring.aop.TestProxyFactoryBean@11978b]
2012-08-14 14:57:30 [spring.aop.TestProxyFactoryBean]-[DEBUG] +Class : spring.aop.TestProxyFactoryBean
2012-08-14 14:57:30 [spring.aop.TestProxyFactoryBean]-[DEBUG] +-------Method : tst
执行方法.
2012-08-14 14:57:30 [org.springframework.beans.factory.support.DefaultListableBeanFactory]-[DEBUG] Returning cached instance of singleton bean ‘proxyBean‘
2012-08-14 14:57:30 [spring.aop.TestProxyFactoryBean]-[DEBUG] +Class : spring.aop.TestProxyFactoryBean
2012-08-14 14:57:30 [spring.aop.TestProxyFactoryBean]-[DEBUG] +-------Method : tst
2012-08-14 14:57:30 [spring.aop.TestProxyFactoryBean]-[DEBUG] +-arg0 : 1
2012-08-14 14:57:30 [spring.aop.TestProxyFactoryBean]-[DEBUG] +-arg1 : 123
tst(int status, String name)
(二)BeanNameAutoProxyCreator属性介绍
target:代理的目标类
beanNames:需要代理的bean的列表
interceptorNames:需要应用到目标对象上的通知Bean的名字.可以是拦截器,advisor和其他通知类型的名字。这个属性必须按照在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>
调用代码:
public static void main(String[] args){
context.getBean("testfunc",TestFunc.class).test("project", 100);
context.getBean("testfunc",TestFunc.class).test1("project1");
}
出处:http://www.cnblogs.com/65702708/archive/2012/08/14/2638043.html
【Spring】25、Spring代理。 BeanNameAutoProxyCreator 与 ProxyFactoryBean
标签:targe 配置 bwt jta mod smm ble 单例 before
原文地址:http://www.cnblogs.com/wangzhongqiu/p/7527707.html