Spring AOP 基础 Java 动态代理实现,阅读文章以前,你最好有如下基础:html
java动态代理java
AOP(Aspect Oriented Programming),即面向切面编程,它是 OOP(Object Oriented Programming,面向对象编程)的补充和完善。git
在开发中,功能点一般分为横向关注点和核心关注点,核心关注点就是业务关注的点,大部分是要给用户看的。而横向关注点是用户不关心,而咱们程序又必须实现的,它的特色是横向分布于核心关注点各处,好比日志功能,核心关注点:增删改查都须要实现日志功能。若是用 面向对象编程来实现的话,那增删改查都须要写一遍日志代码,这会形成很是多冗余代码,显然是不合理的。而此时,AOP 应运而生。它统必定义了,什么时候、何处执行这些横向功能点github
要理解 AOP 首先要认识如下相关术语,有这么个场景,我须要给用户模块的增删改查,实现日志功能,我如今经过这个场景来解释以上术语。spring
被拦截到的点,由于 Spring 只支持方法类型的链接点,因此在 Spring 中链接点指的就是被拦截到的方法。场景中,链接点就是增删改查方法自己。编程
所谓通知指的就是指拦截到链接点以后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类。
一、前置通知(Before):在目标方法被调用以前调用通知功能;
二、后置通知(After):在目标方法完成以后调用通知,此时不会关
心方法的输出是什么;
三、返回通知(After-returning):在目标方法成功执行以后调用通
知;
四、异常通知(After-throwing):在目标方法抛出异常后调用通知;
五、环绕通知(Around):通知包裹了被通知的方法,在被通知的方
法调用以前和调用以后执行自定义的行为。app
对链接点进行拦截的定义,它会匹配通知所要织入的一个或多个链接点。它的格式是这样的:测试
类是对物体特征的抽象,切面就是对横切关注点的抽象,它定义了切点和通知。场景中,日志功能就是这个抽象,它定义了你要对拦截方法作什么?切面是通知和切点的结合。通知和切点共同定义了切面的所有内容——它是什么,在什么时候和何处完成其功能。ui
将切面应用到目标对象并致使代理对象建立的过程spa
在不修改代码的前提下,引入能够在运行期为类动态地添加一些方法或字段
首先,定义一个加减乘除的接口,代码以下:
public interface ArithmeticCalculator { int add(int i, int j); int sub(int i, int j); int mul(int i, int j); int div(int i, int j); }
定义一个实现类,代码以下:
@Component("arithmeticCalculator") public class ArithmeticCalculatorImpl implements ArithmeticCalculator { public int add(int i, int j) { int result = i + j; return result; } public int sub(int i, int j) { int result = i - j; return result; } public int mul(int i, int j) { int result = i * j; return result; } public int div(int i, int j) { int result = i / j; return result; } }
定义切面,代码以下:
/** * 1. 加入 jar 包 * com.springsource.net.sf.cglib-2.2.0.jar * com.springsource.org.aopalliance-1.0.0.jar * com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar * spring-aspects-4.0.0.RELEASE.jar * * 2. 在 Spring 的配置文件中加入 aop 的命名空间。 * * 3. 基于注解的方式来使用 AOP * 3.1 在配置文件中配置自动扫描的包: <context:component-scan base-package="com.atguigu.spring.aop"></context:component-scan> * 3.2 加入使 AspjectJ 注解起做用的配置: <aop:aspectj-autoproxy></aop:aspectj-autoproxy> * 为匹配的类自动生成动态代理对象. * * 4. 编写切面类: * 4.1 一个通常的 Java 类 * 4.2 在其中添加要额外实现的功能. * * 5. 配置切面 * 5.1 切面必须是 IOC 中的 bean: 实际添加了 @Component 注解 * 5.2 声明是一个切面: 添加 @Aspect * 5.3 声明通知: 即额外加入功能对应的方法. * 5.3.1 前置通知: @Before("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(int, int))") * @Before 表示在目标方法执行以前执行 @Before 标记的方法的方法体. * @Before 里面的是切入点表达式: * * 6. 在通知中访问链接细节: 能够在通知方法中添加 JoinPoint 类型的参数, 从中能够访问到方法的签名和方法的参数. * * 7. @After 表示后置通知: 在方法执行以后执行的代码. */ //经过添加 @EnableAspectJAutoProxy 注解声明一个 bean 是一个切面! @Component @Aspect public class LoggingAspect { /** * 在方法正常开始前执行的代码 * @param joinPoint */ @Before("execution(public int com.nasus.spring.aop.impl.*.*(int, int))") public void beforeMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); Object [] args = joinPoint.getArgs(); System.out.println("The method " + methodName + " begins with " + Arrays.asList(args)); } /** * 在方法执行后执行的代码,不管方法是否抛出异常 * @param joinPoint */ @After("execution(* com.nasus.spring.aop.impl.*.*(..))") public void afterMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " ends"); } /** * 在方法正常结束后执行的代码 * 返回通知是能够访问到方法的返回值的 * @param joinPoint * @param result */ @AfterReturning(value = "execution(public int com.nasus.spring.aop.impl.*.*(int, int))", returning = "result") public void afterReturning(JoinPoint joinPoint, Object result){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method " + methodName + " ends with " + result); } /** * 在目标方法出现异常时,会执行的代码 * 能够访问到异常对象,能够指定在出现特定异常时再执行通知代码 * @param joinPoint * @param ex */ @AfterThrowing(value = "execution(public int com.nasus.spring.aop.impl.*.*(int, int))", throwing = "ex") public void afterThrowing(JoinPoint joinPoint, Exception ex){ String methodNames = joinPoint.getSignature().getName(); System.out.println("The method " + methodNames + " occurs exception: " + ex); } /** * 环绕通知须要携带 ProceedingJoinPoint 类型参数 * 环绕通知相似于动态代理的全过程; ProceedingJoinPoint 类型的参数能够决定是否执行目标方法 * 且环绕通知必须有返回值,返回值极为目标方法的返回值 * @param pjd * @return */ @Around("execution(public int com.nasus.spring.aop.impl.*.*(int, int))") public Object aroundMethod(ProceedingJoinPoint pjd){ Object result = null; String methodName = pjd.getSignature().getName(); try { // 前置通知 System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs())); // 执行目标方法 result = pjd.proceed(); // 返回通知 System.out.println("The method " + methodName + " ends with " + result); }catch (Throwable e) { // 异常通知 System.out.println("The method " + methodName + " occurs exception: " + e); throw new RuntimeException(e); } // 后置通知 System.out.println("The method " + methodName + " ends"); return result; } }
xml 配置,代码以下:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 自动扫描的包 --> <context:component-scan base-package="com.nasus.spring.aop.impl"></context:component-scan> <!-- 使 AspectJ 的注解起做用 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <
测试方法:
public class Main { public static void main(String args[]){ // 一、建立 Spring 的 IOC 容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext_aop.xml"); // 二、从 IOC 容器中获取 bean 实例 ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class); // 三、使用 bean arithmeticCalculator.add(3,6); } }
测试结果:
The method add begins with [3, 6] The method add begins with [3, 6] The method add ends with 9 The method add ends The method add ends The method add ends with 9
关于 xml 的实现方式,网上发现一篇文章写的不错,此处,再也不赘述,有兴趣的参考如下连接:
http://www.javashuo.com/article/p-rdagsfro-db.html
https://github.com/turoDog/review_spring
推荐阅读: