SpringMVC记录我遇到的坑_AOP注解无效,切面不执行的解决

网友投稿 308 2022-10-11


SpringMVC记录我遇到的坑_AOP注解无效,切面不执行的解决

AOP注解无效,切面不执行的解决

想做一个api请求日志,想到使用aop,配置过程中遇到了一个坑,aop不起作用,

我的aop是这样的:

package com.ljwm.ibei.aspact;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.anVVtbqmnnotation.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配置:

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

Archetype Created Web Application

shiroFilter

targetFilterLifecycle

<param-value>true

shiroFilter

/*

spring-dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/applicationContext-mvc.xml

1

spring-dispatcher

/

contextConfigLocation

classpath:spring/applicationContext*.xml

characterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

characterEncodingFilter

/*

org.springframework.web.context.ContextLoaderListener

org.springframework.web.context.request.RequestContextListener

com.ljwm.ibei.listener.ContextFinalizer

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

version="3.0">

Archetype Created Web Application

shiroFilter

targetFilterLifecycle

<param-value>true

shiroFilter

/*

spring-dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/applicationContext-mvc.xml

1

spring-dispatcher

/

contextConfigLocation

classpath:spring/applicationContext*.xml

characterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

characterEncodingFilter

/*

org.springframework.web.context.ContextLoaderListener

org.springframework.web.context.request.RequestContextListener

com.ljwm.ibei.listener.ContextFinalizer

我把写在applicationContext中,把写在applicationContext-mvc中,发现aop没有执行,后来经过尝试发现,因为在初始化DispatchServlet的时候加载了mvc的配置,但是aop的代理却没有加载,导致其不能执行,我猜测是因为spring默认bean是单例的,

对已经初始化的bean容器不在做后续处理,由于是先加载的mvc所以是aop失效,我把aop代理的那个放到mvc中,aop就能执行了,不知道我的猜测对不对,还望大神赐教

另一个问题:

springmvc在controller层使用aop切面不成功解决

需要在配置文件中加入


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:ncat使用总结(nc使用方法)
下一篇:基于kali2018.1 64位安装nessus7.0.2(基于技能的公司通常有)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~