码迷,mamicode.com
首页 > 编程语言 > 详细

使用BeanNameAutoProxyCreator实现spring的自动代理

时间:2016-01-18 00:37:40      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:

提到代理,我们可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames实现,但如果需要代理的bean很多,无疑会对spring配置文件的编写带来繁重的工作

Spring为我们提供了,根据beanName匹配后进行自动代理的解决方法

业务接口

 

技术分享package AutoProxyOne;
技术分享
技术分享public interface Shopping {
技术分享  public String buySomething(String type);
技术分享  public String buyAnything(String type);
技术分享  public String sellSomething(String type);
技术分享  public String sellAnything(String type);
技术分享
技术分享
技术分享}
技术分享

 业务实现类A,作为配置文件中的buyBean:

 

技术分享package AutoProxyOne;
技术分享
技术分享public class ShoppingImplA implements Shopping {
技术分享    private Customer customer;
技术分享    public Customer getCustomer() {
技术分享        return customer;
技术分享    }
技术分享    public void setCustomer(Customer customer) {
技术分享        this.customer = customer;
技术分享    }
技术分享    public String buySomething(String type) {
技术分享        System.out.println(this.getCustomer().getName()+" bye "+type+" success");
技术分享        return null;
技术分享    }
技术分享    
技术分享    public String buyAnything(String type) {
技术分享       System.out.println(this.getCustomer().getName()+" bye "+type+" success");
技术分享       return null;
技术分享
技术分享     }
技术分享    public String sellAnything(String type) {
技术分享        System.out.println(this.getCustomer().getName()+" sell "+type+" success");
技术分享        return null;
技术分享    }
技术分享    public String sellSomething(String type) {
技术分享         System.out.println(this.getCustomer().getName()+" sell "+type+" success");
技术分享           return null;
技术分享    }
技术分享
技术分享}
技术分享

 

 业务实现类B,作为配置文件中的sellBean:

 

技术分享package AutoProxyOne;
技术分享
技术分享public class ShoppingImplB implements Shopping {
技术分享    private Customer customer;
技术分享    public Customer getCustomer() {
技术分享        return customer;
技术分享    }
技术分享    public void setCustomer(Customer customer) {
技术分享        this.customer = customer;
技术分享    }
技术分享    public String buySomething(String type) {
技术分享        System.out.println(this.getCustomer().getName()+" bye "+type+" success");
技术分享        return null;
技术分享    }
技术分享    
技术分享    public String buyAnything(String type) {
技术分享       System.out.println(this.getCustomer().getName()+" bye "+type+" success");
技术分享       return null;
技术分享
技术分享     }
技术分享    public String sellAnything(String type) {
技术分享        System.out.println(this.getCustomer().getName()+" sell "+type+" success");
技术分享        return null;
技术分享    }
技术分享    public String sellSomething(String type) {
技术分享         System.out.println(this.getCustomer().getName()+" sell "+type+" success");
技术分享           return null;
技术分享    }
技术分享
技术分享}
技术分享

 

切面通知:

 

技术分享package AutoProxyOne;
技术分享
技术分享import java.lang.reflect.Method;
技术分享
技术分享import org.springframework.aop.MethodBeforeAdvice;
技术分享//前置通知
技术分享public class WelcomeAdvice implements MethodBeforeAdvice {
技术分享
技术分享    public void before(Method method, Object[] args, Object obj)
技术分享            throws Throwable {
技术分享        
技术分享        System.out.println("Hello welcome to bye ");
技术分享
技术分享    }
技术分享
技术分享}
技术分享

 

配置文件:

其中beanNames为buy*,意味着所有以buy开头的bean,都被spring容易自动代理,执行相应的切面通知

 

技术分享<?xml version="1.0" encoding="UTF-8"?>
技术分享<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
技术分享<beans>
技术分享 <bean id="WelcomeAdvice" class="AutoProxyOne.WelcomeAdvice">
技术分享 </bean>
技术分享 
技术分享 <bean  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
技术分享   <property name="beanNames">
技术分享     <list>
技术分享       <value>buy*</value>
技术分享     </list>
技术分享   </property>
技术分享   <property name="interceptorNames">
技术分享     <list>
技术分享        <value>WelcomeAdvice</value>
技术分享     </list> 
技术分享   </property>
技术分享
技术分享 </bean>
技术分享   
技术分享  <bean id="buyBean" class="AutoProxyOne.ShoppingImplA">
技术分享    <property name="customer">
技术分享      <ref bean="customer"/>
技术分享    </property>
技术分享   </bean>
技术分享 <bean id="sellBean" class="AutoProxyOne.ShoppingImplB">
技术分享    <property name="customer">
技术分享      <ref bean="customer"/>
技术分享    </property>
技术分享   </bean>
技术分享
技术分享
技术分享<bean id="customer" class="AutoProxyOne.Customer">
技术分享   <constructor-arg index="0">
技术分享     <value>gaoxiang</value>
技术分享   </constructor-arg>
技术分享    <constructor-arg index="1">
技术分享     <value>26</value>
技术分享   </constructor-arg>
技术分享 </bean>
技术分享
技术分享
技术分享</beans>
技术分享
技术分享

 

测试代码:

在测试代码中,我们的buyBean打印两条买的信息,sellBean打印两条卖的信息,可以看到buyBean执行的方法已经进行了切面处理

需要注意的是,如果使用自动代码,则获得Spring Bean工厂要用

ApplicationContext ctx=new FileSystemXmlApplicationContext(filePath);

而不能用

BeanFactory factory=new XmlBeanFactory(new FileSystemResource(filePath));

原因我想是因为BeanFactory在初始化时并不实例化单例的Bean,而ApplicationContext则在初始化时候全部实例化了Bean,自动代理需要在初始化时候定义好代理关系

 

技术分享package AutoProxyOne;
技术分享
技术分享import java.io.File;
技术分享
技术分享import org.springframework.beans.factory.BeanFactory;
技术分享import org.springframework.beans.factory.xml.XmlBeanFactory;
技术分享import org.springframework.context.ApplicationContext;
技术分享import org.springframework.context.support.FileSystemXmlApplicationContext;
技术分享import org.springframework.core.io.FileSystemResource;
技术分享
技术分享
技术分享import org.springframework.aop.support.RegexpMethodPointcutAdvisor;
技术分享public class TestAdvisor {
技术分享
技术分享    public static void main(String[] args) {
技术分享
技术分享        String filePath=System.getProperty("user.dir")+File.separator+"AutoProxyOne"+File.separator+"hello.xml";
技术分享        
技术分享        BeanFactory factory=new XmlBeanFactory(new FileSystemResource(filePath));
技术分享        ApplicationContext ctx=new FileSystemXmlA

使用BeanNameAutoProxyCreator实现spring的自动代理

标签:

原文地址:http://www.cnblogs.com/langtianya/p/5138125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!