I'm trying to do a SQL query profiling using AOP
Below is my snippet from @Aspect defenition
@Pointcut("(call(* org.springframework.jdbc.core.JdbcTemplate.query*(..)) || call(* org.springframework.jdbc.core.JdbcTemplate.update*(..)))")
public void profileQuery() {
}
@Around("profileQuery()")
public Object profile(ProceedingJoinPoint thisJoinPoint) throws Throwable {
logger.info("profileQuery() aspect Before");
Object pjp= thisJoinPoint.proceed();
logger.info("profileQuery() aspect After");
return pjp;
}
POM snippet for aspectj maven plugin
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.14.0</version>
<configuration>
<complianceLevel>${java.version}</complianceLevel>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<Xlint>ignore</Xlint>
<encoding>UTF-8 </encoding>
</configuration>
<dependencies>
</dependencies>
<executions>
<execution>
<id>compile</id>
<phase>process-classes</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
POM snippet for maven compiler plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<fork>true</fork>
<source>${java.version}</source>
<target>${java.version}</target>
<useIncrementalCompilation>false</useIncrementalCompilation>
<excludes>
<exclude>**/*.*</exclude>
</excludes>
</configuration>
</plugin>
I tried using Eclipse and also tried compiling outside Eclipse
Command used
mvn clean install -Dmaven.test.skip=true
No issues in building and deploying it on Tomcat 8.5.83 , also the APIs are working, but the aspect is not being intercepted as expected.
There are several problems in your POM and code, which I fixed in this pull request, for example:
You configured AspectJ Maven Plugin in the
pluginManagementsection, but forgot to actually add the plugin to the project in a separatepluginssection.Your example does not contain a test base directory. Therefore, you need to remove goal
test-compilein order to avoid build errors.When using
org.codehaus.mojo:aspectj-maven-plugin, usingcomplianceLevelis not enough, you also need to specifysourceandtarget. Or you simply switch to the betterdev.aspectj:aspectj-maven-pluginvariant, thencomplianceLevelis enough.Package name
aspectis not OK for native AspectJ, because in AspectJ it is a reserved keyword. So I renamed your package toaop.Your pointcut
call(* service.apiInnerService(..))is wrong, you forgot the class name and should usecall(* service.MainService.apiInnerService(..))instead.A native AspectJ aspect should not have a
@Componentannotation, because that is used for Spring AOP proxy-based aspects. You should avoid Spring to pick it up a second time.In addition to these changes, I also removed some cruft from your code, reformatted everything a bit and added a main method to
AppConfig, so I could easily run the applicaion from my IDE and verify that the aspect works. But it should work the same in your WAR.When running the application, now I see:
I.e., the aspect is working, intercepting your private method call. But allow me to say that if you need to apply aspects to private methods, you most likely have a design problem in your application.