先用代码讲一下什么是传统的AOP(面向切面编程)编程

需求:实现一个简单的计算器,在每一步的运算前添加日志。最传统的方式如下:

Calculator.Java

  1. package cn.limbo.spring.aop.calculator;
  2. /**
  3. * Created by Limbo on 16/7/14.
  4. */
  5. public interface Calculator {
  6. int add(int i , int j);
  7. int sub(int i , int j);
  8. int mul(int i , int j);
  9. int div(int i , int j);
  10. }

CalculatorImpl.java

  1. package cn.limbo.spring.aop.calculator;
  2. /**
  3. * Created by Limbo on 16/7/14.
  4. */
  5. public class CalculatorImpl implements Calculator {
  6. @Override
  7. public int add(int i, int j) {
  8. System.out.println("The method add begin with [ "+ i +"," + j+" ]");
  9. System.out.println("The method add end with [ "+ i +"," + j+"]");
  10. return i + j;
  11. }
  12. @Override
  13. public int sub(int i, int j) {
  14. System.out.println("The method sub begin with [ "+ i +"," + j+" ]");
  15. System.out.println("The method sub end with [ "+ i +"," + j+" ]");
  16. return i - j;
  17. }
  18. @Override
  19. public int mul(int i, int j) {
  20. System.out.println("The method mul begin with [ "+ i +"," + j+" ]");
  21. System.out.println("The method mul end with [ "+ i +"," + j+" ]");
  22. return i * j;
  23. }
  24. @Override
  25. public int div(int i, int j) {
  26. System.out.println("The method div begin with [ "+ i +"," + j+" ]");
  27. System.out.println("The method div end with [ "+ i +"," + j+" ]");
  28. return i / j;
  29. }
  30. }

这样就完成了需求,但是我们发现,倘若是要修改日志的信息,那么就需要在具体方法里面改,这样做很麻烦,而且把原本清爽的方法改的十分混乱,方法应该表现的是核心功能,而不是这些无关紧要的关注点,下面用原生的java的方式实现在执行方法时,动态添加输出日志方法

CalculatorImpl.java

  1. package cn.limbo.spring.aop.calculator;
  2. /**
  3. * Created by Limbo on 16/7/14.
  4. */
  5. public class CalculatorImpl implements Calculator {
  6. @Override
  7. public int add(int i, int j) {
  8. return i + j;
  9. }
  10. @Override
  11. public int sub(int i, int j) {
  12. return i - j;
  13. }
  14. @Override
  15. public int mul(int i, int j) {
  16. return i * j;
  17. }
  18. @Override
  19. public int div(int i, int j) {
  20. return i / j;
  21. }
  22. }

CalculatorLoggingProxy.java

  1. package cn.limbo.spring.aop.calculator;
  2. import java.lang.reflect.InvocationHandler;
  3. import java.lang.reflect.Method;
  4. import java.lang.reflect.Proxy;
  5. import java.util.Arrays;
  6. import java.util.Objects;
  7. /**
  8. * Created by Limbo on 16/7/14.
  9. */
  10. public class CalculatorLoggingProxy {
  11. //要代理的对象
  12. private Calculator target;
  13. public CalculatorLoggingProxy(Calculator target) {
  14. this.target = target;
  15. }
  16. public Calculator getLoggingProxy(){
  17. //代理对象由哪一个类加载器负责加载
  18. ClassLoader loader = target.getClass().getClassLoader();
  19. //代理对象的类型,即其中有哪些方法
  20. Class[] interfaces = new Class[]{Calculator.class};
  21. // 当调用代理对象其中的方法时,执行改代码
  22. InvocationHandler handler = new InvocationHandler() {
  23. @Override
  24. /**
  25. * proxy:正在返回的那个对象的代理,一般情况下,在invoke方法中不使用该对象
  26. * method:正在被调用的方法
  27. * args:调用方法时,传入的参数
  28. */
  29. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  30. String methodName = method.getName();
  31. System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));
  32. //日志
  33. Object result = method.invoke(target,args);
  34. System.out.println("The method " + methodName + " ends with " + result);
  35. return result;
  36. }
  37. };
  38. Calculator proxy = (Calculator) Proxy.newProxyInstance(loader,interfaces,handler);
  39. return proxy;
  40. }
  41. }

