欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringMVC記錄我遇到的坑_AOP注解無效,切面不執(zhí)行的解決

 更新時(shí)間:2021年07月19日 11:30:42   作者:z781582206  
這篇文章主要介紹了SpringMVC記錄我遇到的坑_AOP注解無效,切面不執(zhí)行的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

AOP注解無效,切面不執(zhí)行的解決

想做一個(gè)api請(qǐng)求日志,想到使用aop,配置過程中遇到了一個(gè)坑,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("請(qǐng)求開始, 各個(gè)參數(shù), 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中,發(fā)現(xiàn)aop沒有執(zhí)行,后來經(jīng)過嘗試發(fā)現(xiàn),因?yàn)樵诔跏蓟疍ispatchServlet的時(shí)候加載了mvc的配置,但是aop的代理卻沒有加載,導(dǎo)致其不能執(zhí)行,我猜測(cè)是因?yàn)閟pring默認(rèn)bean是單例的,

對(duì)已經(jīng)初始化的bean容器不在做后續(xù)處理,由于是先加載的mvc所以是aop失效,我把a(bǔ)op代理的那個(gè)放到mvc中,aop就能執(zhí)行了,不知道我的猜測(cè)對(duì)不對(duì),還望大神賜教

另一個(gè)問題:

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

需要在配置文件中加入

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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于Listener監(jiān)聽器生命周期(詳解)

    基于Listener監(jiān)聽器生命周期(詳解)

    下面小編就為大家?guī)硪黄贚istener監(jiān)聽器生命周期(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • Spring系列中的beanFactory與ApplicationContext

    Spring系列中的beanFactory與ApplicationContext

    這篇文章主要介紹了Spring系列中的beanFactory與ApplicationContext,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • SpringSecurity跨域請(qǐng)求偽造(CSRF)的防護(hù)實(shí)現(xiàn)

    SpringSecurity跨域請(qǐng)求偽造(CSRF)的防護(hù)實(shí)現(xiàn)

    本文主要介紹了SpringSecurity跨域請(qǐng)求偽造(CSRF)的防護(hù)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Springboot?前后端分離項(xiàng)目使用?POI?生成并導(dǎo)出?Excel的操作方法

    Springboot?前后端分離項(xiàng)目使用?POI?生成并導(dǎo)出?Excel的操作方法

    在做一個(gè)?SpringBoot?前后端分離項(xiàng)目的時(shí)候,需要將數(shù)據(jù)存到?Excel中,用戶可以下載?Excel,具體實(shí)現(xiàn)是采用?Apache?強(qiáng)大的?POI,本文給大家介紹Springboot?前后端分離項(xiàng)目使用?POI?生成并導(dǎo)出?Excel相關(guān)知識(shí),感興趣的朋友一起看看吧
    2023-09-09
  • Java實(shí)現(xiàn)批量操作Excel的示例詳解

    Java實(shí)現(xiàn)批量操作Excel的示例詳解

    在操作Excel的場(chǎng)景中,通常會(huì)有一些針對(duì)Excel的批量操作,以GcExcel為例,為大家詳細(xì)介紹一下Java是如何實(shí)現(xiàn)批量操作Excel的,需要的可以參考一下
    2023-07-07
  • 關(guān)于JSCH使用自定義連接池的說明

    關(guān)于JSCH使用自定義連接池的說明

    這篇文章主要介紹了關(guān)于JSCH使用自定義連接池的說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Spring中的ApplicationRunner接口的使用詳解

    Spring中的ApplicationRunner接口的使用詳解

    這篇文章主要介紹了Spring中的ApplicationRunner接口的使用詳解,ApplicationRunner使用起來很簡(jiǎn)單,只需要實(shí)現(xiàn)CommandLineRunner或者ApplicationRunner接口,重寫run方法就行,需要的朋友可以參考下
    2023-11-11
  • Elasticsearch8.1中的Script使用實(shí)例深入解讀

    Elasticsearch8.1中的Script使用實(shí)例深入解讀

    這篇文章主要為大家介紹了Elasticsearch8.1中的Script使用實(shí)例深入解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • 深入淺出重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)

    深入淺出重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)

    通常來講,重構(gòu)是指不改變功能的情況下優(yōu)化代碼,但本文所說的重構(gòu)也包括了添加功能。這篇文章主要介紹了重構(gòu)Mybatis與Spring集成的SqlSessionFactoryBean(上)的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • Java并發(fā)系列之Semaphore源碼分析

    Java并發(fā)系列之Semaphore源碼分析

    這篇文章主要為大家詳細(xì)介紹了Java并發(fā)系列之Semaphore源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02

最新評(píng)論