Spring Aop简单入门学习(三)-基于注解的AOP配置

本文主要是对于spring aop简单入门做一些介绍,并不深入了解,只接触表面,对一些较复杂的内容也不过多描述。如文中有错误之处,望不吝赐教,谢谢~

通过注解可以减少工作量,已提高效率,这里通过一个实例来简单介绍一下有关aop的注解。

一、实例

本次实例在之前的文章Spring AOP简单入门学习(二)-常用通知类型里面的项目的基础上进行展开。

(1)修改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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

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

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


</beans>


(2)在UserServiceImpl类上添加注解

@Service("userService")

(3)修改Logger类

package com.example.utils;

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

@Component("logger")   //交给spring容器处理
@Aspect  //表示其是一个切面类
public class Logger {

    //指定切入点表达式
    @Pointcut("execution(* com.example.service.impl.*.*())")
    private void pt(){}

    /**
     * 前置通知
     */
    @Before("pt()")
    public void beforePrintLog(){
        System.out.println("打印日志-前置通知");
    }

    /**
     * 后置通知
     */
    @AfterReturning("pt()")
    public void afterReturningPrintLog(){
        System.out.println("打印日志-后置通知");
    }

    /**
     * 异常通知
     */
    @AfterThrowing("pt()")
    public void afterThrowingPrintLog(){
        System.out.println("打印日志-异常通知");
    }

    /**
     * 最终通知
     */
    @After("pt()")
    public void afterPrintLog(){
        System.out.println("打印日志-最终通知");
    }   
}

(4)执行AopTest.java,结果如下:
在这里插入图片描述
测试结果没错,但有个问题:后置通知在最终通知后面,显然是不对的。

(5)为了解决上述问题,可采用环绕通知,因为环绕通知可自定义通知的执行顺序,修改Logger.java

package com.example.utils;

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

@Component("logger")   //交给spring容器处理
@Aspect  //表示其是一个切面类
public class Logger {

    //指定切入点表达式
    @Pointcut("execution(* com.example.service.impl.*.*())")
    private void pt(){}

    /**
     * 环绕通知
     */
    @Around("pt()")
    public Object aroundPrintLog(ProceedingJoinPoint pjp){

        Object returnValue=null;

        try {
            Object[] args=pjp.getArgs();
            System.out.println("打印日志-环绕通知-前置通知");
            returnValue=pjp.proceed(args);
            System.out.println("打印日志-环绕通知-后置通知");
            return returnValue;

        }catch (Throwable throwable) {
            System.out.println("打印日志-环绕通知-异常通知");
            throwable.printStackTrace();
            throw new RuntimeException(throwable);
        }finally {
            System.out.println("打印日志-环绕通知-最终通知");
        }


    }
}

运行接结果如下:
在这里插入图片描述
此时通知顺序正确。

二、总结

aop用到的注解如下:

  • 实例化到spring容器中
@Component()
  • 定义切面类
@Aspect
  • 指定切入点表达式
@Pointcut
  • 指定五种通知类型
@Before   //前置通知
@AfterReturning    //后置通知
@AfterThrowing     //异常通知
@After    //最终通知
@Around    //环绕通知

在这里插入图片描述
2020.03.21

发布了82 篇原创文章 · 获赞 91 · 访问量 9499

猜你喜欢

转载自blog.csdn.net/ataraxy_/article/details/105009151
今日推荐