Main.java

  1. package cn.limbo.spring.aop.calculator;
  2. /**
  3. * Created by Limbo on 16/7/14.
  4. */
  5. public class Main {
  6. public static void main(String[] args) {
  7. Calculator calculator = new CalculatorImpl();
  8. Calculator proxy = new CalculatorLoggingProxy(calculator).getLoggingProxy();
  9. int result = proxy.add(1,2);
  10. System.out.println("--->" + result);
  11. result = proxy.sub(1,2);
  12. System.out.println("--->" + result);
  13. result = proxy.mul(3,2);
  14. System.out.println("--->" + result);
  15. result = proxy.div(14,2);
  16. System.out.println("--->" + result);
  17. }
  18. }

这样写虽然已经简化了代码,而且可以任意修改日志信息代码,但是写起来还是很麻烦!!!

下面我们使用spring自带的aop包实现但是要加入

com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.8.5.RELEASE.jar

这两个额外的包

下面看代码,要点全部卸载代码里面了

Calculator.java

  1. package cn.limbo.spring.aop.impl;
  2. /**
  3. * Created by Limbo on 16/7/14.
  4. */
  5. public interface Calculator {
  6. int add(int i, int j);
  7. int sub(int i, int j);
  8. int mul(int i, int j);
  9. int div(int i, int j);
  10. }

CalculatorImpl.java

  1. package cn.limbo.spring.aop.impl;
  2. import org.springframework.stereotype.Component;
  3. /**
  4. * Created by Limbo on 16/7/14.
  5. */
  6. @Component("calculatorImpl")
  7. public class CalculatorImpl implements Calculator {
  8. @Override
  9. public int add(int i, int j) {
  10. return i + j;
  11. }
  12. @Override
  13. public int sub(int i, int j) {
  14. return i - j;
  15. }
  16. @Override
  17. public int mul(int i, int j) {
  18. return i * j;
  19. }
  20. @Override
  21. public int div(int i, int j) {
  22. return i / j;
  23. }
  24. }

LoggingAspect.java

  1. package cn.limbo.spring.aop.impl;
  2. import org.aspectj.lang.JoinPoint;
  3. import org.aspectj.lang.ProceedingJoinPoint;
  4. import org.aspectj.lang.annotation.*;
  5. import org.springframework.core.annotation.Order;
  6. import org.springframework.stereotype.Component;
  7. import java.util.Arrays;
  8. import java.util.List;
  9. import java.util.Objects;
  10. /**
  11. * Created by Limbo on 16/7/14.
  12. */
  13. //把这个类声明为切面:需要该类放入IOC容器中,再声明为一个切面
  14. @Order(0)//指定切面优先级,只越小优先级越高
  15. @Aspect
  16. @Component
  17. public class LoggingAspect {
  18. /**
  19. * 定义一个方法,用于声明切入点表达式,一般的,该方法中再不需要添加其他代码
  20. * 主要是为了重用路径,使用@Pointcut来声明切入点表达式
  21. * 后面的其他通知直接使用方法名来引用当前的切入点表达式
  22. */
  23. @Pointcut("execution(* cn.limbo.spring.aop.impl.Calculator.*(..))")
  24. public void declareJointPointExpression()
  25. {
  26. }
  27. //声明该方法是一个前置通知:在目标方法之前执行
  28. @Before("declareJointPointExpression()")
  29. //  @Before("execution(* cn.limbo.spring.aop.impl.*.*(..))")  该包下任意返回值,任意类,任意方法,任意参数类型
  30. public void beforeMethod(JoinPoint joinPoint)
  31. {
  32. String methodName = joinPoint.getSignature().getName();
  33. List<Object> args = Arrays.asList(joinPoint.getArgs());
  34. System.out.println("The Method "+ methodName+" Begins With " + args);
  35. }
  36. //在目标方法执行之后执行,无论这个方法是否出错
  37. //在后置通知中还不能访问目标方法的返回值,只能通过返回通知访问
  38. @After("execution(* cn.limbo.spring.aop.impl.Calculator.*(int,int))")
  39. public void afterMethod(JoinPoint joinPoint)
  40. {
  41. String methodName = joinPoint.getSignature().getName();
  42. System.out.println("The Method "+ methodName+" Ends ");
  43. }
  44. /**
  45. * 在方法正常结束后执行的代码
  46. * 返回通知是可以访问到方法的返回值
  47. */
  48. @AfterReturning(value = "execution(* cn.limbo.spring.aop.impl.Calculator.*(..))",returning = "result")
  49. public void afterReturning(JoinPoint joinPoint , Object result)
  50. {
  51. String methodName = joinPoint.getSignature().getName();
  52. System.out.println("The Method " + methodName + " Ends With " + result);
  53. }
  54. /**
  55. *在目标方法出现异常的时候执行代码
  56. * 可以访问异常对象,且可以指定出现特定异常时再执行通知
  57. */
  58. @AfterThrowing(value = "execution(* cn.limbo.spring.aop.impl.Calculator.*(..))",throwing = "ex")
  59. public void afterThrowing(JoinPoint joinPoint,Exception ex)//Exception 可以改成 NullPointerException等特定异常
  60. {
  61. String methodName = joinPoint.getSignature().getName();
  62. System.out.println("The Method " + methodName + " Occurs With " + ex);
  63. }
  64. /**
  65. * 环绕通知需要ProceedingJoinPoint 类型参数 功能最强大
  66. * 环绕通知类似于动态代理的全过程:ProceedingJoinPoint 类型的参数可以决定是否执行目标方法
  67. * 且环绕通知必须有返回值,返回值即为目标方法的返回值
  68. */
  69. @Around("execution(* cn.limbo.spring.aop.impl.Calculator.*(..))")
  70. public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint)
  71. {
  72. Object result =null;
  73. String methodName = proceedingJoinPoint.getSignature().getName();
  74. try {
  75. //前置通知
  76. System.out.println("The Method " + methodName + " Begins With " + Arrays.asList(proceedingJoinPoint.getArgs()));
  77. result = proceedingJoinPoint.proceed();
  78. // 返回通知
  79. System.out.println("The Method " + methodName + " Ends With " + result);
  80. } catch (Throwable throwable) {
  81. //异常通知
  82. throwable.printStackTrace();
  83. }
  84. System.out.println("The Method " + methodName + " Ends ");
  85. return result;
  86. }
  87. }

