AOP的配置方式有2种方式:xml配置和AspectJ注解方式。今天我们就来实践一下xml配置方式。

我采用的jdk代理,所以首先将接口和实现类代码附上

  1. package com.tgb.aop;
  2. public interface UserManager {
  3. public String findUserById(int userId);
  4. }
  5. package com.tgb.aop;
  6. public class UserManagerImpl implements UserManager {
  7. public String findUserById(int userId) {
  8. System.out.println("---------UserManagerImpl.findUserById()--------");
  9. if (userId <= 0) {
  10. throw new IllegalArgumentException("该用户不存在!");
  11. }
  12. return "张三";
  13. }
  14. }

单独写一个Advice通知类进行测试。这个通知类可以换成安全性检测、日志管理等等。

  1. package com.tgb.aop;
  2. import org.aspectj.lang.JoinPoint;
  3. import org.aspectj.lang.ProceedingJoinPoint;
  4. /**
  5. * Advice通知类
  6. * 测试after,before,around,throwing,returning Advice.
  7. * @author Admin
  8. *
  9. */
  10. public class XMLAdvice {
  11. /**
  12. * 在核心业务执行前执行,不能阻止核心业务的调用。
  13. * @param joinPoint
  14. */
  15. private void doBefore(JoinPoint joinPoint) {
  16. System.out.println("-----doBefore().invoke-----");
  17. System.out.println(" 此处意在执行核心业务逻辑前,做一些安全性的判断等等");
  18. System.out.println(" 可通过joinPoint来获取所需要的内容");
  19. System.out.println("-----End of doBefore()------");
  20. }
  21. /**
  22. * 手动控制调用核心业务逻辑,以及调用前和调用后的处理,
  23. *
  24. * 注意:当核心业务抛异常后,立即退出,转向After Advice
  25. * 执行完毕After Advice,再转到Throwing Advice
  26. * @param pjp
  27. * @return
  28. * @throws Throwable
  29. */
  30. private Object doAround(ProceedingJoinPoint pjp) throws Throwable {
  31. System.out.println("-----doAround().invoke-----");
  32. System.out.println(" 此处可以做类似于Before Advice的事情");
  33. //调用核心逻辑
  34. Object retVal = pjp.proceed();
  35. System.out.println(" 此处可以做类似于After Advice的事情");
  36. System.out.println("-----End of doAround()------");
  37. return retVal;
  38. }
  39. /**
  40. * 核心业务逻辑退出后(包括正常执行结束和异常退出),执行此Advice
  41. * @param joinPoint
  42. */
  43. private void doAfter(JoinPoint joinPoint) {
  44. System.out.println("-----doAfter().invoke-----");
  45. System.out.println(" 此处意在执行核心业务逻辑之后,做一些日志记录操作等等");
  46. System.out.println(" 可通过joinPoint来获取所需要的内容");
  47. System.out.println("-----End of doAfter()------");
  48. }
  49. /**
  50. * 核心业务逻辑调用正常退出后,不管是否有返回值,正常退出后,均执行此Advice
  51. * @param joinPoint
  52. */
  53. private void doReturn(JoinPoint joinPoint) {
  54. System.out.println("-----doReturn().invoke-----");
  55. System.out.println(" 此处可以对返回值做进一步处理");
  56. System.out.println(" 可通过joinPoint来获取所需要的内容");
  57. System.out.println("-----End of doReturn()------");
  58. }
  59. /**
  60. * 核心业务逻辑调用异常退出后,执行此Advice,处理错误信息
  61. * @param joinPoint
  62. * @param ex
  63. */
  64. private void doThrowing(JoinPoint joinPoint,Throwable ex) {
  65. System.out.println("-----doThrowing().invoke-----");
  66. System.out.println(" 错误信息:"+ex.getMessage());
  67. System.out.println(" 此处意在执行核心业务逻辑出错时,捕获异常,并可做一些日志记录操作等等");
  68. System.out.println(" 可通过joinPoint来获取所需要的内容");
  69. System.out.println("-----End of doThrowing()------");
  70. }
  71. }

只有Advice还不行,还需要在application-config.xml中进行配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:aop="http://www.springframework.org/schema/aop"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
  9. <bean id="userManager" class="com.tgb.aop.UserManagerImpl"/>
  10. <!--<bean id="aspcejHandler" class="com.tgb.aop.AspceJAdvice"/>-->
  11. <bean id="xmlHandler" class="com.tgb.aop.XMLAdvice" />
  12. <aop:config>
  13. <aop:aspect id="aspect" ref="xmlHandler">
  14. <aop:pointcut id="pointUserMgr" expression="execution(* com.tgb.aop.*.find*(..))"/>
  15. <aop:before method="doBefore"  pointcut-ref="pointUserMgr"/>
  16. <aop:after method="doAfter"  pointcut-ref="pointUserMgr"/>
  17. <aop:around method="doAround"  pointcut-ref="pointUserMgr"/>
  18. <aop:after-returning method="doReturn"  pointcut-ref="pointUserMgr"/>
  19. <aop:after-throwing method="doThrowing" throwing="ex" pointcut-ref="pointUserMgr"/>
  20. </aop:aspect>
  21. </aop:config>
  22. </beans>

