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 t.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.t.request.RequestAttributes;
import org.t.request.RequestContextHolder;
import org.t.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
/**
* Created by user on 2017/9/8.
*/
@Aspect
@Configuration
public class ApiRequestLog {
private Logger _log = Logger(ApiRequestLog.class);
@Around("execution(* com.ller.*.*(..))")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
RequestAttributes ra = RequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
HttpServletRequest request = Request();
String url = RequestURL().toString();
String method = Method();
String uri = RequestURI();
String queryString = QueryString();
_log.debug("请求开始, 各个参数, url: {}, method: {}, uri: {}, params: {}", url, method, uri, queryString);
Object result = pjp.proceed();
return result;
}
}
配置⽂件分成l、l还有mybatis和shiro的
<web-app xmlns="java.sun/xml/ns/javaee" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="java.sun/xml/ns/javaee
java.sun/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>
springmvc选择题
<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:l</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.t.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.t.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" />
以上为个⼈经验,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。