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的更多相关文章

  1. Spring框架系列(11) - Spring AOP实现原理详解之Cglib代理实现

    我们在前文中已经介绍了SpringAOP的切面实现和创建动态代理的过程,那么动态代理是如何工作的呢?本文主要介绍Cglib动态代理的案例和SpringAOP实现的原理.@pdai Spring框架系列 ...

  2. Spring AOP 自动创建代理

        Spring为我们提供了自动代理机制,让容器为我们自动生成代理,把我们从烦琐的配置工作中解放出来,在内部,Spring 使用BeanPostProcessor自动地完成这项工作.   1.实现 ...

  3. Spring学习笔记:自动创建Proxy

    为什么需要自动创建Proxy 手动为所有需要代理的类用ProxyFactoryBean创建代理Proxy需要大量的配置. 这样如果需要代理的类很多,配置就很繁琐,而且也不便于xml配置的维护. 因此S ...

  4. 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  5. Spring框架系列(10) - Spring AOP实现原理详解之AOP代理的创建

    上文我们介绍了Spring AOP原理解析的切面实现过程(将切面类的所有切面方法根据使用的注解生成对应Advice,并将Advice连同切入点匹配器和切面类等信息一并封装到Advisor).本文在此基 ...

  6. Spring Aop(十四)——Aop自动创建代理对象的原理

    转发地址:https://www.iteye.com/blog/elim-2398725 Aop自动创建代理对象的原理 我们在使用Spring Aop时,通常Spring会自动为我们创建目标bean的 ...

  7. 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  8. Spring框架系列(12) - Spring AOP实现原理详解之JDK代理实现

    上文我们学习了SpringAOP Cglib动态代理的实现,本文主要是SpringAOP JDK动态代理的案例和实现部分.@pdai Spring框架系列(12) - Spring AOP实现原理详解 ...

  9. 死磕Spring之AOP篇 - Spring AOP自动代理(一)入口

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

随机推荐

  1. iOS手势(滑动)返回的实现(自定义返回按钮)

    如果各位使用的是storyboard布局的话,且用的是系统的返回按钮,那么是自动会有滑动返回效果的,但是相信各位做项目的,一般都是用的自定义的返回按钮,所以我贴几行代码,看看怎么实现系统自带的滑动返回 ...

  2. PHP-Beast V0.6 发布 (PHP源码加密模块)

    本版本主要修改了一些bug和增加了一些配置项: 1. 设置缓存大小可以使用单位, 例如: beast.cache_size = 10m; 2. 可以在配置文件中禁止beast模块, 例如: beast ...

  3. [转]js动态获取图片长宽尺寸

    http://blog.phpdr.net/js-get-image-size.html lightbox类效果为了让图片居中显示而使用预加载,需要等待完全加载完毕才能显示,体验不佳(如filick相 ...

  4. 高德地图iOS SDK限制地图的缩放比例

    问题 高德地图的iOS SDK 3D版中(v2.4.0), 显示范围在560m左右时建筑会呈现3D效果. 我们有没有办法可以限制地图最小缩放到这个比例, 从而保证建筑始终使用3D效果显示呢? 探索 高 ...

  5. VS2015详细安装步骤

    亲身经历记录下来,以备后用.也希望能够帮助到有需要的朋友们! 1.安装之前首先下载VS2015,下载地址: [VS2015社区版官方中文版下载]:http://download.microsoft.c ...

  6. 支持事件穿透?使用pointer-events样式

    使用绝对定位元素,让元素A完全盖住元素B时,如何通过元素A来响应元素B的事件呢? 上图可以用下面的SVG代码来实现: <svg width="200" height=&quo ...

  7. H5常用代码:适配方案1

    在工作中接到H5项目,第一件想到的事就应该是屏幕适配问题,解决了屏幕适配,接下来的事才能真正开始.从此篇博客开始会连续记录下我经常用到的一些适配方案. 对于传统的PC项目,直接在移动端打开也都是会以视 ...

  8. Kafka重复消费和丢失数据研究

    Kafka重复消费原因 底层根本原因:已经消费了数据,但是offset没提交. 原因1:强行kill线程,导致消费后的数据,offset没有提交. 原因2:设置offset为自动提交,关闭kafka时 ...

  9. atitit.web 推送实现方案集合(2)---百度云,jpush 极光推送 ,个推的选型比较.o99

    atitit.web 推送实现方案集合(2)---百度云,jpush 极光推送 ,个推的选型比较.o99 1.1. 云推送有推送次数或频率的限制吗? 1 1.2. 推送的消息长度 1 1.3. 离线消 ...

  10. Leetcode 38 Count and Say 传说中的递推

    class Solution { public: vector<string> vs_; Solution(){ "); vs_.push_back(t); ; i< ;+ ...