Spring5(12)- 基于注解的AOP配置

1 Service

@Service("accountService")
public class AccountServiceImpl implements IAccountService {

    @Override
    public void saveAccount() {
        System.out.println("执行了保存");
    }

    @Override
    public void updateAccount(int i) {
        System.out.println("执行了更新 " + i);
    }

    @Override
    public int deleteAccount() {
        System.out.println("执行了删除");
        return 0;
    }
}

2 bean.xml

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->

    <!--配置spring创建容器时需要扫描的包-->
    <context:component-scan base-package="com.tzb"></context:component-scan>

    <!--配置 spring 开启注解 AOP的支持-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    
</beans>

3 切面类

/**
 * 用于记录日志的工具类
 */
@Component("logger")
@Aspect // 表示当前类是一个切面类
public class Logger {

    @Pointcut("execution(* com.tzb.service.impl.*.*(..))")
    private void pt1(){

    }

    // 前置通知
    @Before("pt1()")
    public void beforePrintLog() {
        System.out.println("前置通知 Logger类中的 beforePrintLog 开始记录日志。。。");
    }

    // 后置通知
    @AfterReturning("pt1()")
    public void afterReturningPrintLog() {
        System.out.println("后置通知 Logger类中的  afterReturningPrintLog 开始记录日志。。。");
    }

    // 异常通知
    @AfterThrowing("pt1()")
    public void afterThrowingPrintLog() {
        System.out.println("异常通知 Logger类中的 afterThrowingPrintLog 开始记录日志。。。");
    }

    // 最终通知
    @After("pt1()")
    public void afterPrintLog() {
        System.out.println("最终通知 Logger类中的 afterPrintLog 开始记录日志。。。");
    }

    /**
     * 环绕通知
     * 问题:切入点方法没有执行,而通知方法执行了
     * <p>
     * 分析:
     * 通过对比动态代理中的环绕通知,发现动态代理的环绕通知有明确的切入点方法调用,我们的方法没有
     * <p>
     * 解决:
     * Spring 提供ProceedingJointPoint ,该接口有一个 proceed(),此方法就相当于明确调用切入点方法
     * 该接口可以作为环绕通知的方法参数,在程序执行时,spring会为我们提供该接口的实现类供我们使用
     */
    //@Around("pt1()")
    public Object aroundPrintLog(ProceedingJoinPoint pjp) {
        Object rtValue = null;
        // 明确调用业务层方法(切入点方法)
        try {
            Object[] args = pjp.getArgs();
            System.out.println("环绕通知 Logger类中的 aroundPrintLog 开始记录日志。。。前置");
            rtValue = pjp.proceed(args);
            System.out.println("环绕通知 Logger类中的 aroundPrintLog 开始记录日志。。。后置");

            return rtValue;
        } catch (Throwable t) {
            System.out.println("环绕通知 Logger类中的 aroundPrintLog 开始记录日志。。。异常通知");
            throw new RuntimeException(t);
        } finally {
            System.out.println("环绕通知 Logger类中的 aroundPrintLog 开始记录日志。。。最终通知");
        }
    }
}


  • 单元测试
public class AOPTest {
    public static void main(String[] args) {
        // 1.获取容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:bean.xml");
        // 2. 获取对象
        IAccountService as = (IAccountService) ac.getBean("accountService");
        // 3.执行方法
        as.saveAccount();
    }
}

  • 发现 后置通知 在 最终通知的前面
    在这里插入图片描述

  • 加入异常
    在这里插入图片描述
  • 最终通知 在 异常通知的前面
    在这里插入图片描述

3.1 切面类使用环绕通知

  • 建议如果使用注解的话,使用环绕通知

  • 切面类
/**
 * 用于记录日志的工具类
 */
@Component("logger")
@Aspect // 表示当前类是一个切面类
public class Logger {

    @Pointcut("execution(* com.tzb.service.impl.*.*(..))")
    private void pt1(){

    }

    // 前置通知
   // @Before("pt1()")
    public void beforePrintLog() {
        System.out.println("前置通知 Logger类中的 beforePrintLog 开始记录日志。。。");
    }

    // 后置通知
   // @AfterReturning("pt1()")
    public void afterReturningPrintLog() {
        System.out.println("后置通知 Logger类中的  afterReturningPrintLog 开始记录日志。。。");
    }

    // 异常通知
   // @AfterThrowing("pt1()")
    public void afterThrowingPrintLog() {
        System.out.println("异常通知 Logger类中的 afterThrowingPrintLog 开始记录日志。。。");
    }

    // 最终通知
    //@After("pt1()")
    public void afterPrintLog() {
        System.out.println("最终通知 Logger类中的 afterPrintLog 开始记录日志。。。");
    }

    /**
     * 环绕通知
     * 问题:切入点方法没有执行,而通知方法执行了
     * <p>
     * 分析:
     * 通过对比动态代理中的环绕通知,发现动态代理的环绕通知有明确的切入点方法调用,我们的方法没有
     * <p>
     * 解决:
     * Spring 提供ProceedingJointPoint ,该接口有一个 proceed(),此方法就相当于明确调用切入点方法
     * 该接口可以作为环绕通知的方法参数,在程序执行时,spring会为我们提供该接口的实现类供我们使用
     */
    @Around("pt1()")
    public Object aroundPrintLog(ProceedingJoinPoint pjp) {
        Object rtValue = null;
        // 明确调用业务层方法(切入点方法)
        try {
            Object[] args = pjp.getArgs();
            System.out.println("环绕通知 Logger类中的 aroundPrintLog 开始记录日志。。。前置");
            rtValue = pjp.proceed(args);
            System.out.println("环绕通知 Logger类中的 aroundPrintLog 开始记录日志。。。后置");

            return rtValue;
        } catch (Throwable t) {
            System.out.println("环绕通知 Logger类中的 aroundPrintLog 开始记录日志。。。异常通知");
            throw new RuntimeException(t);
        } finally {
            System.out.println("环绕通知 Logger类中的 aroundPrintLog 开始记录日志。。。最终通知");
        }
    }
}

在这里插入图片描述

4 不使用 xml 的配置方法

在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章