In last Spring AOP examples – advice, pointcut and advisor, you have to manually create a proxy bean (ProxyFactoryBean) for each beans whose need AOP support.

This is not an efficient way, for example, if you want all of your DAO classes in customer module to implement the AOP feature with SQL logging support (advise), then you have to create many proxy factory beans manually, as a result you just flood your bean configuration file with tons of proxy beans.

Fortunately, Spring comes with two auto proxy creators to create proxies for your beans automatically.

1. BeanNameAutoProxyCreator example

Before that, you have to create a proxy bean (ProxyFactoryBean) manually.

<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.mkyong.customer.services.CustomerService">
<property name="name" value="Yong Mook Kim" />
<property name="url" value="http://www.mkyong.com" />
</bean> <bean id="hijackAroundMethodBeanAdvice" class="com.mkyong.aop.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="hijackAroundMethodBeanAdvice" />
</bean>
</beans>

And get the bean with the bean with proxy name “customerServiceProxy”.

	CustomerService cust = (CustomerService)appContext.getBean("customerServiceProxy");

In auto proxy mechanism, you just need to create a BeanNameAutoProxyCreator, and include all your beans (via bean name, or regular expression name) and ‘advisor’ into a single unit.

<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.mkyong.customer.services.CustomerService">
<property name="name" value="Yong Mook Kim" />
<property name="url" value="http://www.mkyong.com" />
</bean> <bean id="hijackAroundMethodBeanAdvice" class="com.mkyong.aop.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.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>customerAdvisor</value>
</list>
</property>
</bean>
</beans>

Now, you can get the bean via original name “customerService”, you dun even know this bean has been proxies.

	CustomerService cust = (CustomerService)appContext.getBean("customerService");

2. DefaultAdvisorAutoProxyCreator example

This DefaultAdvisorAutoProxyCreator is extremely powerful, if any of the beans is matched by an advisor, Spring will create a proxy for it automatically.

<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.mkyong.customer.services.CustomerService">
<property name="name" value="Yong Mook Kim" />
<property name="url" value="http://www.mkyong.com" />
</bean> <bean id="hijackAroundMethodBeanAdvice" class="com.mkyong.aop.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>

This is just over power, since you don’t have control what bean should be proxy, what you can do is just trust Spring will do the best for you. Please take good care if you want to implement this into your project.

Spring Auto proxy creator example的更多相关文章

  1. Spring Auto scanning components

    Normally you declare all the beans or components in XML bean configuration file, so that Spring cont ...

  2. Spring学习(十四)----- Spring Auto Scanning Components —— 自动扫描组件

    一.      Spring Auto Scanning Components —— 自动扫描组件 1.      Declares Components Manually——手动配置componen ...

  3. Spring AOP 的proxy详解

    spring 提供了多种不同的方案实现对 bean 的 aop proxy, 包括 ProxyFactoryBean, 便利的 TransactionProxyFactoryBean 以及 AutoP ...

  4. spring 源码分析之BeanPostProcessor

    1.官方解答: Factory hook that allows for custom modification of new bean instances, e.g. checking for ma ...

  5. Spring AOP实现声明式事务代码分析

    众所周知,Spring的声明式事务是利用AOP手段实现的,所谓"深入一点,你会更快乐",本文试图给出相关代码分析. AOP联盟为增强定义了org.aopalliance.aop.A ...

  6. Spring源码情操陶冶-AOP之ConfigBeanDefinitionParser解析器

    aop-Aspect Oriented Programming,面向切面编程.根据百度百科的解释,其通过预编译方式和运行期动态代理实现程序功能的一种技术.主要目的是为了程序间的解耦,常用于日志记录.事 ...

  7. spring事务详解(三)源码详解

    系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.引子 ...

  8. Spring基础系列-AOP源码分析

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9560803.html 一.概述 Spring的两大特性:IOC和AOP. AOP是面向切 ...

  9. Spring 3.1新特性之二:@Enable*注解的源码,spring源码分析之定时任务Scheduled注解

    分析SpringBoot的自动化配置原理的时候,可以观察下这些@Enable*注解的源码,可以发现所有的注解都有一个@Import注解.@Import注解是用来导入配置类的,这也就是说这些自动开启的实 ...

随机推荐

  1. linux下对符合条件的文件大小做汇总统计的简单命令

    (1)统计当前目录下的 *txt 文件du -c -h *txt   (2)统计当前目录下的 *txt 文件, 并求出总大小du  *txt |awk 'BEGIN{count=0;size=0;} ...

  2. 如何在Ubuntu上安装最新版本的Node.js

    apt-get update apt-get install -y python-software-properties software-properties-common add-apt-repo ...

  3. git branch

    使用git管理工具,branch 应该是我们接触最多的.不论我们是修复bug,还是做项目,都会新开branch,工作完成后再合并. 然而对一些初学者,对git的一些命令知之甚少,这里,给大家写一些常用 ...

  4. Hadoop集群(第7期)_Eclipse开发环境设置

    1.Hadoop开发环境简介 1.1 Hadoop集群简介 Java版本:jdk-6u31-linux-i586.bin Linux系统:CentOS6.0 Hadoop版本:hadoop-1.0.0 ...

  5. hdu 4970 Killing Monsters (思维 暴力)

    题目链接 题意: 有n座塔,每座塔的攻击范围为[l,r],攻击力为d,有k个怪兽从这些塔前面经过,第i只怪兽初始的生命力为hp,出现的位置为x,终点为第n个格子.问最后有多少只怪兽还活着. 分析: 这 ...

  6. Android百度地图

        帖子   热搜: 二维码 聊天 二维码扫描 传感器 游戏 定位 手势绘图 小项目 相框 绘图 涂鸦 拨打电话 记事本 定时器 通话记录 短信群发 listview 音乐播放器 项目例子 百度地 ...

  7. bzoj1185

    一遇到数学题和计算几何题我就要调半天…… 玛雅,我真是太弱了…… 基本思路很简单,先上凸包,然后矩形与凸包一边重合,然后旋转卡壳即可 然而我没怎么写过计算几何题,一开始写的各种囧,后来看了hzwer的 ...

  8. 使用Java VisualVM监控远程JVM

    我们经常需要对我们的开发的软件做各种测试, 软件对系统资源的使用情况更是不可少, 目前有多个监控工具, 相比JProfiler对系统资源尤其是内存的消耗是非常庞大,JDK1.6开始自带的VisualV ...

  9. 05day2

    05day1 没什么可说,一道模拟水题,两道裸的模板题.05day2 是几天以来最难的一次.   圆排列 动态规划 [问题描述] 有 N 个人顺时针围在一圆桌上开会,他们对身高很敏感. 因此决定想使得 ...

  10. JavaScript备忘录-闭包

    var arr = new Array(); function Person() { for (var i = 0; i < 10; i++) { //要记住,这个属性函数申明,只有立即执行才会 ...