Aspect class showing NotFoundException when adding multiple methods in spring aspect class

39 views Asked by At

I am using aspect oriented programming in my microservice architecture. There is one common module which is used as a dependency in other microservice applications. Now this common class has my aspect class and it contains aspect methods of all the microservices which are using this common module. Now if i am running one of my microservice then the methods in aspect class which are not of the running microservice class are showing classNotFoundException. I will explain with one example @After("execution(public void com.lowes.boomerang.chunkgenerator.service.sendExceptionEvent(..))")

@After("execution(public void com.rboomerang.scraper.code.website.productpage.impl.HomeDepotProductPageScraper.testAop(..))")

Now these two methods are in my aspect class which is in common module. But when i am running either of these microservices then we are getting classNotFoundException for the other method. like in the above example if i run product.scraper microservice then chunkgenerator microservice will throw error and vice versa.

Any way we can include methods of different microservices in the common module and use aop to run all these methods without getting any exception.

Now these two methods are in my aspect class which is in common module. But when i am running either of these microservices then we are getting classNotFoundException for the other method. like in the above example if i run product.scraper microservice then chunkgenerator microservice will throw error and vice versa.

@After("execution(public void com.lowes.boomerang.chunkgenerator.service.sendExceptionEvent(..))")

@After("execution(public void com.rboomerang.scraper.code.website.productpage.impl.HomeDepotProductPageScraper.testAop(..))")

1

There are 1 answers

0
kriegaex On

If your aspects are so specific that they apply to single classes like in this case, or to classes of only one module, they do not belong to a common module in the first place, but exactly to the module they are targeting. Otherwise, you rightfully experience problems like the one you describe. Of course, referring to a class without your module declaring a dependency on it, results in that class not to be on the classpath of the so-called "common" module. Actually, other modules are supposed to depend on the common module, not the other way around. That is just bad design.

I understand the wish to create a separate aspect library, if same library is generic enough to apply to multiple modules or even multiple applications altogether. But then, you do not use fully qualified class names.

A workaround for your problem is to use wildcards * or maybe even something like ..*. But that does not repair your bad design, just makes it work.

Examples:

com.*.boomerang.chunkgenerator.service.sendExceptionEvent
*..*.chunkgenerator.service.sendExceptionEvent
com.lowes.boomerang.chunkgenerator.service.sendExceptionEvent*

The above make your pointcut less precise (in theory, they could match other classes or methods), but because using wildcards, they no longer require an exact class on the classpath. But really, the pointcuts are super specific, just put them into the modules they belong to. They are not "common".