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>
XML

使用代理名称“customerServiceProxy”来获得 bean。

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

在自动代理机制,只需要创建一个的 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>
XML

现在,可以通过“CustomerService”的原始名称获取bean, 如果知道这个bean已经代理。

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

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>
XML

不用管使用什么代理方法, Spring 都会有最适合处理方式。

下载代码 – http://pan.baidu.com/s/1pKdqtjt

Spring学习(十七)----- Spring自动代理创建者的更多相关文章

  1. Spring学习(六)-----Spring使用@Autowired注解自动装配

    Spring使用@Autowired注解自动装配 在上一篇 Spring学习(三)-----Spring自动装配Beans示例中,它会匹配当前Spring容器任何bean的属性自动装配.在大多数情况下 ...

  2. Spring只定义接口自动代理接口实现类

    能够扫描到包 @ComponentScan("org.zxp.esclientrhl") ESCRegistrar类主要实现ImportBeanDefinitionRegistra ...

  3. Spring学习(十一)-----Spring使用@Required注解依赖检查

    Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...

  4. Spring AOP使用整理:自动代理以及AOP命令空间

    三.自动代理的实现 1.使用BeanNameAutoProxyCreator 通过Bean的name属性自动生成代理Bean. <bean class="org.springframe ...

  5. Spring学习笔记--spring+mybatis集成

    前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...

  6. spring学习12 -Spring 框架模块以及面试常见问题注解等

    以下为spring常见面试问题: 1.Spring 框架中都用到了哪些设计模式? Spring框架中使用到了大量的设计模式,下面列举了比较有代表性的: 代理模式—在AOP和remoting中被用的比较 ...

  7. spring学习(三) ———— spring事务操作

    前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...

  8. Spring学习笔记—Spring之旅

    1.Spring简介     Spring是一个开源框架,最早由Rod Johnson创建,并在<Expert One-on-One:J2EE Design and Development> ...

  9. Spring学习2—Spring容器

    一.Spring容器接口关系 容器是Spring框架的核心,Spring容器就是一个巨大的工厂.Spring容器使用Ioc(控制反转(Inversion of Control )管理所有组成应用系统的 ...

随机推荐

  1. Linux stat命令详解

    stat:查看文件或者文件系统的状态  -->可以查看时间等属性 stat常见命令参数 Usage: stat [OPTION]... FILE... Display file or file ...

  2. IntelliJ IDEA2017/2018 激活方法 破解补丁激活(亲测可用)(注册码方法以和谐)

    IntelliJ IDEA2017 激活方法(注册码方法以和谐): 搭建自己的授权服务器,对大佬来说也很简单,我作为菜鸟就不说了,网上有教程. 我主要说第二种,现在,直接写入注册码,是不能成功激活的( ...

  3. windows命令行大全

    命令简介 cmd是command的缩写.即命令行 . 虽然随着计算机产业的发展,Windows 操作系统的应用越来越广泛,DOS 面临着被淘汰的命运,但是因为它运行安全.稳定,有的用户还在使用,所以一 ...

  4. Oracle 数据库创建(图形界面操作)

    Oracle 创建数据库图文分解: 1. 选择所有程序->Oracle-OraDb11g_home1->Configuration and Migration Tools -> Da ...

  5. exits, in, left join性能比较

    exits  in left join性能比较 ,笔者使用的是MYSQL数据库,这三个关键字方法在其他的关系数据库里也是大同小异,如果各种有兴趣,自行比较. 我这里有一个249_account 表,总 ...

  6. 走向Node与Webpack 之路 - CommonJS 模块化

    走向Node与Webpack 之路 - CommonJS 模块化 1. 参考资料 JavaScript 标准参考教程(alpha) CommonJS规范(推荐 - 阮一峰写的) 官方网站 (看半天,不 ...

  7. objc_msgSend函数的实现

    毕竟汇编语言代码比较晦涩难懂,因此这里将函数的实现反汇编成C语言的伪代码: //下面的结构体中只列出objc_msgSend函数内部访问用到的那些数据结构和成员. /* 其实SEL类型就是一个字符串指 ...

  8. MySQL - FEDERATED引擎实现跨服务器查询

    1. MySQL插件的安装与卸载 # 查看插件信息 mysql> show plugins; mysql> select plugin_name,plugin_status,plugin_ ...

  9. linq to sql 分页技术

    昨天在用LINQ写分页的时候碰到一个很奇怪的问题:翻页的时候,有的数据会莫名其妙地消失,查了半个多小时才发现问题所在,其实是一个很细节的地方. 数据表如下: LINQ分页的实现是: var artic ...

  10. Sequelize-nodejs-11-Raw queries

    Raw queries原始查询 就是使用了原始的查询语句,如UPDATE users SET y = 42 WHERE x = 12 As there are often use cases in w ...