编一个客户端类进行测试一下:

  1. package com.tgb.aop;
  2. import org.springframework.beans.factory.BeanFactory;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Client {
  5. public static void main(String[] args) {
  6. BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
  7. UserManager userManager = (UserManager)factory.getBean("userManager");
  8. //可以查找张三
  9. userManager.findUserById(1);
  10. System.out.println("=====我==是==分==割==线=====");
  11. try {
  12. // 查不到数据,会抛异常,异常会被AfterThrowingAdvice捕获
  13. userManager.findUserById(0);
  14. } catch (IllegalArgumentException e) {
  15. }
  16. }
  17. }

结果如图:

 

值得注意的是Around与Before和After的执行顺序。3者的执行顺序取决于在xml中的配置顺序。图中标记了3块,分别对应Before,Around,After。其中②中包含有③。这是因为aop:after配置到了aop:around的前面,如果2者调换一下位置,这三块就会分开独立显示。如果配置顺序是aop:after  -> aop:around ->aop:before,那么①和③都会包含在②中。这种情况的产生是由于Around的特殊性,它可以做类似于Before和After的操作。当安全性的判断不通过时,可以阻止核心业务逻辑的调用,这是Before做不到的。

  

使用xml可以对aop进行集中配置。很方便而简单。可以对所有的aop进行配置,当然也可以分开到单独的xml中进行配置。当需求变动时,不用修改代码,只要重新配置aop,就可以完成修改操作。

Spring Aop实例之xml配置的更多相关文章

  1. spring aop自动代理xml配置

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

  2. Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置

    用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...

  3. Spring装配Bean---使用xml配置

    声明Bean Spring配置文件的根元素是<beans>. 在<beans>元素内,你可以放所有的Spring配置信息,包括<bean>元素的声明. 除了Bean ...

  4. Spring AOP 注解和xml实现 --转载

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

  5. Spring的配置文件ApplicationContext.xml配置头文件解析

    Spring的配置文件ApplicationContext.xml配置头文件解析 原创 2016年12月16日 14:22:43 标签: spring配置文件 5446 spring中的applica ...

  6. 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP

    上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. package com.yan ...

  7. Spring学习十四----------Spring AOP实例

    © 版权声明:本文为博主原创文章,转载请注明出处 实例 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...

  8. (一)spring aop的两种配置方式。

    sring aop的方式有两种:(1)xml文件配置方式(2)注解的方式实现,我们可以先通过一个demo认识spring aop的实现,然后再对其进行详细的解释. 一.基于注解的springAop配置 ...

  9. Spring AOP实例——异常处理和记录程序执行时间

    实例简介: 这个实例主要用于在一个系统的所有方法执行过程中出线异常时,把异常信息都记录下来,另外记录每个方法的执行时间. 用两个业务逻辑来说明上述功能,这两个业务逻辑首先使用Spring AOP的自动 ...

随机推荐

  1. [转]利用vertical-align:middle实现在整个页面居中

    本文转自:http://www.cnblogs.com/xueming/archive/2012/03/21/VerticalAlign.html 如果想让一个div或一张图片相对于整个页面居中,用v ...

  2. C#基础性问题

    解决方案.项目.类之间的关系: 一个解决方案可以包含多个项目.一个项目可以包含多个类 解决方案:公司 项目:部门 类:员工 .sln:解决方案文件,里面包含着整个解决方案的信息,可以双击运行. .cs ...

  3. JavaScript部分总结

    一.词法结构 1.js里面区分大小写 2.注释分为两类:  // 单行注释    /*多行注释*/ 3.字面量(直接量 literal) 12                             ...

  4. django 学习-10 Django多对多关系模型

    1.vim blog/models.py class   Author(models.Model): name = models.CharField(max_length=30) def unicod ...

  5. 【转载】Kafka实现篇之消息和日志

    http://blog.csdn.net/honglei915/article/details/37760631 消息格式 日志 一个叫做“my_topic”且有两个分区的的topic,它的日志有两个 ...

  6. SSIS_TXT有规则资料导入到EXCEL

    SSIS开发需要完全安装sqlserver.本次demo是sqlserver2008. 1.创建项目 2.解决方案打开如图所示. 3.拉取一个序列容器,一个数据流任务. 4.在数据流任务点击.拉取一个 ...

  7. ios自动滚动图片功能源码

    源码AdScrollerView,一个已经封装好的UIScrollView的子类,可以自动滚动图片以及对应的描述语,类似淘宝app首页的广告滚动效果.滚动图片数量不限,并且显示pageControl. ...

  8. JS调用iframe方式实现Web区域打印页面内容

    1.程序说明 1) 此程序可以实现选择页面中的区域进行打印,以iframe方式进行打印: 2) 与原生态的print() 区别在于,取消打印页面后可以完整保留当前访问页面的内容. 2.代码部分 1) ...

  9. Xcode-项目模板修改

    项目模板就是创建工程的时候选择的某一个条目, Xcode会根据选择的条目生成固定格式的项目 例如想创建一个命令行项目就选择Command Line Tool 如何修改项目模板 1.应用程序中,找到Xc ...

  10. java坑之无法创建线程

    环境:linux 错误:java.lang.OutOfMemoryError: unable to create new native thread 原因:OS对线程是有限制 解决办法: 在Linux ...