Spring框架学习(3.3)基于@AspectJ注解的AOP实现

前言

记录学习历程,上接前两节
基于xml配置文件的AOP实现免不了面临xml文件臃肿的情况,而且配置过程麻烦,Annotation注解技术能够解决这些问题

正文

AOP的实现提供了一套Annotation注解

  • @AspectJ:定义一个切面
  • @Pointcut:定义一个切入点,切入点的名称由一个方面名称定义
  • @Before:用于定义一个前置通知
  • @AfterReturning:用于定义一个后置通知(返回通知)
  • @AfterThrowing:用于定义一个异常通知
  • @Around:用于定义一个环绕通知

例:用注解法重写前两节中编写的多种通知
(1)修改日志通知类LogAdvice
@Aspect定义一个切面
@Pointcut定义一个切入点,切入点的名字叫allMethod()

package com.BeforeAdvice.Aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Controller;
import org.aspectj.lang.JoinPoint;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;
@Aspect
@Controller("logAdvice")
public class LogAdvice {
    @Pointcut("execution(* com.BeforeAdvice.Service.MealService.*(..))")
    private void allMethod(){}
    //此方法将作为前置通知
    @Before("allMethod()")
    public void MyBeforeAdvice(JoinPoint joinpoint){
        //获得业务方法参数
        List<Object> list= Arrays.asList(joinpoint.getArgs());
        //日志格式字符串
        String logInfo="前置通知:"+
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+
                " "+list.get(0).toString()+" 浏览商品: "+list.get(1).toString();
        System.out.println(logInfo);
    }
    //此方法将为返回通知
    @AfterReturning("allMethod()")
    public void MyAfterReturnAdvice(JoinPoint afterjoinPoint){
        List<Object> list= Arrays.asList(afterjoinPoint.getArgs());
        //日志格式字符串
        String logInfo="返回通知:"+
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+
                " "+list.get(0).toString()+" 浏览商品: "+list.get(1).toString();
        System.out.println(logInfo);
    }
    //此方法为异常通知
    @AfterThrowing(pointcut = "allMethod()")
    public void MyThrowingAdvice(JoinPoint throwingPoint){
        //获取被调用的类名
        String targetClassName=throwingPoint.getTarget().getClass().getName();
        //获取被调用的方法名
        String targetMethodName=throwingPoint.getSignature().getName();
        //日志格式字符串
        String logInfo="异常通知:执行"+targetClassName+"类的"+
                targetMethodName+"方法时发生异常";
        System.out.println(logInfo);
    }
    //此方法为环绕通知
    @Around("allMethod()")
    public void MyAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        long beginTime=System.currentTimeMillis();
        proceedingJoinPoint.proceed();
        long endTime=System.currentTimeMillis();
        //获得被调用的方法名
        String targetMethodName=proceedingJoinPoint.getSignature().getName();
        //日志格式字符串
        String logInfo="环绕通知:"+targetMethodName+"方法调用前时间"+
                beginTime+"毫秒,"+"调用后时间"+endTime+"毫秒";
        System.out.println(logInfo);
    }
}

(2)修改配置文件

<?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:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--配置指定扫描的基包-->
    <context:component-scan base-package="com"></context:component-scan>

    <!--开启基于@AspectJ切面的注解处理器-->
    <aop:aspectj-autoproxy/>

</beans>

(3)测试
(单独测试了前置通知)
在这里插入图片描述
注:可能出现这个问题

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘mealService’ defined in file [C:\Users\dddnkj\IdeaProjects\TestSpringAdvice\out\production\TestSpringAdvice\com\BeforeAdvice\Impl\MealServiceImpl.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 can’t find referenced pointcut allMethod

这是创建Bean失败
可能就是注释名错了或者包错了
我出现这个问题就是包的版本不对,查了半天发现这个包版本太落后了,而其他的Spring包都是最新版的。。。。
在这里插入图片描述
解决办法就是去官网下载最新的包
在这里插入图片描述

发布了8 篇原创文章 · 获赞 0 · 访问量 153

猜你喜欢

转载自blog.csdn.net/key_768/article/details/103940943