使用BeanNameAutoProxyCreator实现spring的自动代理
提到代理,我们可以使用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的自动代理的更多相关文章
- 死磕Spring之AOP篇 - Spring AOP自动代理(一)入口
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- 使用注解配置Spring框架自动代理通知
话不多说上代码 项目架构图及Lib包如下: 第二步创建业务类接口 package cn.happy.day01.entity; /** * 1.业务接口 * @author Happy * */ pu ...
- spring aop自动代理xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- spring aop自动代理注解配置之二
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- spring aop自动代理注解配置之一
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- SSM-Spring-15:Spring中名称自动代理生成器BeanNameAutoProxyCreator
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 名称自动代理生成器:BeanNameAutoProxyCreator 为了更好的测试,我放了俩个接口,俩个实现 ...
- Spring AOP使用整理:自动代理以及AOP命令空间
三.自动代理的实现 1.使用BeanNameAutoProxyCreator 通过Bean的name属性自动生成代理Bean. <bean class="org.springframe ...
随机推荐
- Hadoop-2.2.0 (传 hadoop-2.2.0.tar.gz)
配置hadoop 2.1 上传hadoop包 2.2 解压hadoop包 首先在根目录下创建一个cloud目录 mkdir /cloud tar -zxvf hadoop-2.2.0.tar.gz - ...
- [日常训练]常州集训day7
T1 Description 给定一个序列,初始为空.依次将$1-n$插入序列,其中$i$插到当前第$a_i$个数的右边($a_i=0$表示插到序列最左边).求最终序列. Input 第一行一个整数$ ...
- hadoop报错:WARN mapred.JobClient: Error reading task outputNo route to host
解决方案: /etc/sysconfig/network/etc/hosts$hostname 这三处的主机名都要一样. 具体参考:http://blog.itpub.net/28254374/vie ...
- python图形界面(GUI)设计
不要问我为什么要用 python 来做这种事,我回到“高兴咋地”也不是不可以,总之好奇有没有好的解决方案.逛了一圈下来,总体上来说,python 图形界面有以下几个可行度比较高的解决方案. 1. py ...
- C#把数据写到硬盘指定位置
FileStream fs; StreamWriter sw;string RootPath = @"C:\Advantech" + "\\tempData\\" ...
- Java 中常用缓存Cache机制的实现
所谓缓存,就是将程序或系统经常要调用的对象存在内存中,一遍其使用时可以快速调用,不必再去创建新的重复的实例.这样做可以减少系统开销,提高系统效率. 所谓缓存,就是将程序或系统经常要调用的对象存在内存中 ...
- git命令笔记
git -bare init git remote add origin ssh://myserver.com/home/git/myapp.git git remote show origin [r ...
- Scala的sealed关键字
Scala的sealed关键字 缘起 今天在学习Akka的监控策咯过程中看到了下面一段代码: def supervisorStrategy(): SupervisorStrategy = OneFor ...
- IDLE快捷键
Ctrl + Space 完成类.方法.变量名称的自动输入,这个快捷键是我最经常使用的快捷键了,它可以完成类.方法.变量名称的自动录入,很方便.(不过在我的电脑上和输入法冲突) Ctrl + N 快 ...
- 配置ASP.NET Web应用程序, 使之运行在medium trust
这文章会向你展示, 怎么配置ASP.NET Web应用程序, 使之运行在medium trust. 如果你的服务器有多个应用程序, 你可以使用code access security和medium ...