上一篇介绍了几种Advice(增强),并通过代码演示了生成代理的方式,下面来看通过配置文件配置方式把Advice织入目标类。

注意,配置文件方式仍然不是spring AOP的最好方式,学习配置方式也是为了循序渐进的掌握内核技术。

接口SmartCar

  1. public interface SmartCar {
  2. void lock(String userName);
  3. }

实现类MyCar

  1. public class MyCar implements SmartCar {
  2. @Override
  3. public void lock(String userName) {
  4. System.out.println(userName + "锁车");
  5. }
  6. }

定义了两个增强,一个在锁车方法前执行,提示检查窗户是否关闭,一个在锁车方法后执行,"哔哔"声提示锁车成功

  1. public class BeforeAdviceDemo implements MethodBeforeAdvice {
  2. @Override
  3. public void before(Method method, Object[] args, Object obj)
  4. throws Throwable {
  5. System.out.println("请检查窗户是否关好");
  6. }
  7. }
  1. public class AfterAdviceDemo implements AfterReturningAdvice {
  2. @Override
  3. public void afterReturning(Object returnObj, Method method, Object[] args,
  4. Object obj) throws Throwable {
  5. System.out.println("哔哔");
  6. }
  7. }

applicationContext.xml: 用配置文件来声明代理

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  8. <bean id="beforeAdvice" class="demo.aop.BeforeAdviceDemo" />
  9. <bean id="afterAdvice" class="demo.aop.AfterAdviceDemo" />
  10. <bean id="target" class="demo.aop.MyCar" />
  11. <bean id="myCar" class="org.springframework.aop.framework.ProxyFactoryBean"
  12. p:target-ref="target"
  13. p:proxyTargetClass="true">
  14. <property name="interceptorNames">
  15. <list>
  16. <idref local="beforeAdvice" />
  17. <idref local="afterAdvice" />
  18. </list>
  19. </property>
  20. </bean>
  21. </beans>

测试代码

  1. public static void main(String[] args) {
  2. ApplicationContext context = new ClassPathXmlApplicationContext("demo/aop/applicationContext.xml");
  3. SmartCar myCar = (SmartCar)context.getBean("myCar");
  4. myCar.lock("Tom");
  5. }

执行结果

  1. 请检查窗户是否关好
  2. Tom锁车
  3. 哔哔

配置文件中首先声明了3个bean,分别是增强的目标类和两个增强,没有特别的配置内容。重点是ProxyFactoryBean的配置,来看看它可配置的属性和用法。

target 目标对象,ref另一个bean
proxyTargetClass 是否对类进行代理(而不是对接口),true或false,设置为true时,使用CGLib代理技术
interceptorNames 织入目标对象的Advice(实现了Advisor或者MethodInterceptor接口的bean)
proxyInterfaces 代理所要实现的接口,如果指定了proxyTargetClass=true,此属性会被忽略
optimize 设置为true时,强制使用CGLib代理技术

下面再分别增加一个环绕增强和一个异常抛出增强。

先修改MyCar类,使它能够抛出异常,粗心的约翰会忘记锁车而抛出一个异常

  1. public class MyCar implements SmartCar {
  2. @Override
  3. public void lock(String userName) {
  4. if(userName.equals("Careless John")) {
  5. throw new RuntimeException(userName + "忘记锁车");
  6. } else {
  7. System.out.println(userName + "锁车");
  8. }
  9. }
  10. }

环绕增强类

  1. public class AroundAdviceDemo implements MethodInterceptor {
  2. @Override
  3. public Object invoke(MethodInvocation invocation) throws Throwable {
  4. Object[] args = invocation.getArguments();
  5. String name = (String)args[0];
  6. System.out.print("Hey, " + name);
  7. Object obj = invocation.proceed(); //调用目标方法
  8. System.out.println("Goodbye! " + name);
  9. return obj;
  10. }
  11. }

异常抛出增强

  1. public class ThrowsAdviceDemo implements ThrowsAdvice {
  2. public void afterThrowing(Method method, Object[] args, Object target, Exception ex) throws Throwable {
  3. System.out.println("捕获异常:" + ex.getMessage());
  4. System.out.println("(报警)滴滴滴滴滴滴滴滴滴滴滴滴");
  5. }
  6. }

applicationContext.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  8. <bean id="beforeAdvice" class="demo.aop.BeforeAdviceDemo" />
  9. <bean id="afterAdvice" class="demo.aop.AfterAdviceDemo" />
  10. <bean id="aroundAdvice" class="demo.aop.AroundAdviceDemo" />
  11. <bean id="throwsAdvice" class="demo.aop.ThrowsAdviceDemo" />
  12. <bean id="target" class="demo.aop.MyCar" />
  13. <bean id="myCar" class="org.springframework.aop.framework.ProxyFactoryBean"
  14. p:target-ref="target"
  15. p:proxyTargetClass="true">
  16. <property name="interceptorNames">
  17. <list>
  18. <idref local="aroundAdvice" />
  19. <idref local="throwsAdvice" />
  20. <idref local="beforeAdvice" />
  21. <idref local="afterAdvice" />
  22. </list>
  23. </property>
  24. </bean>
  25. </beans>

测试代码

  1. public static void main(String[] args) {
  2. ApplicationContext context = new ClassPathXmlApplicationContext("demo/aop/applicationContext.xml");
  3. SmartCar myCar = (SmartCar)context.getBean("myCar");
  4. myCar.lock("Tom");
  5. System.out.println("-------------------------");
  6. myCar.lock("Careless John");
  7. }

