Spring学习(十七)----- Spring自动代理创建者
1. BeanNameAutoProxyCreator示例
在此之前,必须手动创建一个代理bean(ProxyFactryBean)。
<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.yiibai.customer.services.CustomerService">
<property name="name" value="Yiibai Mook Kim" />
<property name="url" value="http://www.yiibai.com" />
</bean> <bean id="hijackAroundMethodBeanAdvice" class="com.yiibai.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.NameMatchMethodYiibaicutAdvisor">
<property name="mappedName" value="printName" />
<property name="advice" ref="hijackAroundMethodBeanAdvice" />
</bean>
</beans>
使用代理名称“customerServiceProxy”来获得 bean。
CustomerService cust = (CustomerService)appContext.getBean("customerServiceProxy");
在自动代理机制,只需要创建一个的 BeanNameAutoProxyCreator,并包含所有你的bean(通过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.yiibai.customer.services.CustomerService">
<property name="name" value="Yiibai Mook Kim" />
<property name="url" value="http://www.yiibai.com" />
</bean> <bean id="hijackAroundMethodBeanAdvice" class="com.yiibai.aop.HijackAroundMethod" /> <bean id="customerAdvisor"
class="org.springframework.aop.support.NameMatchMethodYiibaicutAdvisor">
<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>
现在,可以通过“CustomerService”的原始名称获取bean, 如果知道这个bean已经代理。
CustomerService cust = (CustomerService)appContext.getBean("customerService");
2. DefaultAdvisorAutoProxyCreator示例
这个 DefaultAdvisorAutoProxyCreator 是非常强大的,如果有 bean 相关连,Spring会自动创建一个代理。
<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.yiibai.customer.services.CustomerService">
<property name="name" value="Yiibai Mook Kim" />
<property name="url" value="http://www.yiibai.com" />
</bean> <bean id="hijackAroundMethodBeanAdvice" class="com.yiibai.aop.HijackAroundMethod" /> <bean id="customerAdvisor"
class="org.springframework.aop.support.NameMatchMethodYiibaicutAdvisor">
<property name="mappedName" value="printName" />
<property name="advice" ref="hijackAroundMethodBeanAdvice" />
</bean> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /> </beans>
不用管使用什么代理方法, Spring 都会有最适合处理方式。
下载代码 – http://pan.baidu.com/s/1pKdqtjt
Spring学习(十七)----- Spring自动代理创建者的更多相关文章
- Spring学习(六)-----Spring使用@Autowired注解自动装配
Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...
- Spring只定义接口自动代理接口实现类
能够扫描到包 @ComponentScan("org.zxp.esclientrhl") ESCRegistrar类主要实现ImportBeanDefinitionRegistra ...
- Spring学习(十一)-----Spring使用@Required注解依赖检查
Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...
- Spring AOP使用整理:自动代理以及AOP命令空间
三.自动代理的实现 1.使用BeanNameAutoProxyCreator 通过Bean的name属性自动生成代理Bean. <bean class="org.springframe ...
- Spring学习笔记--spring+mybatis集成
前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...
- spring学习12 -Spring 框架模块以及面试常见问题注解等
以下为spring常见面试问题: 1.Spring 框架中都用到了哪些设计模式? Spring框架中使用到了大量的设计模式,下面列举了比较有代表性的: 代理模式—在AOP和remoting中被用的比较 ...
- spring学习(三) ———— spring事务操作
前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...
- Spring学习笔记—Spring之旅
1.Spring简介 Spring是一个开源框架,最早由Rod Johnson创建,并在<Expert One-on-One:J2EE Design and Development> ...
- Spring学习2—Spring容器
一.Spring容器接口关系 容器是Spring框架的核心,Spring容器就是一个巨大的工厂.Spring容器使用Ioc(控制反转(Inversion of Control )管理所有组成应用系统的 ...
随机推荐
- ZT 80-90年代港台300部电视剧 你看过多少?
80-90年代港台300部电视剧 你看过多少? [复制链接] 噗噗 738主题 18精华 万家金领 发消息 发表于 2010-4-27 09:01:02 |显示全部楼层 1.(珍珠传奇) ...
- U-Mail详解邮件营销优势及应用领域
最近频频有营销人员向U-Mail小编咨询:邮件营销到底有什么好处呢?与此同时,还有不少人对邮件营销存在一定的误解:邮件营销是不是只给潜在消费者发送邮件推广商品呢?其实邮件群发的应用面非常广泛,可不仅仅 ...
- HBase性能优化 Java Api
1. 使用“连接池” 如果每次和Hbase交互时都去新建连接的话,显然是低效率的,HBase也提供类连接池相关的API. 1.1. HTablePool 早期的API中使用它,但很不幸,现在它已经过时 ...
- iOS AOP框架Aspects实现原理
总结: Aspects 是对 类的继承结构isa.mataclass结构的调整和维护:相当于链表的节点插入和删除: 同时使用method Swizzling 对方法统一重定向: 同时使用类似代理的机制 ...
- SharePoint 改动passwordWeb Part部署方案
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u012025054/article/details/31773231 SharePoint 改动pa ...
- was集群下基于接口分布式架构和开发经验谈
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/luozhonghua2014/article/details/34084935 某b项目是我首 ...
- python 小技巧同时迭代多个列表,多变量同时运算
再来一个大家很熟悉的 dict={"a": "b","c": "d"}k,v=dict.items()print(k,v ...
- Google 历年笔试面试30题
1.正整数序列Q中的每个元素都至少能被正整数a和b中的一个整除,现给定a和b,需要计算出Q中的前几项,例如,当a=3,b=5,N=6时,序列为3,5,6,9,10,12(1).设计一个函数void g ...
- 1.1《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——运行终端
终端是个允许我们运行命令行的程序,运行命令前,先打开它.在MacOS系统上,可以使用macOS应用 Spotlight来打开终端窗口,Spotlight也有其他两种方式触发,一种是键入⌘␣(comma ...
- 洛谷P4245 【模板】MTT(任意模数NTT)
题目背景 模板题,无背景 题目描述 给定 22 个多项式 F(x), G(x)F(x),G(x) ,请求出 F(x) * G(x)F(x)∗G(x) . 系数对 pp 取模,且不保证 pp 可以分解成 ...