提到代理,我们可以使用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
分享到:
相关推荐
Spring实现自动代理Demo,BeanNameAutoProxyCreator的运用,学习参考原理
NULL 博文链接:https://tomboxfan.iteye.com/blog/350398
2.3.2 混合使用自动和手动装配 2.3.3 缺省自动装配 2.3.4 何时采用自动装配 2.4 使用Spring的特殊Bean 2.4.1 对Bean进行后处理 2.4.2 对Bean工厂进行后处理 2.4.3 分散配置 2.4.4 ...
7.9. 使用“自动代理(autoproxy)”功能 7.9.1. 自动代理bean定义 7.9.1.1. BeanNameAutoProxyCreator 7.9.1.2. DefaultAdvisorAutoProxyCreator 7.9.1.3. AbstractAdvisorAutoProxyCreator 7.9.2. 使用元数据驱动...
6.5 自动创建代理 6.5.1 实现类介绍 6.5.2 BeanNameAutoProxyCreator 6.5.3 DefaultAdvisorAutoProxyCreator 6.6 小结 第7章 基于@AspectJ和Schema的AOP 7.1 Spring对AOP的支持 7.2 JDK 5.0注解知识快速进阶 7.2.1 ...
6.5 自动创建代理 6.5.1 实现类介绍 6.5.2 BeanNameAutoProxyCreator 6.5.3 DefaultAdvisorAutoProxyCreator 6.6 小结 第7章 基于@AspectJ和Schema的AOP 7.1 Spring对AOP的支持 7.2 JDK 5.0注解知识快速进阶 7.2.1 ...
IntroductionInterceptor DelegatingIntroductionInterceptor Autoproxing 自动代理可以让您不用为每一个要被 Advised 的 Target 手动定义代理物件,透过 Bean 名称或是 Pointcut 的比对,自动为...
--定义DAO Bean ,由于BeanNameAutoProxyCreator自动生成事务代理--> class="com.service.impl.UserManagerImpl"> singleton="false"> </beans>
本程序是struts spring hibernate的完整环境配置, 包括: struts1.3 spring 2.0 hibernate 3.1 的... BeanNameAutoProxyCreator事务处理 中文乱码解决 MD5程序加密 AJAX验证码等 希望大家喜欢
Spring的Ioc Spring的AOP , AspectJ Spring的事务管理 , 三大框架的整合 目录 1.1 Spring 框架学习路线:..........................................................................................................
本程序是struts spring hibernate的完整环境配置, 包括: struts1.3 spring 2.0 hibernate ... BeanNameAutoProxyCreator事务处理 中文乱码解决 MD5程序加密 AJAX验证码等 希望大家喜欢 4/4
本程序是struts spring hibernate的完整环境配置, 包括: struts1.3 spring 2.0 hibernate ... BeanNameAutoProxyCreator事务处理 中文乱码解决 MD5程序加密 AJAX验证码等 希望大家喜欢 3/4
nested exception is java.lang.AbstractMethodError: org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator.postProcessAfterInstantiation(Ljava/lang/Object;Ljava/lang/String;)Z at org....
BeanNameAutoProxyCreator BeanNameAware BeanNameUrlHandlerMapping BeanNameViewResolver BeanNotOfRequiredTypeException BeanPostProcessor BeanPropertyBindingResult BeanPropertySqlParameterSource ...
Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................
Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................