AOP
1. 什么是AOP
- 面向切面编程:利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发效率。
- 通俗描述就是,不通过修改源代码的方式,在我们的主干功能里面添加一个新的功能。
2. AOP底层原理
-
AOP底层使用动态代理
-
有接口情况的动态代理,使用JDK动态代理
- 创建接口实现类的代理对象,增强类的方法
-
-
没有接口情况的动态代理,使用CGLIB动态代理
-
创建当前类子类的代理对象
-
-
AOP(JDK动态代理)
-
使用jdk动态代理,使用Proxy类里面的方法创建出代理对象
-
第一个参数:类加载器
-
第二个参数:增强方法所在的类实现的接口,支持多个接口
-
第三个参数:实现这个接口InvocationHandler,创建代理对象,写增强的方法
-
动态代理思想:
-
一个接口A,一个实现类B,一个Proxy类
-
Proxy类继承InvocationHandler:
- 定义需要代理的实现类B,并set方法它
- 重写invoke方法,并在方法里增强自己想添加的功能,return method.invoke
- 写一个生成代理类的方法,里面返回Proxy.newProxyInstance,参数分别是类加载器,增强方法所在类实现的接口,调用哪个类的invoke
-
测试:
-
新建一个接口实现对象
-
Proxy生成一个对象,对象set代理对象
-
获取代理对象,这里一定要注意强制转换!同级是不能转换的,所以要用共同的接口来转换(这样才是父子关系)
-
调用新增强的方法
-
使用AOP织入,需要导入一个依赖包:
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
3. 使用Spring的API接口实现AOP
<!-- 方式一:使用原生Spring API接口 -->
<!--配置aop:需要导入aop约束-->
<aop:config>
<!--切入点 execution:要执行的位置-->
<aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<!--执行环绕增强-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
public class Log implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
//objects:参数
//target:目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
import java.lang.reflect.Method;
//returnValue 返回值
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
}
}
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("SpringApplicationContext.xml");
//动态代理代理的是接口
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
4. 自定义实现AOP(主要是切面定义)
切面定义:
public class DiyPointCut {
public void before(){
System.out.println("=========方法执行前==========");
}
public void after(){
System.out.println("=========方法执行后==========");
}
}
配置定义:
<!--方式二:自定义类-->
<bean id="diy" class="com.kuang.diy.DiyPointCut"/>
<aop:config>
<!--自定义切面,ref要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
5. 使用注解实现AOP
开启注解支持:
<!--方式三-->
<bean id="annotationPointCut" class="com.kuang.diy.AnnotationPointCut"/>
<!--开启注解支持-->
<aop:aspectj-autoproxy/>
@Aspect //标注这个类是一个切面
public class AnnotationPointCut {
@Before("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("=========方法执行前==========");
}
@After("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("=========方法执行后==========");
}
//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
@Around("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
//执行方法
Object proceed = jp.proceed();
System.out.println("环绕后");
}