文章出處
文章列表
一般我們可以使用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時使用
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);
}
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)" );
}
}
@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());
}
}
}
* 日志代理
* @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 id="testproxyfactorybean" class="spring.aop.TestProxyFactoryBean" />
<bean id="loggerAdvice" class="spring.aop.LoggerAdvice" />
<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");
}
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)
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>
<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");
}
context.getBean("testfunc",TestFunc.class).test("project", 100);
context.getBean("testfunc",TestFunc.class).test1("project1");
}

相關:
使用BeanNameAutoProxyCreator實現spring的自動代理
文章列表
全站熱搜