概念:
AOP,面向切面编程,就是当你已经完成,或者更加注意一些核心功能的时候。需要使用的编程过程,这个使用,你会发现,一个核心的业务,是由一个核心功能,和若干个额外服务类型的功能,统一组成的,比如数据验证,再比如日志的记录。
其实在场景中,我们的核心功能是针对数据库的操作,可能是增删过程。而服务类功能,比如记录日志,或者输入内容的验证过程。其实不是非有不可。没有这个功能,核心功能也能正常执行。
这个时候,我们就要考虑一个问题,这些必要或者非必要的额外功能,是不是要写在你的核心代码过程当中啊?
我们现在,将核心业务,做一个简単的切分
-
核心功能
-
辅助功能
spring-AOP 的实现:
1、明确名词
切点(pointcut) 在某些个核心方法之前,之后等位置
- 切点表达式:pointcut = execution(* com.lanou.test.service. * . * (…))
- execution(返回值 全包名.类名.方法名 (参数集合))
增强(advisor) 辅助功能
织入(aspect) 将辅助功能,安装在切点上的过程
2、AOP增强的五种类型:
- 前置增强:before
- 后置增强:after
- 环绕增强:around
- 异常增强:throw
- 返回增强:after returning
通过实现接口的方式实现增强
1、前置增强:
通过实现 MethodBeforeAdvice接口,然后编写前置通知的内容。
before方法参数的含义:
- Method method:要被增强的方法
- Object[] objects:切点方法的参数
- Object o:要代理的类的对象(就是要执行方法的对象)
所以前置通知可以拿到参数,并且修改参数或对参数做一些判断
package com.lanou.test.advice;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class MyBeforAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
// 如果传入的参数不是shiruntao的话,就不执行切点方法
if (objects[0].equals("shiruntao")) {
System.out.println("我是AOP ");
} else {
throw new Exception("参数不是shiruntao");
}
}
}
2、后置增强:
通过实现 AfterReturningAdvice, AfterAdvice 接口,然后编写后置通知的内容。
afterReturning方法参数的含义:
-
Object o:返回值对象
-
Method method:要被增强的方法
-
Object[] objects:切点方法的参数
-
Object o1:要代理的类的对象(就是要执行方法的对象)
所以后置通知可以拿到返回值,并且可以修改返回值
package com.lanou.test.advice;
import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class MyAfterAdvice implements AfterReturningAdvice, AfterAdvice {
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("你好,我是返回后置通知");
}
}
3、配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.