Spring framework learning (3.3) AOP implementation based on @ AspectJ annotation

Keywords: Java Spring xml encoding


Record the learning process, continued from the previous two sections
The implementation of AOP based on xml configuration file inevitably faces the situation of overstaffed xml file, and the configuration process is troublesome. Annotation annotation technology can solve these problems


AOP implementation provides a set of Annotation annotations

  • @AspectJ: define a tangent
  • @Pointcut: defines a pointcut whose name is defined by an aspect name
  • @Before: used to define a pre notification
  • @AfterReturning: used to define a post notification (return notification)
  • @AfterThrowing: used to define an exception notification
  • @Around: used to define a surround notification

Example: using annotation method to rewrite multiple notifications written in the first two sections
(1) Modify log notification class LogAdvice
@Aspect defines a facet
@Pointcut defines a pointcut whose name is 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;
public class LogAdvice {
    @Pointcut("execution(* com.BeforeAdvice.Service.MealService.*(..))")
    private void allMethod(){}
    //This method will be used as a pre notification
    public void MyBeforeAdvice(JoinPoint joinpoint){
        //Get business method parameters
        List<Object> list= Arrays.asList(joinpoint.getArgs());
        //Log format string
        String logInfo="Advance notice:"+
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+
                " "+list.get(0).toString()+" Browse products: "+list.get(1).toString();
    //This method will return notifications for
    public void MyAfterReturnAdvice(JoinPoint afterjoinPoint){
        List<Object> list= Arrays.asList(afterjoinPoint.getArgs());
        //Log format string
        String logInfo="Return notification:"+
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+
                " "+list.get(0).toString()+" Browse products: "+list.get(1).toString();
    //This method is exception notification
    @AfterThrowing(pointcut = "allMethod()")
    public void MyThrowingAdvice(JoinPoint throwingPoint){
        //Get the name of the called class
        String targetClassName=throwingPoint.getTarget().getClass().getName();
        //Get the called method name
        String targetMethodName=throwingPoint.getSignature().getName();
        //Log format string
        String logInfo="Exception notification: Execution"+targetClassName+"Class"+
                targetMethodName+"Exception in method";
    //This method is a circular notification
    public void MyAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        long beginTime=System.currentTimeMillis();
        long endTime=System.currentTimeMillis();
        //Get the called method name
        String targetMethodName=proceedingJoinPoint.getSignature().getName();
        //Log format string
        String logInfo="Surround notification:"+targetMethodName+"Time before method call"+
                beginTime+"Millisecond,"+"Time after call"+endTime+"Millisecond";

(2) Modify profile

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!--Configure the base package for the specified scan-->
    <context:component-scan base-package="com"></context:component-scan>

    <!--Open on@AspectJFaceted annotation processor-->


(3) Testing
(pre notice tested separately)

Note: this problem 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 a failure to create a Bean
Maybe the comment name is wrong or the package is wrong
My problem is that the version of the package is wrong. After a long time of checking, I found that the version of the package is too backward, while other Spring packages are the latest version....

The solution is to go Download the latest package on the official website

Published 8 original articles, won praise 0, visited 141
Private letter follow

Posted by assgar on Sun, 12 Jan 2020 01:08:31 -0800