Spring3系列11- Spring AOP——自动创建Proxy
Spring3系列11- Spring AOP——自动创建Proxy
在《Spring3系列9- Spring AOP——Advice》和《Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法》中的例子中,在配置文件中,你必须手动为每一个需要AOP的bean创建Proxy bean(ProxyFactoryBean)。
这不是一个好的体验,例如,你想让DAO层的所有bean都支持AOP,以便写SQL日志,那么你必须手工创建很多的ProxyFactoryBean,这样会直接导致你的xml配置文件内容成几何级的倍增,不利于xml配置维护。
幸运的是,Spring有两种方法,可以为你自动创建proxy。
1. 利用BeanNameAutoProxyCreator自动创建proxy
手工创建ProxyFactoryBean如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class=" com.lei.demo.aop.advice.CustomerService">
<property name="name" value="LeiOOLei" />
<property name="url" value="http://www.cnblogs.com/leiOOlei/" />
</bean> <bean id="hijackAroundMethodBean" class=" com.lei.demo.aop.advice.HijackAroundMethod" /> <bean id="customerServiceProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService" />
<property name="interceptorNames">
<list>
<value>customerAdvisor</value>
</list>
</property>
</bean> <bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedName" value="printName" />
<property name="advice" ref=" hijackAroundMethodBean " />
</bean>
</beans>
配置完后要得到customerServiceProxy,需要如下代码
CustomerService cust = (CustomerService) appContext.getBean("customerServiceProxy");
在自动模式中,你需要创建BeanNameAutoProxyCreator,将所有的bean(通过名字或正则表达式匹配)和advisor形成一个独立的单元,配置如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService">
<property name="name" value="LeiOOLei" />
<property name="url" value="http://www.cnblogs.com/leiOOlei/" />
</bean> <bean id="hijackAroundMethodBeanAdvice" class=" com.lei.demo.aop.advice.HijackAroundMethod" /> <bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>customerAdvisor</value>
</list>
</property>
</bean> <bean id="customerAdvisor"
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedName" value="printName" />
<property name="advice" ref="hijackAroundMethodBeanAdvice" />
</bean> </beans>
以上配置中只要bean的id符合*Service,就会自动创建proxy,所以,你可以用以下代码获得proxy。
CustomerService cust = (CustomerService) appContext.getBean("customerService");
2. 利用DefaultAdvisorAutoProxyCreator创建Proxy
这种方式利用DefaultAdvisorAutoProxyCreator实现自动创建Proxy,此种方式威力巨大,任何匹配Advisor的bean,都会自动创建Proxy实现AOP,所以慎用。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerService" class="com.lei.demo.aop.advice.CustomerService">
<property name="name" value="LeiOOLei" />
<property name="url" value="http://www.cnblogs.com/leiOOlei/" />
</bean> <bean id="hijackAroundMethodBeanAdvice" class="com.lei.demo.aop.advice.HijackAroundMethod" /> <bean id="customerAdvisor"
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedName" value="printName" />
<property name="advice" ref="hijackAroundMethodBeanAdvice" />
</bean> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /> </beans>
以上例子中,xml中任何bean,只要有method名字为printName,使用以下代码时,都会自动创建Proxy,来支持AOP。
CustomerService cust = (CustomerService) appContext.getBean("customerService");
Spring3系列11- Spring AOP——自动创建Proxy的更多相关文章
- Spring框架系列(11) - Spring AOP实现原理详解之Cglib代理实现
我们在前文中已经介绍了SpringAOP的切面实现和创建动态代理的过程,那么动态代理是如何工作的呢?本文主要介绍Cglib动态代理的案例和SpringAOP实现的原理.@pdai Spring框架系列 ...
- Spring AOP 自动创建代理
Spring为我们提供了自动代理机制,让容器为我们自动生成代理,把我们从烦琐的配置工作中解放出来,在内部,Spring 使用BeanPostProcessor自动地完成这项工作. 1.实现 ...
- Spring学习笔记:自动创建Proxy
为什么需要自动创建Proxy 手动为所有需要代理的类用ProxyFactoryBean创建代理Proxy需要大量的配置. 这样如果需要代理的类很多,配置就很繁琐,而且也不便于xml配置的维护. 因此S ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- Spring框架系列(10) - Spring AOP实现原理详解之AOP代理的创建
上文我们介绍了Spring AOP原理解析的切面实现过程(将切面类的所有切面方法根据使用的注解生成对应Advice,并将Advice连同切入点匹配器和切面类等信息一并封装到Advisor).本文在此基 ...
- Spring Aop(十四)——Aop自动创建代理对象的原理
转发地址:https://www.iteye.com/blog/elim-2398725 Aop自动创建代理对象的原理 我们在使用Spring Aop时,通常Spring会自动为我们创建目标bean的 ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- Spring框架系列(12) - Spring AOP实现原理详解之JDK代理实现
上文我们学习了SpringAOP Cglib动态代理的实现,本文主要是SpringAOP JDK动态代理的案例和实现部分.@pdai Spring框架系列(12) - Spring AOP实现原理详解 ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(一)入口
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
随机推荐
- Kali Linux Web 渗透测试— 第十二课-websploit
Kali Linux Web 渗透测试— 第十二课-websploit 文/玄魂 目录 Kali Linux Web 渗透测试— 第十二课-websploit..................... ...
- [ACM_水题] ZOJ 3714 [Java Beans 环中连续m个数最大值]
There are N little kids sitting in a circle, each of them are carrying some java beans in their hand ...
- CSS3与页面布局学习总结
目录 一.BFC与IFC 1.1.BFC与IFC概要 1.2.如何产生BFC 1.3.BFC的作用与特点 二.定位 2.2.relative 2.3.absolute 2.4.fixed 2.5.z- ...
- mobilebone.js使用笔记
mobilebone.js主要用来是网页呈现单页效果,添加类似native app的页面切换效果.原理是:当打开a链接里的页面时,不再以传统的新页面打开,而是以ajax-html的方式,将新页面的内容 ...
- paip.java win程序迁移linux的最佳实践
paip.java win程序迁移linux的最佳实践 1.class load路径的问题... windows哈第一的从calsses目录加载,,而linux优先从jar加载.. 特别的是修理了ja ...
- OpenCV Linux 安装 Make出错
执行完CMake之后再执行make时遇到以下错误 [ %] Generating precomp.hpp.gch/opencv_core_Release.gch In file included /b ...
- C# winform的WebBrowser非常规编程(强烈推荐)
本文章被今日头条推荐 1.在WebBrowser中实现抓取301和302协议 在WebBrowser中抓取301和302协议目前官方提供的组件远远不够,需要借助HttpMonitor.dll.这个组件 ...
- 使用MFC提供的Http类下载和上传文件
1.下载文件 Download(const CString& strFileURLInServer, //待下载文件的URL const CString & strFileLocalF ...
- GO語言視頻教程下載
需要的朋友可以加QQ群195112,在群共享內可以下載到.
- 阿里云产品介绍(三):云数据库RDS
写完云服务器ECS,本来想先写负载均衡的. 因为发现很多客户,都是直接将单台云服务器应用对外提供访问,如果云服务器宕机,应用就会停止服务.云服务器标称有99.95%的可用率,一年下来宕机四个多小时也是 ...