基于注解的AOP配置

基于注解的AOP配置(一定要要参考上一篇的“基于xml文件的AOP配置”)

  • 主要是将上面的xml文件的内容替换掉,以下是xml文件所有内容,环绕通知的内容就是替换四种通知即可

    <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"
           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">
        
        <bean id="wordsService" class="com.mypro.service.impl.WordsServiceImpl"></bean>
        
        <bean id="logger" class="com.mypro.service.impl.WordsServiceImpl"></bean>
    <aop:config>
        <aop:aspect id="loggerAdvice" ref="logger">
            <aop:pointcut id="pt1" expression="execution(* com.mypro.service.impl.*.*(..))"></aop:point>
            <aop:before method="beforeInfo" pointcut-ref="pt1"></aop:before>
            <aop:after-returning method="afterReturningInfo" pointcut-ref="pt1"></aop:after-returning>
            <aop:after-throwing method="afterThrowingInfo" pointcut-ref="pt1"></aop:after-throwing>
            <aop:after method="afterInfo" pointcut-ref="pt1"></aop:after>
        </aop:aspect>
    </aop:config>
    </beans>
  • 首先我们需要配置我们扫描的包,将xml文件的Bean对象替换成注解,即在我们实现类上方添加注解。在此之前,还需要添加xmlns:context依赖

    <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">
        <!-- 代替xml文件的两个Bean对象的配置 -->
        <context:component-scan base-package="com.mypro"></context:component-scan>
    </beans>
  • 开启Spring支持的AOP配置

    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  • 修改通知类的代码,注意实现五种通知标签的功能

    package com.mypro.utils;
    ​
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    ​
    /**
     * 用于记录日志的工具类,里面提供公共的代码
     */
    // 代替xml文件的<aop:config><aop:aspect id="loggerAdvice" ref="logger"></aop:aspect></aop:config>
    @Component("logger")
    @Aspect                      
    public class Logger {
        
        // 代替切入点的配置
        @Pointcut("execution(* com.mypro.service.impl.*.*(..))")
        public void pt1(){}
    ​
        @Before("pt1()")
        public void beforeInfo(){
            System.out.println("前置通知");
        }
        @AfterReturning("pt1()")
        public void totalInfo(){
            System.out.println("后置通知");
        }
        @AfterThrowing("pt1()")
        public void expectInfo(){
            System.out.println("异常通知");
        }
        @After("pt1()")
        public void afterInfo(){
            System.out.println("最终通知");
        }
        
        @Around("pt1()")
        public Object aroundInfo(){
            Object rtValue = null;
            try{
                System.out.println("前置通知");
                // 得到方法执行时所需的参数
                Object[] args = pjp.getArgs();
                // 明确调用业务层方法(切入点方法)
                rtValue = pjp.proceed(args);
                System.out.println("后置通知");
                return rtValue;
            }catch(Throwable t){      // 必须是用Throwable捕捉异常
                System.out.println("异常通知");
                throw new RuntimeException(t);
            }finally{
                System.out.println("最终通知");
            }
        }
    ​
    }
    ​