spring03-aop之注解配置,前置通知,后置通知,异常通知,最终通知, 环绕通知

配置spring开启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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.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">

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

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

</beans>

将业务类和日志类加入容器中:

package com.hr.service.impl;

import com.hr.service.IAccountService;
import org.springframework.stereotype.Service;

@Service("accountService")
public class AccountServiceImpl implements IAccountService {
    public void saveAccount() {
        System.out.println("执行了保存");
//        int i = 1 /0;
    }

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

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

Logger:

package com.hr.service.utils;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 日志工具类
 */

@Component("logger")
@Aspect
public class Logger {

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


    /**
     * 打印日志, 计划让其在切入点方法执行之前执行(切入点方法就是要被增强的业务层方法)
     * 前置通知
     */
    @Before("pt1()")
    public void beforPrintLog() {
        System.out.println("前置通知Logger类中的beforPrintLog方法开始记录日志了");
    }

    /**
     * 后置通知
     */
    @AfterReturning("pt1()")
    public void afterReturnPrintLog() {
        System.out.println("后置通知Logger类中的beforPrintLog方法开始记录日志了");
    }

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

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

    /**
     * 环绕通知
     * 问题:
     *  当配置了环绕通知之后,切入点方法没有执行,而通知方法执行了
     * 分析:
     *  通过对比动态代理中环绕通知的代码,发现动态代理的环绕通知有明确的切入点方法调用,而我们的代码没有
     *  解决:
     *      Spring框架为我们提供了一个接口,ProceedingJoinPoint, 该接口有一个方法proceed(),此方法就相当于明确调用切入点方法
     *      该接口可以作为环绕通知的方法参数, 在程序执行时,spring会为我们提供该接口的实现类供我们使用
     * spring环绕通知作用:
     *      它是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 throwable) {
            System.out.println("Logger类中的aroundPrintLog方法开始记录日志了... 异常");
            throw new RuntimeException(throwable);
        }finally {
            System.out.println("Logger类中的aroundPrintLog方法开始记录日志了... 最终");
        }

    }


}

 注意: 四种通知和环绕通知不能一起实现

测试类:

package com.hr.text;


import com.hr.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AopTest {

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService iAccountService = ac.getBean("accountService",IAccountService.class);

        iAccountService.saveAccount();
    }
}

接上一讲xml配置AOP, 注解配置相对简单,在对要织入的Logger类中方法上写各个通知类型即可, Spring对注解上面的命名很规范,但还是要注意切入点表达式的书写

github: https://github.com/2402zmybie/spring03_05annotationAop

 

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