SpringMVC record I encountered pits _AOP annotation is invalid section does not perform the resolution

  • 2021-11-01 03:30:28
  • OfStack

AOP annotation is invalid, section is not executed resolution

Want to make an api request log, think of using aop, encounter a pit in the configuration process, aop doesn't work,

My aop looks like this:


package com.ljwm.ibei.aspact; 
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; 
import javax.servlet.http.HttpServletRequest;
 
/**
 * Created by user on 2017/9/8.
 */
@Aspect
@Configuration
public class ApiRequestLog { 
    private Logger _log = LoggerFactory.getLogger(ApiRequestLog.class); 
    @Around("execution(* com.ljwm.ibei.controller.*.*(..))")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
 
        String url = request.getRequestURL().toString();
        String method = request.getMethod();
        String uri = request.getRequestURI();
        String queryString = request.getQueryString();
        _log.debug(" Request start ,  Parameters , url: {}, method: {}, uri: {}, params: {}", url, method, uri, queryString);
        Object result = pjp.proceed();
        return result;
    }
}

Configuration files are divided into applicationContext. xml, applicationContext-mvc. xml, mybatis and shiro

web. xml configuration:


<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
  <display-name>Archetype Created Web Application</display-name>
 
  <filter>
    <filter-name>shiroFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
      <param-name>targetFilterLifecycle</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>shiroFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <servlet>
    <servlet-name>spring-dispatcher</servlet-name>
    <servlet-class>
      org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring/applicationContext-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext*.xml</param-value>
  </context-param>
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
  <listener>
    <listener-class>com.ljwm.ibei.listener.ContextFinalizer</listener-class>
  </listener>
</web-app>

I put < aop:aspectj-autoproxy proxy-target-class="true"/ > Write it in applicationContext and put it < context:component-scan > And < mvc:annotation-driven > Write in applicationContext-mvc, and found that aop did not execute. Later, after trying to find out, because the configuration of mvc was loaded when initializing DispatchServlet, but the proxy of aop was not loaded, which made it unable to execute. I guess it is because spring defaults to bean as a singleton.

The initialized bean container is not in the follow-up processing, because it is the first loading mvc, so it is aop failure, I put the aop agent into mvc, aop can be executed, do not know my guess is right, but also hope that the great God

Another question:

springmvc using aop section in controller layer was not solved successfully

Need to add in the configuration file


<aop:aspectj-autoproxy proxy-target-class="true" />

Related articles: