简体   繁体   English

未创建Spring AspectJ切入点

[英]Spring AspectJ pointcut not created

I have a Spring web app in which I'm trying to use AspectJ to perform some logging. 我有一个Spring Web应用程序,其中正在尝试使用AspectJ执行一些日志记录。

I have enabled @EnableAspectJAutoProxy on my config class and declared my aspect: 我在配置类上启用了@EnableAspectJAutoProxy并声明了我的方面:

@Aspect
@Component
public class LoggerAspect {

I have then placed a pointcut on a method of a class defined into a library (that's, of course, shipped into the war as a dependency) and another pointcut on a class of the main project (the one shipped into the war). 然后,我在定义到库中的类的方法上放置了一个切入点(当然,这是作为依赖项运送到战争中的),在主项目的类上放置了另一个切入点(运送到战争中的一个)。

When running unit tests everything works correctly and I get both my advice called. 在运行单元测试时,一切正常,我得到了我的建议。

If I deploy my app on tomcat 7, only the advice that's placed on the class contained into the jar gets called while the other one (deployed into WEB-INF/classes) is apparently ignored. 如果我将我的应用程序部署在tomcat 7上,则仅会调用放置在jar中包含的类上的建议,而另一个(部署到WEB-INF / classes中)将被忽略。

From the logs I can't seen anything strange a part from the fact that no AopProxy is created for the class located into WEB-INF (and this is coherent with the fact that the advice doesn't get called). 从日志中,我看不到任何奇怪之处,这是因为没有为位于WEB-INF中的类创建AopProxy(这与不调用建议的事实是一致的)。

What am I missing? 我想念什么?

Thanks. 谢谢。

got it. 得到它了。

The problem was that during a normal run, the aspect was autodiscovered BEFORE the class that it was supped to follow. 问题在于,在正常运行期间,方面是在应该遵循的类之前被自动发现的。

In practice I have this in my WebAppInitializer: 实际上,我在WebAppInitializer中具有以下功能:

@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { CoreConfig.class, LibConfig.class };
}

@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class};
}

LibConfig is the one that autodiscovers the service in the library (the one on which the aspect was walways working). LibConfig是在库中自动发现服务的一个(方面一直在工作的那个)。 CoreConfig is the one that discovers the aspect. CoreConfig是发现方面的一种。 WebConfig is the one that autodiscovers the other class that I wanted to intercept. WebConfig是自动发现我要拦截的另一个类的一个。

So, while starting up, the aspect was initialized before the class from WebConfig was available and so the it couldn't place the Pointcut. 因此,在启动时,方面已在WebConfig中的类可用之前初始化,因此它无法放置Pointcut。 During tests, all the classes where loaded together and everything was fine. 在测试期间,所有类都一起加载,一切都很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM