spring aop 自动代理机制

我们一直使用ProxyFactoryBean来显式的创建AOP代理。但是在很多场合,这种方式将会使编写配置文件的工作量大大增加;由于要从ProxyFactoryBean获得代理对象,也会使应用和Spring之间的耦合度增加。下面介绍使用Spring提供的自动代理机制来解决这类问题。

1、使用BeanNameAutoProxyCreator

Spring提供的BeanNameAutoProxyCreator类允许我们通过Bean的name属性来指定代理的Bean。它暴露了java.lang.String[]类型的beanNames和 interceptorNames属性。beanNames可以指定被代理的Bean名字列表,支持“*”通配符,例如“*DAO”表示所有名字以“DAO”结尾的Bean。interceptorNames指定通知(Advice)列表,或者通知者(Advisor)列表。

下面通过一个例程来演示如何使用BeanNameAutoProxyCreator。在例子中,有两个Bean:TestBeanA和BeanB,并在TestMain类中的main方法中调用其MyMethod()方法。自动代理将会在方法调用前自动的执行配置的前置通知,输出提示信息。

新建一个名字为AOP_Test4.10的工程,添加Spring的IoC和AOP库后,新建一aop.test包,再分别创建两个类TestBeanA和BeanB,添加MyMethod()方法,代码如下:

代码 查看源代码copy to clipboard打印
/**  
*
*/
package aop.test;
/**
* @author zhangyong
*
*/
public class TestBeanA {
public void MyMethod() {
System.out.println(this.getClass().getName()
+ ".MyMethod() is run!");
}
}
/**
*
*/
package aop.test;
/**
* @author zhangyong
*
*/
public class TestBeanA {
public void MyMethod() {
System.out.println(this.getClass().getName()
+ ".MyMethod() is run!");
}
}



代码 查看源代码copy to clipboard打印
/**  
*
*/
package aop.test;
/**
* @author zhangyong
*
*/
public class BeanB {
public void MyMethod() {
System.out.println(this.getClass().getName()
+ ".MyMethod() is run!");
}
}
/**
*
*/
package aop.test;
/**
* @author zhangyong
*
*/
public class BeanB {
public void MyMethod() {
System.out.println(this.getClass().getName()
+ ".MyMethod() is run!");
}
}

  再创建前置通知类BeforeAdvice:

代码 查看源代码copy to clipboard打印
/**  
*
*/
package aop.test;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
/**
* @author zhangyong
*
*/
public class BeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println(method.getName() + "(),将要运行!");
}
}
/**
*
*/
package aop.test;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
/**
* @author zhangyong
*
*/
public class BeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target)
throws Throwable {
System.out.println(method.getName() + "(),将要运行!");
}
}
  最后创建含有main方法的测试类TestMain:

代码 查看源代码copy to clipboard打印
/**  
*
*/
package aop.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author zhangyong
*
*/
public class TestMain {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"applicationContext.xml");
TestBeanA beanA = (TestBeanA)ac.getBean("TestBeanA");
beanA.MyMethod();
BeanB beanB = (BeanB)ac.getBean("BeanB");
beanB.MyMethod();
}
}
/**
*
*/
package aop.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author zhangyong
*
*/
public class TestMain {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"applicationContext.xml");
TestBeanA beanA = (TestBeanA)ac.getBean("TestBeanA");
beanA.MyMethod();
BeanB beanB = (BeanB)ac.getBean("BeanB");
beanB.MyMethod();
}
}

  在配置文件中配置Bean和自动代理Bean,完成后代码如下:

代码 查看源代码copy to clipboard打印
<?xml version="1.0" encoding="UTF-8"?>  
<beans …………>
<bean id="TestBeanA" class="aop.test.TestBeanA"/>
<bean id="BeanB" class="aop.test.BeanB"/>
<bean id="BeforeAdvice" class="aop.test.BeforeAdvice"></bean>

<bean class="org.springframework.aop.framework.autoproxy.
BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>Test*</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>BeforeAdvice</value>
</list>
</property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans …………>
<bean id="TestBeanA" class="aop.test.TestBeanA"/>
<bean id="BeanB" class="aop.test.BeanB"/>
<bean id="BeforeAdvice" class="aop.test.BeforeAdvice"></bean>

<bean class="org.springframework.aop.framework.autoproxy.
BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>Test*</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>BeforeAdvice</value>
</list>
</property>
</bean>
</beans>

  运行主类,输出结果如下:
[img]http://www.blogjava.net/images/blogjava_net/cmzy/WindowsLiveWriter/Spring_139E1/AutoProxy1_thumb.jpg[/img]


  可以看到,在主类TestMain中,我们是直接从Spring IoC容器中获取收管Bean而不是像以前那样从ProxyFactoryBean中获取代理,但是我们的前置通知BeforeAdvice仍然在TestBeanA对象的MyMethod()方法执行前被触发,这说明我们的自动代理正在工作。



2、使用DefaultAdvisorAutoProxyCreator

  DefaultAdvisorAutoProxyCreator允许我们只需定义相应的Advisor通知者,就可以完成自动代理。配置好DefaultAdvisorAutoProxyCreator受管Bean后,它会自动查找配置文件中定义的Advisor,并将它们作用于所有的Bean。

修改例程4.10的配置文件,使用DefaultAdvisorAutoProxyCreator来完成自动代理。完成后配置文件代码如下(本例完整工程代码见例程4.11):

代码 查看源代码copy to clipboard打印
<?xml version="1.0" encoding="UTF-8"?>  
<beans ……>
<bean id="TestBeanA" class="aop.test.TestBeanA" />
<bean id="BeanB" class="aop.test.BeanB" />
<bean id="BeforeAdvice" class="aop.test.BeforeAdvice"/>
<bean class="org.springframework.aop.framework.autoproxy.
DefaultAdvisorAutoProxyCreator" />

<bean class="org.springframework.aop.support.NameMatchMethod
PointcutAdvisor">
<property name="advice" ref="BeforeAdvice" />
<property name="mappedNames">
<list>
<value>*Method*</value>
</list>
</property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans ……>
<bean id="TestBeanA" class="aop.test.TestBeanA" />
<bean id="BeanB" class="aop.test.BeanB" />
<bean id="BeforeAdvice" class="aop.test.BeforeAdvice"/>
<bean class="org.springframework.aop.framework.autoproxy.
DefaultAdvisorAutoProxyCreator" />

<bean class="org.springframework.aop.support.NameMatchMethod
PointcutAdvisor">
<property name="advice" ref="BeforeAdvice" />
<property name="mappedNames">
<list>
<value>*Method*</value>
</list>
</property>
</bean>
</beans>

  运行主类输出结果如下:

[img]http://www.blogjava.net/images/blogjava_net/cmzy/WindowsLiveWriter/Spring_139E1/AutoProxy2_thumb.jpg[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值