使用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 ...
随机推荐
- PHP递归生成树形数组
数据表结构 id name pid ){ foreach($data as $row){ if($row['pid']==$p_id){ $tmp = $this->tree( ...
- linux定时器(crontab)实例
linux实验示例----实现每2分钟将“/etc”下面的文件打包存储到“/usr/lobal”目录下 ·Step1:编辑当前用户的crontab并保存终端输入:>crontab -u root ...
- Value和Object的区别
在使用NSMutableDictionary的时候经常会使用setValue forKey与setObject forKey,他们经常是可以交互使用的,代码中经常每一种的使用都有. 1,先看看setV ...
- MWeb
专业的 Markdown 写作支持 极简 UI.Dark Mode.漂亮的 Markdown 语法高亮.列表缩进优化,提供 5 种主题选择. 除了支持基本的 Markdown 语法外,还支持大量 Ma ...
- bzoj 1101 zap
gcd(x,y)=d-->gcd(x/d,y/d)=1. 即求Σ(i<=n/d)Σ(j<=m/d) e(gcd(i,j)) 因为e=miu×1,可以卷积. 因为多组询问,需要sqrt ...
- 【BZOJ-1952】城市规划 [坑题] 仙人掌DP + 最大点权独立集(改)
1952: [Sdoi2010]城市规划 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 73 Solved: 23[Submit][Status][ ...
- 【BZOJ-2460&3105】元素&新Nim游戏 动态维护线性基 + 贪心
3105: [cqoi2013]新Nim游戏 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 839 Solved: 490[Submit][Stat ...
- C语言之捕捉信号
我们有时候需要在程序中做一些对于用户或内核发出的信号后的处理,如写回文件等善后处理的事情,或者直接忽略信号(当你按Ctrl+C时我压根不理你).下面是一段信号处理的代码(POSIX C): int c ...
- bzoj1535[POI2005]sza-template
此题解无病呻吟,啰里啰嗦,现已加入零分作文全家桶 这题......坑死我了...... 不妨记原串长为i的前缀为prefix(i),next[i]表示prefix(i)的最长公共前后缀长度(不等于pr ...
- [SVN Mac自带SVN结合新浪SAE进行代码管理]
前一篇我转载了别人SVN的使用方法,前面的配置和服务器我不是很明白,自己尝试后发现我需要使用到的核心命令是下面一些. 新浪SAE提供了SVN代码管理仓库,只要进入相应应用,然后点击左侧代码管理,到最下 ...