I wish to do logging in our project using AOP. I am facing the issue where if a method of a class was calling another method of the same class inside it, then AOP would not work on that call because of the proxy way of doing things. To counter it, I am trying to do compile time weaving using aspectj maven plugin. My top level project pom looks like this:
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.7</complianceLevel>
<source>1.7</source>
<target>1.7</target>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I have 6 sub projects but I wish to do AOP based logging in only one of those. That project's pom looks like this:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.1</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
I though this would resolve the problem, but it is still doing proxy based method calling. What else needs to be done?
Edit: Spring context.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Allow proxys -->
<!-- <aop:aspectj-autoproxy /> -->
<context:component-scan base-package="com.relevant.package" ></context:component-scan>
</beans:beans>
LoggingAspect.java
@Named
@Aspect
public class LoggingAspect {
@Before("execution(public * com.relevant.package.*.process(..))")
public void beforeProcessAdvice() {
System.out.println("AOP");
System.out.println("Before");
}
@Before("execution(public * com.relevant.package.*.internalMethod(..))")
public void beforeProcessAdvice() {
System.out.println("Internal");
System.out.println("Method");
}
}
The target file has process method like:
process() {
internalMethod();
}
Since I have commented autoproxy part, I am saying that don't use proxy. However, no logs appear now (neither when process method is called nor when internalMethod is called). If autoproxy is on, then logs appear for process method but not for internalMethod, which is understandable.
After one day into this problem, the solution is very strange. I did a step by step debugging with the following results:
I checked the showWeaveInfo and verbose to true in the aspectj plugin, and during the maven build I could see properly that the aspect was getting applied at the correct join point.
Then I checked the compiled .class file to see whether weaving was indeed done, and to my surprise the .class was properly weaved.
Then I clicked Maven->Update maven projects and checked the byte code again. Now the weaved part had disappeared from the .class file.
So as soon as I did an update maven project the .class file would somehow revert back to a state where it was not woven with the aspect code. Refreshing the project instead of updating maven project solved my problem but I fail to understand the reason for this.