applicationContext.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:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
  7. <!--配置自动扫描的包-->
  8. <context:component-scan base-package="cn.limbo.spring.aop.impl"></context:component-scan>
  9. <!--使Aspect注解起作用,自动为匹配的类生成代理对象-->
  10. <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  11. </beans>

用xml来配置aop

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>

一共有5类的通知,其中around最为强大,但是不一定最常用,aspect的底层实现都是通过代理来实现的,只能说这个轮子造的不错

2016.12.09更新

最近在实际项目中配置aop发现了几个问题,实际的项目配置的的模式是ssh即Spring4+SpringMVC+Hibernate4的模式。

基于注解的方式配置方式有些坑点:

1.发现SpringMVC中aop不起任何作用

经过排查和查找网上的资料,发现问题如下:

Spring MVC启动时的配置文件,包含组件扫描、url映射以及设置freemarker参数,让spring不扫描带有@Service注解的类。为什么要这样设置?因为springmvc.xml与applicationContext.xml不是同时加载,如果不进行这样的设置,那么,spring就会将所有带@Service注解的类都扫描到容器中,等到加载applicationContext.xml的时候,会因为容器已经存在Service类,使得cglib将不对Service进行代理,直接导致的结果就是在applicationContext 中的事务配置不起作用,发生异常时,无法对数据进行回滚。以上就是原因所在。

所以改进后的applicationContext.xml如下(只是更改自动扫描包的那个配置):

  1. <context:component-scan base-package="cn.limbo">
  2. <!--不要将Controller扫进来,否则aop无法使用-->
  3. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
  4. </context:component-scan>

spring-mvc.xml扫描包配置如下:

  1. <context:component-scan base-package="cn.limbo">
  2. <!--不要将Service扫进来,否则aop无法使用-->
  3. <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
  4. </context:component-scan>

这样就可以成功解决ssh配置下不能使用aop的情况。看来扫描包的时候不能暴力直接全部扫描进来啊,真是成也扫描,败也扫描,手动哭笑不得。

Spring--AOP 例子的更多相关文章

  1. 一个简单的Spring AOP例子

    转载自: http://www.blogjava.net/javadragon/archive/2006/12/03/85115.html 经过这段日子的学习和使用Spring,慢慢地体会到Sprin ...

  2. spring aop例子

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAATcAAAFWCAIAAACD6E2aAAAgAElEQVR4nO2df1gTV77/55/93z/2ee

  3. Spring5.0源码学习系列之Spring AOP简述

    前言介绍 附录:Spring源码学习专栏 在前面章节的学习中,我们对Spring框架的IOC实现源码有了一定的了解,接着本文继续学习Springframework一个核心的技术点AOP技术. 在学习S ...

  4. c# spring aop的简单例子

    刚刚完成了一个c#的spring aop简单例子,是在mac下用Xamarin Studio开发的.代码如下: 接口 using System; using System.Collections.Ge ...

  5. 峰Spring4学习(6)spring AOP的应用例子

    一.AOP简介: 二.AOP实例: 三.使用的例子 需求:在student添加的前后,打印日志信息: 0)spring AOP需要引用的jar包: 1)StudentService.java接口: p ...

  6. Spring AOP 学习例子

    http://outofmemory.cn/code-snippet/3762/Spring-AOP-learn-example     工作忙,时间紧,不过事情再多,学习是必须的.记得以前的部门老大 ...

  7. 学习AOP之深入一点Spring Aop

    上一篇<学习AOP之认识一下SpringAOP>中大体的了解了代理.动态代理及SpringAop的知识.因为写的篇幅长了点所以还是再写一篇吧.接下来开始深入一点Spring aop的一些实 ...

  8. 学习AOP之认识一下Spring AOP

    心碎之事 要说知道AOP这个词倒是很久很久以前了,但是直到今天我也不敢说非常的理解它,其中的各种概念即抽象又太拗口. 在几次面试中都被问及AOP,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...

  9. spring aop

    什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),它利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将 ...

  10. 基于Spring AOP的JDK动态代理和CGLIB代理

    一.AOP的概念  在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的 ...

随机推荐

  1. EasyUI 数据网格行过滤

    前段时间发现一个GridView很有用的功能,可以筛选数据,实现起来很简单 一.添加一个引用,这个可以自己去网上下载 <script type="text/javascript&quo ...

  2. python基础——面向过程的编程思想及举例

    面向过程的编程思想 1.面向过程的编程思想及举例 写程序时: 要先想功能,分步实现 2. os模块中walk输出目录中文件路径 os.walk() 方法用于通过在目录树中游走输出在目录中的文件名,向上 ...

  3. python基础—迭代器、生成器

    python基础-迭代器.生成器 1 迭代器定义 迭代的意思是重复做一些事很多次,就像在循环中做的那样. 只要该对象可以实现__iter__方法,就可以进行迭代. 迭代对象调用__iter__方法会返 ...

  4. anguar使用指令写选项卡

    今天,我们来学习一下angular中怎么使用指令来实现两个选项卡的问题. 首先,要先引入jQuery文件与angularjs文件. <!DOCTYPE html><html lang ...

  5. codeforces 798c Mike And Gcd Problem

    题意: 给出一个数列,现在有一种操作,可以任何一个a[i],用a[i] – a[i+1]和a[i]+a[i+1]替代a[i]和a[i+1]. 问现在需要最少多少次操作,使得整个数列的gcd大于1. 思 ...

  6. 2018 php的flush和ob_flush不起作用 整理解决

    2018 php的flush和ob_flush不起作用 整理解决成功解决. 要点 : 使用函数 str_repeat2处配置:检查php.ini.Nginx 中有下面两个设置使用方式:echo str ...

  7. 南京邮电大学java程序设计作业在线编程第四次作业

    王利国的的 "Java语言程序设计第4次作业(2018)" 详细 主页 我的作业列表 作业结果详细 总分:100 选择题得分:40  1.下列方法定义中,正确的是() A.doub ...

  8. 南京邮电大学java程序设计作业在线编程第三次作业

    王利国的"Java语言程序设计第3次作业(2018)"详细 作业结果详细 总分:100 选择题得分:60  1. 设有如下定义语句: String s1="My cat& ...

  9. POJ-1751 Highways---确定部分边的MST

    题目链接: https://vjudge.net/problem/POJ-1751 题目大意: 有一个N个城市M条路的无向图,给你N个城市的坐标,然后现在该无向图已经有M条边了,问你还需要添加总长为多 ...

  10. Selenium_java coding

    1)public class HelloWorld { // class 是类的意思 // 类名指的是class后面这个词,这个词是我们起的名 public static void main(Stri ...