标签:eth ext 匹配 连接点 process property efault work []
在前面的例子中, 我们都通过ProxyFactoryBean创建织入切面的代理, 每一个需要被代理的bean都需要使用一个ProxyFactoryBean进行配置, 虽然可以使用父子bean进行改造,但还是很麻烦.对于小型系统而言,这种方法还可以将就使用, 但是对于很多需要代理的bean的系统, 再使用这种方法就会很麻烦.
幸运的是, spring为我们提供了自动创建代理的机制, 容器为我们自动生成代理, 把我们从繁琐的配置工作中解放出来.在内部, Spring使用BeanPostFactory完成这项工作.
3)基于Bean中AspjectJ注解标签的自动代理创建器:为包含AspectJ注解的Bean自动创建代理实例,它的实现类是AnnotationAwareAspectJAutoProxyCreator,该类是Spring 2.0的新增类。
使用Bean名进行自动代理配置:
<bean id="waiterTarget" class="com.bao.bao.advisor.Waiter"/>
<bean id="sellerTarget" class="com.bao.bao.advisor.Seller"/>
<bean id="greetAdvice" class="com.bao.bao.advisor.GreetingBeforeAdvice"/>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames" value="*Target"/>
<property name="interceptorNames" value="greetAdvice"/>
<property name="proxyTargetClass" value="true"/>
</bean>
BeanNameAutoProxyCreator 有一个beanNames属性,它允许用户指定一组需要自动代理的Bean名称,Bean名称可以使用 * 通配符。
测试代码:
package com.bao.bao.advisor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by xinfengyao on 16-10-22.
*/
public class TestAdvisor {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-base.xml");
Waiter waiter = (Waiter) context.getBean("waiterTarget");
Seller seller = (Seller) context.getBean("sellerTarget");
waiter.greetTo("tom");
waiter.serveTo("tom");
seller.greetTo("tom");
}
}
运行结果:
Here are you! tom
waiter greetTo tom...
Here are you! tom
waiter serving tom...
Here are you! tom
seller greet to tom...
可以看到, 都自动织入了切面
<bean id="waiterTarget" class="com.bao.bao.advisor.Waiter"/>
<bean id="sellerTarget" class="com.bao.bao.advisor.Seller"/>
<bean id="greetAdvice" class="com.bao.bao.advisor.GreetingBeforeAdvice"/>
<bean id="regexpAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="greetAdvice"/>
<property name="patterns">
<list>
<value>.*greet.*</value>
</list>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
测试代码:
package com.bao.bao.advisor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by xinfengyao on 16-10-22.
*/
public class TestAdvisor {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring/spring-base.xml");
Waiter waiter = (Waiter) context.getBean("waiterTarget");
Seller seller = (Seller) context.getBean("sellerTarget");
waiter.greetTo("tom");
waiter.serveTo("tom");
seller.greetTo("tom");
}
}
运行结果:
Here are you! tom
waiter greetTo tom...
waiter serving tom...
Here are you! tom
seller greet to tom...
标签:eth ext 匹配 连接点 process property efault work []
原文地址:http://www.cnblogs.com/NewMan13/p/5988791.html