简体   繁体   中英

Spring AspectJ pointcut not created

I have a Spring web app in which I'm trying to use AspectJ to perform some logging.

I have enabled @EnableAspectJAutoProxy on my config class and declared my aspect:

@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.

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).

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:

@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). CoreConfig is the one that discovers the aspect. WebConfig is the one that autodiscovers the other class that I wanted to intercept.

So, while starting up, the aspect was initialized before the class from WebConfig was available and so the it couldn't place the Pointcut. During tests, all the classes where loaded together and everything was fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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