【发布时间】:2018-04-30 10:39:46
【问题描述】:
我想在我的应用程序中使用 Spring AOP 来记录一些日志内容。我已经在一个独立的应用程序中使用了 AOP,它可以工作,但现在在 tomcat 上的一个 web 应用程序它不能工作。
我有一个 application-core 和一个 application-web 项目,在 core 项目中所有的逻辑都发生了,而 web-project 只包含与 web 相关的东西。
首先,我尝试将我的 LoggingAspect 类添加到我的核心项目中,因为这不起作用,我现在将它移到 Web 项目中,但尽管它不起作用。
这是我的 applicationContext.xml,它位于文件夹中:/application-web/src/main/webapp
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- Add AspectJ autoproxy support for AOP -->
<aop:aspectj-autoproxy/>
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="my.foobar"/>
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
这是我的 LoggingAspect 类,现在位于:application-web/src/main/my.foobar.web/aspects/LoggingAspect
@Aspect
@Order(1)
public class LoggingAspect {
private static final LogHandler LOG = LogHandler.getLogger(LoggingAspect.class);
@Pointcut("execution(* my.foobar.*.*.*(..))")
private void completePackage(){}
@Pointcut("execution(* my.foobar.*.*.get*(..))")
private void getterMethods(){}
@Pointcut("execution(* my.foobar.*.*.set*(..))")
private void setterMethods(){}
@Pointcut("completePackage() && !(getterMethods() || setterMethods())")
private void allMethodsExceptGetterAndSetter(){}
@Around("completePackage()")
private Object aroundMethod(ProceedingJoinPoint theProceedingJointPoint) throws Throwable{
LOG.info("around method");
String method = theProceedingJointPoint.getSignature().getName();
Object[] arguments = theProceedingJointPoint.getArgs();
LOG.info("method call: {0}", method);
for(Object arg: arguments){
LOG.info("argument[{0}]", arg);
}
Object result = theProceedingJointPoint.proceed();
return result;
}
}
我还在我的应用程序网络项目中添加了一个类
@Configurable
@EnableAspectJAutoProxy
@ComponentScan("my.foobar")
public class ApplicationWebAppConfig {
}
我期望的是,每个被调用的方法都会被记录,但那不会发生。
【问题讨论】:
-
如果您希望组件扫描检测到您的方面,则需要为
@Component。它还需要在您为组件扫描指定的包中。 -
我已经添加了@Component 但它也没有工作,方面是我在 componentsscan 中定义的包内。我通过破坏一些切入点语法来尝试这样做,在这里我收到一些“切入点格式不正确的异常”,所以我猜我的 LoggingAspect 已加载,但不知何故它没有激活?
-
请公开您的
@Around建议。春天需要找到它。如果这还不够,还请公开切入点。如果两者都不够,请在公开方面中的每个方法后告诉我,如果将completePackage()切入点更改为execution(* *(..)),会发生什么? -
在更改切入点时,您是否按照我所说的那样除了公开所有内容?你的目标类也是组件吗?您是否曾经基于一个示例项目在您的 Tomcat 上运行过 任何 Spring-AOP 方面?可能你有一些配置问题。如果所有这些猜测(无论如何我都不喜欢)没有帮助,请编辑您的问题并提供MCVE,最好准备好在 GitHub 上使用 Maven 进行克隆和构建。
-
我刚刚看到了 Gretty 的评论。在我用谷歌搜索之前我什至不知道它是什么,所以我不能说任何关于它的智能。也许对初学者来说保持简单并手动启动 Tomcat 以排除其他可能的错误源。运行最简单的设置,然后对其进行优化和自动化。这样你就会发现哪里出了问题。如果您刚刚开始使用一项新技术,那么在激活所有花里胡哨的情况下进行调试可能有点过于雄心勃勃。
标签: java tomcat spring-aop