开发者社区 问答 正文

SpringMVC中配置AOP拦截controller:报错

小弟对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");
	}

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

展开
收起
kun坤 2020-06-06 17:49:54 706 分享 版权
阿里云 AI 助理回答

在使用Spring AOP记录日志时,你遇到的问题可能是由于多种原因导致的。根据你的描述,配置后启动没有错误,但切面(Aspect)中的方法没有如预期那样执行。这里有几个可能的原因和相应的解决办法:

  1. 确保AOP配置正确: 你的切点表达式execution(* com.*.windrunner.controller.*(..))看起来是正确的,它应该会匹配到com.windrunner.controller包下所有类的所有方法。但是,请确认以下几点:

    • 确保你的项目中确实有控制器类位于这个包路径下。
    • 检查是否有其他AOP配置(比如更宽泛的切点)覆盖了你的配置,导致你的切面被忽略。
  2. Spring AOP代理模式: Spring AOP可以基于两种代理模式:JDK动态代理和CGLIB代理。默认情况下,Spring对实现了接口的类使用JDK动态代理,对没有实现接口的类使用CGLIB代理。如果你的控制器类没有实现任何接口,确保Spring配置支持CGLIB代理。在Spring Boot应用中,这通常是自动处理的,但在传统的Spring应用中,你可能需要在配置中显式开启,例如在XML配置中添加 <aop:aspectj-autoproxy proxy-target-class="true"/>

  3. 检查日志输出配置: 你说“然后呢,AOP类...现在不输”,如果指的是控制台没有输出信息,除了检查AOP是否生效外,还要确认日志框架的配置是否正确。确保你的日志级别设置为能够打印出DEBUG或INFO级别的消息,因为System.out.println通常在开发环境中有效,但如果应用部署在生产环境并使用了如Logback或Log4j这样的日志框架,标准输出可能被重定向或过滤。

  4. Spring Boot应用: 如果你是在Spring Boot环境下工作,确保你的AOP配置类被扫描到了。通常,只要将配置类放在与主启动类同级或其子包内,并且使用了@ComponentScan或@SpringBootApplication注解,就不需要额外配置。但如果你的配置类位置特殊,可能需要手动调整@ComponentScan的范围。

  5. 查看Spring启动日志: 在Spring应用启动过程中,会有关于AOP配置和代理创建的日志输出。仔细检查这些日志,看是否有任何关于你的切面的警告或错误信息,这可能会提供问题的线索。

  6. 测试和调试

    • 尝试在一个简单的测试用例中单独测试AOP逻辑,比如创建一个简单的服务类和对应的切面,验证AOP是否正常工作。
    • 使用断点或者日志记录来跟踪AOP切面的生命周期,看看beforeMethod方法是否被调用,以及何时被调用。

如果以上建议都不能解决问题,建议提供更多的上下文信息,比如Spring框架的版本、项目的具体结构、以及详细的启动日志,以便进行更深入的分析。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答