Skip to content
Advertisement

Spring aop: Pointcut defined for subclasses but only one subclass is invoked

I am defining a JobProcess with a method Object process( JobContext jobContext ); with an impl called JobProcessImpl. Whenever this JobProcessImpl.process method is executed, I want to spy with more than one subclass. I want all this subclasses to be executed.

The spy class is defined as base class Task to look for JobProcessImpl.process invocation.

In the output, I always see that only log from AnnotationTask and not from ReviewTask.

Please let me know, if it is possible and what is the issue.

I tried for 2 days on solving this by following various posts.

JavaScript

Base Classes: AnnotationTask

JavaScript

ReviewTask

JavaScript

I want these task execution by spying on them with TaskAspects.

JavaScript

I have an enum classes as well (giving for the code completion)

JavaScript

JobProcess

JavaScript

JobProcessImpl

JavaScript

Advertisement

Answer

You are right, I just noticed that TaskAspects not get triggered at all. I do not see any logs from TaskAspects. Can I use AspectJ inside spring-boot without any spring-aop? Thanks for taking a look and notifying. Much appreaciated.

Basically you just remove the @Component annotations from the aspects you want use via AspectJ and add those aspects to file src/main/resources/org/aspectj/aop.xml (assuming you use Maven to build your project):

JavaScript

Then you start your application with -javaagent:/path/to/aspectjweaver.jar, e.g.something like:

JavaScript

You can also use both agents, AspectJ Weaver and Spring Instrument, together on the Java command line, e.g.

JavaScript

For your convenience and in order to have a example project for myself, I shared my working example in a GitHub repository. There is a branch for Spring AOP and another branch for AspectJ LTW. The diff between those branches looks like this:

JavaScript

BTW, I also quickly tried to get a combination of Spring AOP aspects (Task subclasses) and AspectJ aspect (TaskAspects, really a horrible name, why not TasksAspect or TaskInterceptor?). Even though the Spring manual and many users on mailing lists say, it is easy to combine both approaches even without extra configuration, I could not make it run the way I wanted. So for now I have no solution for that. Probably I just made a small mistake. I am an AspectJ expert, but actually never use Spring or Spring AOP, sorry.


Update: I forgot to mention that in my repository I also solved this problem I mentioned in my comment before:

If you really use AspectJ LTW scenario, your TaskAspects aspect gets triggered too often because within(com.spring.aspect.dynamicflow.activity.Task+) in AspectJ not only intercepts method executions but also object and class initialisation, field access, method calls to other classes etc. So either you use AspectJ LTW and it prints too much or (…)

This is really true if you use that pointcut. The log output (time stamps, log channel info etc. cut off from log output) would be:

JavaScript

I also cut out AspectJ weaver log messages (some of them errors) from in between the log lines. You see 29x “Handling the task aspects” instead of only 2x, which is why I changed the pointcut to within(com.spring.aspect.dynamicflow.activity.Task+) && execution(* task(..)) for you. Now the log output looks as expected:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement