使用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 ...
随机推荐
- Android高工必备技能
转载:http://www.jianshu.com/p/d791bbede02c Step 1. 玩转RxJava 使用RxJava处理异步极其方便,各种操作符可以对数据做流水线式操作,再加上与Ret ...
- NuGet命令行工具和可视化工具
Nuget出了命令行工具之外,还有一个可视化工具,下载地址如下: 命令行(推荐官网下载,有中文提示!):https://dist.nuget.org/index.html 可视化(简单.方便.易用): ...
- [NOIP2012] 提高组 洛谷P1081 开车旅行
题目描述 小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的 城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i 的海拔高度为 Hi,城市 ...
- 移动端web app自适应布局探索与总结
要掌握的知识点: iphone6 屏幕尺寸为 375*667 (pt)也就是 网页 全屏显示时候 document.documentElement.clientWidth------可以理解为屏幕越大 ...
- Unity 插件制作笔记(持续更新)
示例: [MenuItem(Menu1/Menu2)] static void TestMenu1() { Debug.log("点击了菜单"); } 导航菜单添加 [MenuIt ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
- 【突发问题】昨天更新了OS X EI Capitan 出现了Cocoapods的 pod :command not found
然后我百度:http://www.jianshu.com/p/6ff1903c3f11 果真,我想想然后执行了作者说的第一步,删除本地Cocoapods文件,然后发现我执行不了接下来的几个步骤了.所以 ...
- STM8S VCAP
There is a specific pin called vcap in stm8s mcu. I recommend this pin connects to a 1uF capacitor w ...
- css居中那点事儿
css居中那点事儿 在css中对元素进行水平居中是非常简单的,然而使元素垂直居中就不是一件简单的事情了,多年以来,垂直居中已经成为了CSS领域的圣杯,因为它是极其常见的需求,但是在实践中却不是一件简单 ...
- css3实现小黄人
效果就像这样: 不废话,直接上代码! hrml代码: <!DOCTYPE html> <html> <head lang="zh"> <m ...