[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. C# 错误代码

    附录B 错误CS0001 编译器内部错误 错误CS0003 内存溢出 错误CS0004 提升为错误的警告 错误CS0005 编译器选项后应跟正确的参数 错误CS0006 找不到动态链接的元数据文件 错 ...

  2. 【Toolkit】关闭Closeable的简单工具类

    Java中有很多连接类的类实现java.io.Closeable,而关闭资源是一项重复的劳动,写一个简单的工具类避免重复劳动. > JDK7.0中,哪些类实现java.io.Closeable?

  3. Jetty和Tomcat的选择:按场景而定

    Jetty和Tomcat的选择:按场景而定 Jetty和Tomcat为目前全球范围内最著名的两款开源的webserver/servlet容器.由于它们的实现都遵循Java Servlet规范,一个Ja ...

  4. XMPP Server

    XMPPFramework,编译失败,@import libxmlSimu后提示:Module 'libxmlSimu' not found XMPP协议实现原理介绍 XMPP协议学习笔记 四.地址格 ...

  5. Systematic LncRNA Classification

    Systematic LncRNA Classification From: http://www.arraystar.com/Services/Services_main.asp?ID=307 An ...

  6. Metasploit中使用Nessus插件命令

    基本命令 导入扫描结果 db_import  /路径/文件.nessus 查看数据库里面现有的IP信息 msf > db_hosts -c address,svcs,vulns(注:vulns是 ...

  7. 12.Generics

    benifit: 1.make developers extremely productive is code reuse, which is the ability to derive a clas ...

  8. CUBRID学习笔记 12防火墙设置 linux

    这玩意是linux上用的. 如果你的数据库不是装在linux下可以飘过了 iptables -I INPUT -p tcp --dport 8001 -j ACCEPT iptables -I INP ...

  9. 随机步法A-Z

    程序是生成一个10X10的字符数组,初始化时全为 ‘.’  的随机步法.程序必须随机的从一个元素 ‘走到’ 另一个元素,每次只向上.向下.向左或向右移动一个元素位置.已访问过的元素按访问顺序用字母A到 ...

  10. Facebook内部分享:25个高效工作的小技巧

    Facebook内部分享:25个高效工作的小技巧 Facebook 内部分享:不论你如何富有,你都赚不到更多的时间,你也回不到过去.没有那么多的假如,只有指针滴答的时光飞逝和你应该好好把握的现在,以下 ...