前篇
AOP(概念)
-
什么是AOP?
(1) 面向切面编程,利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑的各个部分耦合度降低,提高程序的可重用性,同时也提高了代码的开发效率。
(2)通俗的讲:就是在不修改源代码的情况下,在主干功能中添加新的功能。
(3)使用登录的例子说明AOP:
AOP(底层原理)
1.AOP底层使用动态代理
(1)有两种情况的动态代理:
第一种 有接口情况,使用JDK动态代理
核心:创建接口实现类的代理对象,增强类的方法
第二种 没有接口的情况,使用CGLIB动态代理
核心:创建当前类子类的代理对象,增强类的方法
AOP(JDK动态代理)
- 使用JDK动态代理,使用Proxy类里面的方法创建代理对象
(1) 调用Proxy类里面的静态方法newProxyInstance方法
该方法有三个参数:
第一个参数:类加载器
第二个参数:要增强方法类中实现的接口
第三个参数:实现InvocationHandler接口的代理对象(要在该代理对象中写方法的增强)
2. 编写JDK动态代理的代码
(1)创建接口,定义方法
public interface UserDao {
void update(int a);
}
(2)创建接口的实现类,实现方法
public class UserDaoImpl implements UserDao{
@Override
public void update(int a) {
System.out.println("update..." + a);
}
}
(3)使用Proxy类创建上述接口的代理对象
public class JDKProxy {
public static void main(String[] args) {
Class[] interfaces = {UserDao.class};
UserDaoImpl userDao = new UserDaoImpl();
UserDao dao = (UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new UserDaoProxy(userDao));
dao.update(2);
}
}
(4)代理对象中增强的部分
class UserDaoProxy implements InvocationHandler {
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.invoke(obj,args);
System.out.println("调用方法后...");
return obj;
}
}
AOP(术语)
-
连接点
类里面哪些方法可以被增强,这些方法称为连接点
-
切入点
实际被真正增强的方法,称为切入点
-
通知(增强)
实际增强的逻辑部分称为通知(增强)
通知有多种类型:
前置通知 后置通知 环绕通知 异常通知 最终通知
4. 切面
是动作
把通知应用到切入点的过程
AOP操作(准备工作)
-
Spring框架一般都是基于AspectJ来实现AOP的操作
AspectJ不是Spring的组成部分,是独立的AOP框架。 一般把AspectJ和Spring框架一起使用,进行AOP操作 -
基于AspectJ实现AOP操作
1)基于XML配置文件方式实现
2)基于注解的方式实现(常用)
-
在项目工程中引入AOP依赖
4. 切入点表达式
1)切入点表达式的作用:知道对哪个类的哪个方法进行增强
2)语法结构:execution([权限修饰符] [返回值类型] [类的全路径] [方法名] ([参数列表]))
举例1:对com.atguigu.dao.UserDao的add()方法进行增强
execution(* com.atguigu.dao.UserDao add(..))
举例2:对com.atguigu.dao.UserDao的所有方法进行增强
execution(* com.atguigu.dao.UserDao *(..))
举例3:对com.atguigu.dao包中的所有类所有方法进行增强
execution(* com.atguigu.dao.* *(..))
AOP操作(AspectJ 注解)
- 创建类,在类里面定义方法
被增强的类
public class User {
public void add(){
System.out.println("add...");
}
}
-
创建增强类(编写增强逻辑)
(1) 在增强类里面,编写增强方法,让不同的方法代表不同的通知类型
//增强的类
public class UserProxy {
public void before() {//前置通知
System.out.println("before......");
}
}
-
进行通知的设置
(1)在Spring文件中,开启注解扫描,添加context和aop的名称空间
<?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="com.atguigu.spring5.aopanno"></context:component-scan>
(2)使用注解创建User和UserProxy的对象
@Component
public class User {
@Component
public class UserProxy {
(3)在增强类上面添加注解@Aspect
@Component
@Aspect //生成代理对象
public class UserProxy {
(4)在Spring配置文件中开启生成代理对象
<!-- 开启Aspect生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
- 配置不同类型的通知
/**
* @author crystal
* @create 2023-01-30 13:30
*/
// 增强类
@Component
@Aspect
public class UserProxy {
// 前置通知
@Before(value = "execution(* com.atguigu.aopanno.User.add(..))")
public void before(){
System.out.println("before...");
}
// 后置通知
@AfterReturning(value = "execution(* com.atguigu.aopanno.User.add(..))")
public void afterReturning(){
System.out.println("afterReturning...");
}
// 最终通知
@After(value = "execution(* com.atguigu.aopanno.User.add(..))")
public void after(){
System.out.println("after...");
}
// 异常通知
@AfterThrowing(value = "execution(* com.atguigu.aopanno.User.add(..))")
public void afterThrowing(){
System.out.println("afterThrowing...");
}
// 环绕通知
@Around(value = "execution(* com.atguigu.aopanno.User.add(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕之前...");
proceedingJoinPoint.proceed();
System.out.println("环绕之后...");
}
}
- 相同的切入点抽取
@Pointcut(value = "execution(* com.atguigu.aopanno.User.add(..))")
public void publicCut(){
}
// 前置通知
@Before(value = "publicCut()")
public void before(){
System.out.println("before...");
}
// 后置通知
@AfterReturning(value = "publicCut()")
public void afterReturning(){
System.out.println("afterReturning...");
}
-
当有多个增强类对同一个类中的同一个方法进行增强时,设置增强类的优先级
(1)在增强烈上使用注解@Order(数值类型)来设置优先级,数字越小,优先级越高
// 增强类
@Component
@Aspect
@Order(2)
public class UserProxy {
@Pointcut(value = "execution(* com.atguigu.aopanno.User.add(..))")
public void publicCut(){
}
@Component
@Aspect
@Order(1)
public class PersonProxy {
@Before("execution(* com.atguigu.aopanno.User.add(..))")
public void before(){
System.out.println("PersonProxy...");
}
}
7. AOP-完全注解开发
(1)创建配置类,不需要创建XML配置文件
@Configuration
@ComponentScan(basePackages = {"com.atguigu.aopanno"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class ConfigAop {
}
(2)测试
@Test
public void test1(){
ApplicationContext context =
new AnnotationConfigApplicationContext(ConfigAop.class);
User user = context.getBean("user",User.class);
user.add();
}
AOP操作(AspectJ XML配置文件实现)
- 创建两个类,增强类和被增强类,并创建方法
public class Book {
public void buy(){
System.out.println("buy...");
}
}
public class BookProxy {
public void before(){
System.out.println("before...");
}
}
- 在Spring配置文件中创建两个类的对象
<bean id="book" class="com.atguigu.aopxml.Book"></bean>
<bean id="bookProxy" class="com.atguigu.aopxml.BookProxy"></bean>
- 在Spring配置文件中配置切入点
<aop:config>
<!-- 切入点-->
<aop:pointcut id="p" expression="execution(* com.atguigu.aopxml.Book.buy())"/>
<!-- 切面-->
<aop:aspect ref="bookProxy">
<aop:before method="before" pointcut-ref="p"></aop:before>
</aop:aspect>
</aop:config>