提到代理,我们可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames实现,但如果需要代理的bean很多,无疑会对spring配置文件的编写带来繁重的工作
Spring为我们提供了,根据beanName匹配后进行自动代理的解决方法
业务接口
业务实现类A,作为配置文件中的buyBean:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package AutoProxyOne;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public class ShoppingImplA implements Shopping {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Customer customer;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public Customer getCustomer() {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return customer;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setCustomer(Customer customer) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.customer = customer;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String buySomething(String type) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(this.getCustomer().getName()+" bye "+type+" success");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String buyAnything(String type) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(this.getCustomer().getName()+" bye "+type+" success");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String sellAnything(String type) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(this.getCustomer().getName()+" sell "+type+" success");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String sellSomething(String type) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(this.getCustomer().getName()+" sell "+type+" success");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
业务实现类B,作为配置文件中的sellBean:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package AutoProxyOne;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public class ShoppingImplB implements Shopping {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Customer customer;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public Customer getCustomer() {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return customer;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setCustomer(Customer customer) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.customer = customer;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String buySomething(String type) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(this.getCustomer().getName()+" bye "+type+" success");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String buyAnything(String type) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(this.getCustomer().getName()+" bye "+type+" success");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String sellAnything(String type) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(this.getCustomer().getName()+" sell "+type+" success");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String sellSomething(String type) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(this.getCustomer().getName()+" sell "+type+" success");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
切面通知:
配置文件:
其中beanNames为buy*,意味着所有以buy开头的bean,都被spring容易自动代理,执行相应的切面通知
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="UTF-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<beans>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="WelcomeAdvice" class="AutoProxyOne.WelcomeAdvice">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="beanNames">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>buy*</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="interceptorNames">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>WelcomeAdvice</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</list>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="buyBean" class="AutoProxyOne.ShoppingImplA">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="customer">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="customer"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="sellBean" class="AutoProxyOne.ShoppingImplB">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="customer">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<ref bean="customer"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<bean id="customer" class="AutoProxyOne.Customer">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<constructor-arg index="0">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>gaoxiang</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</constructor-arg>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<constructor-arg index="1">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<value>26</value>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</constructor-arg>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</bean>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</beans>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
测试代码:
在测试代码中,我们的buyBean打印两条买的信息,sellBean打印两条卖的信息,可以看到buyBean执行的方法已经进行了切面处理
需要注意的是,如果使用自动代码,则获得Spring Bean工厂要用
ApplicationContext ctx=new FileSystemXmlApplicationContext(filePath);
而不能用
BeanFactory factory=new XmlBeanFactory(new FileSystemResource(filePath));
原因我想是因为BeanFactory在初始化时并不实例化单例的Bean,而ApplicationContext则在初始化时候全部实例化了Bean,自动代理需要在初始化时候定义好代理关系