Spring Framework learning (3.3) Based on AOP @AspectJ annotation

Foreword

Record learning process, then the first two
xml-based configuration file AOP implementation will inevitably face a bloated xml file of the case, and the configuration process is cumbersome, Annotation annotation technology can solve these problems

text

AOP implementations provides a set of annotations Annotation

  • @AspectJ: defining aspects
  • @Pointcut: define a starting point, the name of the entry point is defined by one aspect of the name
  • @Before: used to define a pre-notification
  • @AfterReturning: used to define a post-notification (notification return)
  • @AfterThrowing: used to define an exception notice
  • @Around: used to define a circumferential notification

Example: Rewrite variety of notification in the first two notes prepared by the method
(1) modify the log notification class LogAdvice
the @Aspect defining aspects
@Pointcut define an entry point, entry point name 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) modify the configuration file

<?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) Test
(individually tested pre-notification)
Here Insert Picture Description
Note: This issue may occur

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

This is the failure to create a Bean
is probably the wrong name or comment pack the wrong
I have such problems is the wrong version of the package, check for a long time found the package version too far behind, while the other package is the latest version of Spring. . . .
Here Insert Picture Description
The solution is to go to the official website to download the latest package
Here Insert Picture Description

Released eight original articles · won praise 0 · Views 153

Guess you like

Origin blog.csdn.net/key_768/article/details/103940943