AspectJ Integration with Spring & IntelliJ not working
[X-Posting from StackOverflow] as I might be doing something wrong with IntelliJ
I am currently working on a multi-module gradle project where all modules are dependent upon the foo-common module. foo-common is the only one that does not have a main function. I want to integrate AOP with AspectJ and in order to do that I followed the following steps from the Official IntelliJ Documentation:
- Using IntelliJ Idea Ultimate 2017.2 with the two plugins installed
- Installed AspectJ
- Created a library for
aspectjrt.jarand added it as a dependency tofoo-common_mainandfoo-bar_mainmodules using this documentation. - Changed the compiler in IntelliJ from
javactoajcusing this documentation. - Enabled
Build | AspectJ Weaving - Created AspectJ Facets in the project structure for both
foo-commonandfoo-barwithPost-compile weave modeenabled
My files look like below:
Aspect.java (foo-common module)
@Aspect
public class TraceAspect extends Pointcuts {
private static final Logger logger = LoggerFactory.getLogger(TraceAspect.class);
@Before("call(* com.package.foo..*(..))")
public void calling(JoinPoint joinPoint) {
logger.trace("[CALL] " + joinPoint.getSignature().toString());
}
@Before("execution(* com.package.foo..*(..))")
public void initiating(JoinPoint joinPoint) {
logger.trace("[EXEC] " + joinPoint.getSignature().toString());
}
}
ApplicationConfiguration.java (foo-bar module)
@SpringBootConfiguration
public class ApplicationConfiguration{
private static final Logger logger = LogManager.getLogger(ApplicationConfiguration.class);
@Bean
public DummyClientFactory dummyClientFactory() {
return new dummyClientFactoryImpl();
}
}
The project itself compiles and runs. If I try to modify ApplicationConfiguration.java to have manual log statements, that too works. And I can see IntelliJ using intelli-sense to point me to which advices connect to which methods. However I cannot see the logs being generated at all via the Aspects.
Please sign in to leave a comment.
In Gradle/Maven projects the configuration should be performed using these tools, not by editing the dependencies manually, otherwise your manual changes will be discarded on the next project reimport/refresh.
As stated in the comments at StackOverflow, this has to be resolved via Gradle configuration, then build the project using Gradle from the command line to ensure that it works.
One of the docs I've googled: http://foat.me/articles/java-aspects-using-spring-aop-and-aspectj/#gradle-configuration.
Note that in IntelliJ IDEA you can delegate building and running to Gradle so that it works the same in IDE and in the command line/terminal: http://mrhaki.blogspot.ru/2016/11/gradle-goodness-delegate-build-and-run.html.