菜鸟学习7-19 Spring的AOP 注解配置

AOP基于注解的配置

XML文件配置:
首先需要在约束依赖里开启context,即注解
xmlns:context=“http://www.springframework.org/schema/context”
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd

其次配置好当spring创建容器时需要扫描的包
最后开启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.itheima"></context:component-scan>

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

在通知类中,首先通知类不属于MVC三层架构中的任意一层,所以我们用@Component声明该类需要注册一个Bean,而不是@Service、@Controller或者是@Repository。
其次需要用到@Aspect来声明该类是一个切面类,即通知类。
常用的通知类型对应的注解:
前置通知:@Before("")
后置通知:@AfterReturning("")
异常通知:@AfterThrowing("")
最终通知:@After("")
环绕通知:@Arround("")
他们均有一个参数,需要像XML配置一样输入切入点表达式:execution(* com.itheima.service.impl..(…))
同样为了解耦和增加代码的可复用性,我们使用一个方法private void pt1(){},并给它加上注解@Pointcut(“execution(* com.itheima.service.impl..(…))”)来用pt1代替表达式。注意,应该使用pt1(),带括号,否则会报错!
完成之后的代码如下:

@Component("logger")
@Aspect//表示当前类是一个切面类
public class Logger {

    @Pointcut("execution(* com.itheima.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类中的afteringprintLog方法开始记录日志了。。。");
    }

    //@Around("pt1()")
    //这里先把环绕通知注释上,因为环绕通知和上面4种通知是重复的。当然把上面四种注释掉,测试环绕绕通知结果也是一样的。
    public Object aroundPrintLog(ProceedingJoinPoint pjp){
        Object rtValue = null;
        try {
            Object[] arjs = pjp.getArgs();//得到方法执行所需的参数

            System.out.println("Logger类中的aroundPrintLog方法开始记录日志了。。。前置");

            rtValue = pjp.proceed();//明确调用业务层(切入点)方法

            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方法开始记录日志了。。。最终");
        }
    }
}

输出结果:
注解配置AOP输出结果
注意:如果使用稍旧版本spring,可能导致后置通知和最终通知顺序出现误差。当前使用spring版本为5.2.7 RELEASE,为当前最新版本(2020.7.19),已经修复该问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值