小弟对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"); } }
这个里面因为有公司项目,所以把包名用*号代替了,见谅!!现在不输,求帮忙
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
在使用Spring AOP记录日志时,你遇到的问题可能是由于多种原因导致的。根据你的描述,配置后启动没有错误,但切面(Aspect)中的方法没有如预期那样执行。这里有几个可能的原因和相应的解决办法:
确保AOP配置正确: 你的切点表达式execution(* com.*.windrunner.controller.*(..))
看起来是正确的,它应该会匹配到com.windrunner.controller
包下所有类的所有方法。但是,请确认以下几点:
Spring AOP代理模式: Spring AOP可以基于两种代理模式:JDK动态代理和CGLIB代理。默认情况下,Spring对实现了接口的类使用JDK动态代理,对没有实现接口的类使用CGLIB代理。如果你的控制器类没有实现任何接口,确保Spring配置支持CGLIB代理。在Spring Boot应用中,这通常是自动处理的,但在传统的Spring应用中,你可能需要在配置中显式开启,例如在XML配置中添加 <aop:aspectj-autoproxy proxy-target-class="true"/>
。
检查日志输出配置: 你说“然后呢,AOP类...现在不输”,如果指的是控制台没有输出信息,除了检查AOP是否生效外,还要确认日志框架的配置是否正确。确保你的日志级别设置为能够打印出DEBUG或INFO级别的消息,因为System.out.println
通常在开发环境中有效,但如果应用部署在生产环境并使用了如Logback或Log4j这样的日志框架,标准输出可能被重定向或过滤。
Spring Boot应用: 如果你是在Spring Boot环境下工作,确保你的AOP配置类被扫描到了。通常,只要将配置类放在与主启动类同级或其子包内,并且使用了@ComponentScan或@SpringBootApplication注解,就不需要额外配置。但如果你的配置类位置特殊,可能需要手动调整@ComponentScan的范围。
查看Spring启动日志: 在Spring应用启动过程中,会有关于AOP配置和代理创建的日志输出。仔细检查这些日志,看是否有任何关于你的切面的警告或错误信息,这可能会提供问题的线索。
测试和调试:
beforeMethod
方法是否被调用,以及何时被调用。如果以上建议都不能解决问题,建议提供更多的上下文信息,比如Spring框架的版本、项目的具体结构、以及详细的启动日志,以便进行更深入的分析。