SpringBoot(10.1) -- AOP

发布时间 2023-05-16 23:26:59作者: 天晴修屋顶

SpringBoot的AOP的2种实现方式

  1. 基于Spring的AOP写法

spring.aop.auto=true # 是否启用aop
spring.aop.proxy-target-class=false # 代理方式有接口使用jdk动态代理,如果没有接口使用cglib代理
  1. 基于SpringBoot做法 @EnableAopProxyClass

exposeProxy属性表示如果使用true就可以使用AopContext对象获取当前代理对象,false则不能使用
proxyTargetClass true表示使用jdk的动态代理, false表示使用cglib代理
 引入SpringBoot的AOP依赖spring-boot-starter-aop
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.11</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <groupId>com.mike.study</groupId>
  <artifactId>springboot-aop</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>springboot-aop</name>
  <description>Demo project for Spring Boot</description>

  <properties>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

</project>

  

第一种方式:Spring的AOP写法
类添加@Aspect注解,指定切入的方法点,然后就可以在方法点前后添加@Before和@After方法。
假设有个BookDaoImpl.java如下
import org.springframework.stereotype.Repository;

/**
 * @Classname BookDaoImpl
 * @Created by Michael
 * @Date 2023/5/16
 * @Description Book操作类
 */

@Repository
public class BookDaoImpl implements BookDao {
  @Override
  public void readBook(String name, String authod) {
    System.out.println("书名:"+name+",作者:"+authod);
  }
}
 
现在对readBook()方法增强,添加PointCut.java,定义切入点POINT_CUT ,这里是指定某个包下的所有类的所有方法,分别在方法的前后打印信息
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.aspectj.lang.annotation.Aspect;

import java.util.Arrays;

/**
 * @Classname PointCut
 * @Created by Michael
 * @Date 2023/5/16
 * @Description AOP增强类
 */
@Component
@Aspect
public class PointCut {
  // 定义切入点
  public static final String POINT_CUT = "execution(* com.mike.study.springbootaop.dao..*.*(..))";

  @Before(POINT_CUT)
  public void beforeReadBook(){
    System.out.println("+++++++++++++++++++++++");
    System.out.println("浏览目录");
    System.out.println("--------------");
  }

  @After(POINT_CUT)
  public void afterReadBook(JoinPoint jp){
    System.out.println("--------------");
    System.out.println("整理读书笔记");
    System.out.println(jp.getTarget());
    System.out.println("参数:"+ Arrays.asList(jp.getArgs()));
  }
}

调用readBook()

import com.mike.study.springbootaop.dao.BookDao;
import com.mike.study.springbootaop.dao.BookDaoImpl;
import com.mike.study.springbootaop.dao.BookDaoImpl2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.awt.print.Book;

@SpringBootApplication
public class SpringbootAopApplication {

  public static void main(String[] args) {
    ConfigurableApplicationContext context = SpringApplication.run(SpringbootAopApplication.class, args);
    BookDao bookDao = context.getBean(BookDao.class);
    bookDao.readBook("《Java从入门到删库跑路》","不详");

    BookDaoImpl2 bookDao2 = context.getBean(BookDaoImpl2.class);
    bookDao2.readBook("《代码重构》","不详");
    context.close();
  }

}

查看输出结果