开发者社区> 问答> 正文

SpringMVC中配置AOP拦截controller - java报错

小弟对spring aop不熟,现在有个项目需要记录日志,不想每个controller中去写,就想使用AOP,结果呢,配置后启动没错,但不出来

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd ">

	<!-- DispatcherServlet Context: defines this servlet's request-processing 
		infrastructure -->

	<!-- Scans within the base package of the application for @Components to 
		configure as beans -->
	<!-- @Controller, @Service, @Configuration, etc. -->
	<aop:aspectj-autoproxy proxy-target-class="true" />
	<mvc:resources location="/css/" mapping="/css/**" />
	<mvc:resources location="/img/" mapping="/img/**" />
	<mvc:resources location="/js/" mapping="/js/**" />
	<mvc:resources location="/json/" mapping="/json/**" />
	<mvc:resources location="/jquery-easyui-1.3.3/" mapping="/jquery-easyui-1.3.3/**" />
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize" value="100000000" />
	</bean>
</beans>
然后呢,AOP类
package com.*.windrunner.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ControllerLogAspect {

	@Before(value = "execution(* com.*.windrunner.controller.*(..))")
	public void beforeMethod(JoinPoint point) {
		System.out.println("------test aop before");
	}

}
这个里面因为有公司项目,所以把包名用*号代替了,见谅!!现在不输,求帮忙

展开
收起
montos 2020-05-30 11:15:27 560 0
1 条回答
写回答
取消 提交回答
  • controller里面无法直接这样切入的。需要切入

    execution(* org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(..))

    因为你controller注解的类,都被这个org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter给代理了。

    ######谢谢,我想请问下,还有没有其它的配置需要做呢?谢谢######

    我的功能和你类似,是这么写的

    /**
    * 异常日志处理
     * 
     * @param joinPoint
     * @param throwable
     */
    // 拦截语法
    // AbstractAction的子类被@RequestMapping注解的方法
    @Around("within(cn.org.sysu.cems.utils.superclass.AbstractAction+) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public ModelAndView handleError(ProceedingJoinPoint joinPoint) {
    主要是那个@annotation 如果光用execution的话可能会把你Controller本身的getter setter等非请求处理方法一并给拦截了

    ######请问我这个配置对吗?我没有配appalicationContext.xml,上面的XML是spring-servlet.xml######

    引用来自“汉唐”的答案

    controller里面无法直接这样切入的。需要切入

    execution(* org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(..))

    因为你controller注解的类,都被这个org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter给代理了。

    不需要了。现在还是无效?
    ######回复 @bill_ : 你看我的1楼回答,配置个AOP代理就可以了######回复 @张浩春 : 你好,请问是怎么解决的呢?我也遇到这个问题了。 springmvc里面使用aop不起作用。。######。。。对不起,已经好了,我把两个项目搞混 了,配置的包错了,非常感谢!######还是无效,这评论不太会用,麻烦您看下下面我自己的评论######

    引用来自“汉唐”的答案

    引用来自“汉唐”的答案

    controller里面无法直接这样切入的。需要切入

    execution(* org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(..))

    因为你controller注解的类,都被这个org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter给代理了。

    不需要了。现在还是无效?
    不用客气  ,好了就好。 
    ######最好不要这么处理日志,你想记录什么日志?访问日志吗?###### @张浩春 管理员的操作日志没有必要使用aop 使用过滤器和拦截器就可以了,具体参见 springrain的日志管理######管理员操作日志,不想在每个类里面写###### ######

     问题解决了。 目测大部分同学的aop失效都是因为在springmvc的配置文件里面扫描了类,那么spring去扫描的时候发现内存中已经有了对象,就不会在对类进行aop增强。所以当我们确定在那一层切入的时候,那么在springmvc的配置文件中,应该要排除欲切入的层。


    <!-- 扫描的时候过滤掉Service层,aop要在service进行切入!  -->
    	<context:component-scan base-package="com.xxx.infoaudit">
    		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    	</context:component-scan>
    在spring里面扫描service 层


    <context:component-scan base-package="com.**.service" />



    OK.问题完美解决。 一个小问题纠结了好久。
    2020-05-30 11:15:50
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载