1. AOP基本概念
- 什么是AOP?
AOP为Aspect Oriented Programming的缩写,意为面向切面编程。用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
通俗描述:不通过修改源代码的方式,在主干功能里添加新功能。
如:
2. AOP底层原理
- AOP底层使用动态代理
(1)有两种情况的动态代理
第一种:有接口的情况,使用JDK动态代理
创建接口实现类代理对象,增强类的方法
第二种:没有接口的情况,使用CGLIB动态代理
创建子类的代理对象,增强类的方法
- JDK动态代理实现
使用JDK动态代理,使用java.lang.reflect.Proxy类里面的方法创建代理对象。
(1)调用newProxyIntance方法
static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
//Returns an instance of a proxy class for the specified interfaces
//that dispatches method invocations to the specified invocation handler.
方法里面有三个参数:
第一个参数:类的加载器
第二个参数:增强方法所在的类,这个类实现的接口,支持多个接口
第三个参数:实现这个接口InvocationHandler,创建代理对象,写增强方法。
编写JDK动态代理方法
(1)创建接口,定义方法
public interface UserDao {
int add(int a, int b);
String update(String id);
}
(2)创建接口实现类,实现方法
public class UserDaoImpl implements UserDao{
@Override
public int add(int a, int b) {
return a+b;
}
@Override
public String update(String id) {
return id;
}
}
(3)使用Proxy类创建接口代理对象
package org.example.spring5;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
/**
* @author AmorFati
* @create 2020/10/29 22:49
*/
public class JDKProxy {
public static void main(String[] args) {
//创建接口实现类的代理对象
Class[] interfaces = {UserDao.class};
UserDaoImpl userDaoImpl = new UserDaoImpl();
UserDao userDao = (UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new UserDaoProxy(userDaoImpl));
int result = userDao.add(1, 2);
System.out.println("result:"+result);
}
}
//创建代理对象代码
class UserDaoProxy implements InvocationHandler {
//1. 把创建的是谁的代理对象,把谁传递过来
//有参构造传递
private Object obj;
public UserDaoProxy(Object obj) {
this.obj = obj;
}
//增强的逻辑
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//方法之前
System.out.println("方法之前执行..."+ method.getName()+"传递的参数"+ Arrays.toString(args));
//被增强的方法执行
Object res = method.invoke(obj, args);
//方法之后
System.out.println("方法之后执行..." + obj);
return res;
}
}
3. AOP术语
-
连接点:类里面那些方法可以被增强,这些方法成为连接点。
-
切入点:实际被真正增强的方法,称为切入点。
-
通知(增强)
(1)实际增强的逻辑部分成为通知(增强)
(2)通知有多种类型- 前置通知
- 后置通知
- 环绕通知
- 异常通知
- 最终通知
-
切面
是动作,把通知应用到切入点的过程
4. AOP操作
一、 准备工作
-
Spring框架一般都是基于AspectJ实现AOP操作。
(1)什么是AspectJ?
AspectJ不是Spring框架,独立AOP框架,一般把AspectJ和Spring框架一起使用,进行AOP操作。 -
基于AspectJ实现AOP操作。
(1)基于xml配置文件实现
(2)基于注解方式实现(常用) -
在项目工程里卖弄引入AOP相关依赖
-
切入点表达式
(1)切入点表达式的作用:知道对哪个类里面的哪个方法进行增强
(2)语法结构:
execution([权限修饰符][类全路径][方法名称]([参数列表])
举例1:对org.example.dao.BookDao类里面的add进行增强
execution(* org.example.dao.BookDao.add(..))
//*号表示private public都可以
举例2:对org.example.dao.BookDao类里面的所有方法进行增强
execution(* org.example.dao.BookDao.*(..))
举例3:对org.example.dao包里面所有类,类里面的所有方法进行增强
execution(* org.example.dao.*.*(..))
二、AspectJ注解实现AOP操作
1.创建类,在类里面定义方法
public class User {
public void add() {
System.out.println("user add...");
}
2.创建增强类(编写增强逻辑)
在增强类里面,创建方法,让不同方法代表不同通知类型
//增强类
public class UserProxy {
//前置通知
public void before() {
System.out.println("before...");
}
}
3.进行通知的配置
(1) 在spring配置文件中,开启注解扫描
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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">
<!--开启注解扫描-->
<context:component-scan base-package="org.example.spring5.aop"></context:component-scan>
</beans>
(2) 使用注解创建User和UserProxy对象
@Component
public class User {}
@Component
public class UserProxy {}
(3) 在增强类上面添加注解@Aspect
@Aspect //生成代理对象
@Component
public class UserProxy {}
(4) 在spring配置文件中开启生成代理对象
<!--开启AspectJ生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
4.配置不同类型的通知
(1) 在增强类的里面,在作为通知方法方法上面添加通知类型注解,使用切入点表达式配置
//增强类
@Aspect //生成代理对象
@Component
public class UserProxy {
//前置通知
//@Before注解表示作为前置通知
@Before(value = "execution(* org.example.spring5.aop.User.add(..))")
public void before() {
System.out.println("before...");
}
@After(value = "execution(* org.example.spring5.aop.User.add(..))")
public void after() {
System.out.println("after...");
}
@AfterReturning(value = "execution(* org.example.spring5.aop.User.add(..))")
public void afterReturning() {
System.out.println("afterReturning");
}
//异常通知,只有add()方法中有异常才执行
@AfterThrowing(value = "execution(* org.example.spring5.aop.User.add(..))")
public void afterThrowing() {
System.out.println("afterThrowing");
}
//环绕通知
@Around(value = "execution(* org.example.spring5.aop.User.add(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕之前");
proceedingJoinPoint.proceed();
System.out.println("环绕之后");
}
}
5.相同的切入点切取
//相同切入点抽取
@Pointcut(value = "execution(* org.example.spring5.aop.User.add(..))")
public void pointcut() {
}
//前置通知
//@Before注解表示作为前置通知
@Before(value = "pointcut()")
public void before() {
System.out.println("before...");
}
6.有多个增强类对同一个方法进行增强, 设置增强类的优先级
在增强类上面添加注解(@Order(数字类型值),数字类型值越小,优先级越高)
三、AspectJ配置文件方式实现AOP操作(了解即可,不常用)
1.创建两个类,增强类和被增强类,创建方法
public class Book {
public void buy() {
System.out.println("buy....");
}
}
public class BookProxy {
public void before() {
System.out.println("before");
}
}
2.在spring配置文件中创建两个类的对象
<bean id="book" class="org.example.spring5.aopxml.Book"></bean>
<bean id="bookProxy" class="org.example.spring5.aopxml.BookProxy"></bean>
3.在spring配置文件中配置切入点
<!--配置aop增强-->
<aop:config>
<!--切入点-->
<aop:pointcut id="p" expression="execution(* org.example.spring5.aopxml.Book.buy())"/>
<!--配置切面-->
<aop:aspect ref="bookProxy">
<!--配置增强作用在具体的方法上-->
<aop:before method="before" pointcut-ref="p"></aop:before>
</aop:aspect>
</aop:config>
7.完全使用注解开发
创建配置类,不需要创建xml配置文件