SpringMVC記錄我遇到的坑_AOP註解無效,切面不執行的解決

AOP註解無效,切面不執行的解決

想做一個api請求日志,想到使用aop,配置過程中遇到瞭一個坑,aop不起作用,

我的aop是這樣的:

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("請求開始, 各個參數, url: {}, method: {}, uri: {}, params: {}", url, method, uri, queryString);
        Object result = pjp.proceed();
        return result;
    }
}

配置文件分成applicationContext.xml、applicationContext-mvc.xml還有mybatis和shiro的

web.xml配置:

<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>

我把<aop:aspectj-autoproxy proxy-target-class=”true”/>寫在applicationContext中,把<context:component-scan> 和 <mvc:annotation-driven>寫在applicationContext-mvc中,發現aop沒有執行,後來經過嘗試發現,因為在初始化DispatchServlet的時候加載瞭mvc的配置,但是aop的代理卻沒有加載,導致其不能執行,我猜測是因為spring默認bean是單例的,

對已經初始化的bean容器不在做後續處理,由於是先加載的mvc所以是aop失效,我把aop代理的那個放到mvc中,aop就能執行瞭,不知道我的猜測對不對,還望大神賜教

另一個問題:

springmvc在controller層使用aop切面不成功解決

需要在配置文件中加入

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

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: