[Spring]AOP拦截-三种方式实现自动代理

这里的自动代理,我讲的是自动代理bean对象,其实就是在xml中让我们不用配置代理工厂,也就是不用配置class为org.springframework.aop.framework.ProxyFactoryBean的bean。

用Spring一个自动代理类DefaultAdvisorAutoProxyCreator:

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>

例如: 
原来不用自动代理的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> <!-- 代理前原对象 -->
<bean id="person" class="cn.hncu.xmlImpl.Person"></bean> <!-- 切面 = 切点+通知 -->
<bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<!-- 切点 -->
<property name="patterns">
<list>
<value>.*run.*</value>
</list>
</property>
<!-- 通知-由我们写,实际代理动作 -->
<property name="advice">
<bean id="advice" class="cn.hncu.xmlImpl.AroundAdvice"></bean>
</property>
</bean> <!-- 代理工厂 -->
<bean id="personProxied" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 放入原型对象 -->
<property name="target" ref="person"></property> <!-- 放入切面 -->
<property name="interceptorNames">
<list>
<value>advisor</value>
</list>
</property>
</bean>
</beans>

现在改用自动代理,如下配置:

<beans ...>
<!-- 代理前原对象 -->
<bean id="person" class="cn.hncu.xmlImpl.Person"></bean> <!-- 切面 = 切点+通知 -->
<bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<!-- 切点 -->
<property name="patterns">
<list>
<value>.*run.*</value>
</list>
</property>
<!-- 通知-由我们写,实际代理动作 -->
<property name="advice">
<bean id="advice" class="cn.hncu.xmlImpl.AroundAdvice"></bean>
</property>
</bean> <!-- 自动代理 -->
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
</beans>

测试方法

  @Test//自动代理
public void demo4(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/4.xml");
//我们直接在这里获取Person对象就可以了,因为在最开始xml文件newPerson对象后,Spring就已经帮我们代理了!
Person p =ctx.getBean(Person.class);
p.run();
p.say();
}

相对于前面,也就是把代理工厂部分换成自动代理了。

能源项目xml文件标签释义--DefaultAdvisorAutoProxyCreator的更多相关文章

  1. 能源项目xml文件标签释义--<context:component-scan>

    <context:component-scan base-package="com.xindatai.ibs" use-default-filters="false ...

  2. 能源项目xml文件标签释义--default-lazy-init

    1.spring的default-lazy-init参数 spring在启动的时候,会默认加载会默认加载整个对象实例图,从初始化ACTION配置.到 service配置到dao配置.乃至到数据库连接. ...

  3. 能源项目xml文件标签释义--CommonsMultipartResolver

    <!-- 文件上传表单的视图解析器 --><bean id="multipartResolver" class="org.springframework ...

  4. 能源项目xml文件标签释义--DataSource

    <bean id="dataSource1" class="org.apache.tomcat.jdbc.pool.DataSource" destroy ...

  5. 能源项目xml文件标签释义--<mvc:annotation-driven>

    <mvc:annotation-driven />的可选配置 <mvc:annotation-driven message-codes-resolver ="bean re ...

  6. 能源项目xml文件 -- app-context.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  7. 能源项目xml文件 -- springMVC-servlet.xml -- default-lazy-init

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...

  8. 能源项目xml文件 -- app-dubbo.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  9. 能源项目xml文件 -- app-datasource.xml

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

随机推荐

  1. EI表达式和JSTL

    JSP九大内置对象     WEB域内置对象(存值取值,传值数据用的)    setAttribute/getAttribute;    page  基本不用/ pageContext 只在本页面有效 ...

  2. IOS设计模式之一(MVC模式,单例模式)

    iOS 设计模式-你可能已经听说过这个词,但是你真正理解它意味着什么吗?虽然大多数的开发者可能都会认为设计模式是非常重要的,然而关于设计模式这一主题的文章却不多,并且有时候我们开发者在写代码的时候也不 ...

  3. SQL查看表锁定,死锁解锁

    select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sys.dm_tran ...

  4. sql语句查询出表里符合条件的第二条记录的方法

    创建用到的表的SQL CREATE TABLE [dbo].[emp_pay]( [employeeID] [int] NOT NULL, [base_pay] [money] NOT NULL, [ ...

  5. [SAP ABAP开发技术总结]字符串处理函数、正则表达式

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  6. 2.mybatis入门实例 连接数据库进行查询

    1.新建项目,添加mybatis和mysql的jar包 2.在mysql中新建表user[id,name,age] CREATE TABLE `users` ( `id` ) NOT NULL aut ...

  7. Spring的DI(Ioc) - 利用构造器注入

    1: 在给对象提供构造器 public class PersonServiceImpl implements PersonService { private PersonDao personDao; ...

  8. 将客户端将IE9强制为IE7

    有时候由于浏览器的问题我们在IE7中开发的东西需要在IE9中展示 但是会出现兼容性的问题. 那么我们可以同技巧将用户端的浏览器强行以IE7的文档模式展示我们的网页 下面是针对iis asp.net程序 ...

  9. E2 2014.07.01 更新日志

    增加功能   完善功能 电话报修单,添加可以发短信通知客户和技术员选项 商品历程分析,增加按商品分类条件统计 修件库,增加可以按维修商条件过滤,以方便查询某维修商的返修件 维修中,备件转销售时,自动读 ...

  10. Redis核心知识之—— 时延问题分析及应对、性能问题和解决方法【★★★★★】

    参考网址: Redis时延问题分析及应对:http://www.cnblogs.com/me115/p/5032177.html Redis常见的性能问题和解决方法:http://www.search ...