Spring Auto proxy creator example

时间:2022-08-28 13:29:17

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学习&lpar;十四&rpar;----- 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&period;1新特性之二:&commat;Enable&ast;注解的源码&comma;spring源码分析之定时任务Scheduled注解

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

随机推荐

  1. PHP 之 this self parent static 对比

    this 绑定的是当前已经实例化的对象 这样长出现的问题: 如果你在一个父类中使用$this调用当前一个类的方法或者属性,如果这个类被继承,且在相应的子类中有调用的属性或者方法是,则父类中的$this ...

  2. HDU 2825 Wireless Password&lpar;AC自动机&plus;DP&rpar;

    题目链接 做题, #include <cstdio> #include <string> #include <cstring> using namespace st ...

  3. Maven-006-手动部署第三方构件至 nexus 私服

    某些 Java 构件因许可证因素,无法公开的部署到公共仓库中:或者,一些小型的开源项目(例如 SourceForge.GitHub 中的一些项目),没有将构件分发到*仓库中,也没有维护自己的仓库,因 ...

  4. APICloud:轻松6步完成App软件开发

    现如今,谁不知道App绝对就是OUT,谁不用App简直没法过日子!但是说到App软件开发,不懂编程,不懂技术的人就一脸懵圈.在门外汉来看,App软件开发是一件非常困难的事情,然而APICloud却说, ...

  5. XMPP通讯开发-好友获取界面设计

    在XMPP通讯开发-服务器连接 中我们成功连接到服务器上面,然后进入到主界面,接下来就是获取好友列表,这里我们分段开发,首先就是界面的设计,这里仿照QQ好友界面,里面的数据先是用模拟的,下一章获取服务 ...

  6. Ruby中有意思的块

    块:是在调用方法时,能与参数一起传递的多个处理的集合 简单点说,跟在方法执行后面的do |变量| end就是一个块,这个块会被传入方法中去执行! 这个非常厉害,非常有意思! 在ruby中,如果需要便利 ...

  7. js如何实现重载

    所谓重载,就是一组相同的函数名,有不同个数的参数,在使用时调用一个函数名,传入不同参数,根据你的参数个数,来决定使用不同的函数!但是我们知道js中是没有重载的,因为后定义的函数会覆盖前面的同名函数,但 ...

  8. php自定义session存储路径

    1.找到php.ini配置文件,找到session.save_path,修改如下: 其中2表示session存储的目录深度,也就是分目录,避免一个目录下文件太多,造成IO负担. session.sav ...

  9. 批处理系列&lpar;13&rpar; -从视频导出高质量GIF图片

    需要ffmpeg,配置ffmpeg到环境变量. 保存代码到HQGIF.bat,与视频同目录,管理员权限运行CMDcd到此目标目录: HQGIF.bat input_video_name.mp4 out ...

  10. java学习笔记2--数据类型、数组

    本文地址:http://www.cnblogs.com/archimedes/p/java-study-note2.html,转载请注明源地址. 1.数据类型 Java数据类型有: 原始数据类型(Pr ...