运行结果

  1. Hey, Tom请检查窗户是否关好
  2. Tom锁车
  3. 哔哔
  4. Goodbye! Tom
  5. -------------------------
  6. Hey, Careless John请检查窗户是否关好
  7. 捕获异常:Careless John忘记锁车
  8. (报警)滴滴滴滴滴滴滴滴滴滴滴滴

除了上面的四种增强,还有一种特殊的引介增强(Introduction),在下一篇中单独介绍

循序渐进之Spring AOP(3) - 配置代理的更多相关文章

  1. 基于注解的Spring AOP的配置和使用

    摘要: 基于注解的Spring AOP的配置和使用 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不 ...

  2. 基于注解的Spring AOP的配置和使用--转载

    AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...

  3. Spring AOP 和 动态代理技术

    AOP 是什么东西 首先来说 AOP 并不是 Spring 框架的核心技术之一,AOP 全称 Aspect Orient Programming,即面向切面的编程.其要解决的问题就是在不改变源代码的情 ...

  4. Spring AOP 不同配置方式产生的冲突问题

    Spring AOP的原理是 JDK 动态代理和CGLIB字节码增强技术,前者需要被代理类实现相应接口,也只有接口中的方法可以被JDK动态代理技术所处理:后者实际上是生成一个子类,来覆盖被代理类,那么 ...

  5. 深入理解Spring AOP之二代理对象生成

    深入理解Spring AOP之二代理对象生成 spring代理对象 上一篇博客中讲到了Spring的一些基本概念和初步讲了实现方法,当中提到了动态代理技术,包含JDK动态代理技术和Cglib动态代理 ...

  6. spring AOP为什么配置了没有效果?

     spring Aop的配置一定要配置在springmvc配置文件中         springMVC.xml 1 <!-- AOP 注解方式 :定义Aspect --> <!-- ...

  7. spring aop注解配置

    spring aop是面向切面编程,使用了动态代理的技术,这样可以使业务逻辑的代码不掺入其他乱七八糟的代码 可以在切面上实现合法性校验.权限检验.日志记录... spring aop 用的多的有两种配 ...

  8. spring aop + xmemcached 配置service层缓存策略

    Memcached 作用与使用 基本介绍 1,对于缓存的存取方式,简言之,就是以键值对的形式将数据保存在内存中.在日常业务中涉及的操作无非就是增删改查.加入缓存机制后,查询的时候,对数据进行缓存,增删 ...

  9. spring---aop(5)---Spring AOP的配置的背后的配置

    写在前面 Spring AOP中Pointcut,dvice 和 Advisor三个概念 1)切入点 Pointcut 在介绍Pointcut之前,有必要先介绍 Join Point(连接点)概念. ...

随机推荐

  1. 《认知与设计:理解UI设计准则》【PDF】下载

    <认知与设计:理解UI设计准则>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382276 内容介绍 <图灵交互设计丛书·认 ...

  2. BZOJ4817 SDOI2017 相关分析

    4821: [Sdoi2017]相关分析 Time Limit: 10 Sec  Memory Limit: 128 MBSec  Special Judge Description Frank对天文 ...

  3. JS画几何图形之一【直线】

    JS画图的想法经过大脑的时候,觉得有点意思,所以就实践了一番.JS画图为系列文章,本是讲点.线和面 先看样例:http://www.zhaojz.com.cn/demo/draw5.html 一.点 ...

  4. Cat 客户端采用什么策略上报消息树

    策略分类 目前搞清楚两种 第一种(蓝色):默认服务器列表中选一个,算法核心是根据应用名的哈希值取模.也就是说同一个应用始终打到同一台服务器上,如果这台服务器挂了,另选一台服务器. 第二种(红色):应用 ...

  5. Clonezilla SE---克隆linux------转载

    引入: 本博文将会是<学生机房中的虚拟化>专题中的核心内容.因为,通过本篇博文的讲述,大家可以看到用于网络化批量部署Linux系统的Clonezilla SE搭建的全过程.注意,几乎所有命 ...

  6. selenium 封装

    周末无聊 在家封装一个pyselenium.可能这些封装大家都会使用,但是我还是根据我自己的习惯去选择性的去封装一些在我工作中用的,这样的话,我就不用去看selenium的api的,我可以根据我自己的 ...

  7. base64格式图片转换为FormData对象进行上传

    原理:理由ArrayBuffer.Blob和FormData var base64String = /*base64图片串*/; //这里对base64串进行操作,去掉url头,并转换为byte va ...

  8. 阿里云服务器 ubuntu14.04 配置ftp

    1.执行apt-get update 2.使用apt-get命令安装vsftp:apt-get install vsftpd -y 3.先检查一下nologin的位置,通常在/usr/sbin/nol ...

  9. [SDOI2009]E&D

    题目描述 小E 与小W 进行一项名为“E&D”游戏. 游戏的规则如下: 桌子上有2n 堆石子,编号为1..2n.其中,为了方便起见,我们将第2k-1 堆与第2k 堆 (1 ≤ k ≤ n)视为 ...

  10. 各类模块的粗略总结(time,re,os,sys,序列化,pickle,shelve.#!json )

    ***collections 扩展数据类型*** ***re 正则相关操作 正则 匹配字符串*** ***time 时间相关 三种格式:时间戳,格式化时间(字符串),时间元组(结构化时间).***`` ...