Search code examples
javaaspect

Advices aren't woven into class files across modules


I have problem with AspectJ, I wanted to create 2 jar files. One is file with aspects and logic, while second is with MVC service. I have two modules:

logger-client
logger-service

inside module client

@Aspect
public class AspectTesting {

   @Pointcut("execution(* * (..))")
   public void allServices2() {
   }

   @Before("allServices2()")
   public void executee(JoinPoint joinPoint) {
       System.out.println("Advice woken" + joinPoint.getSignature().getName());
   }
}

and module service

 public class Server {
        public static void main(String[] args) throws IOException {
        SpringApplication.run(Server.class, args);
        testig();
        System.out.println("Hey");
    }

     public void testing() { 
         System.out.println("Aspect woken");
    }
 }

All is built with gradle. I have added dependency inside module logger-service

  dependencies {
       compile project (":logger-client")
  }

and I have added AspectJ inside both gradle files

 project.ext {
   aspectjVersion = '1.9.1'
 }
 apply plugin: 'aspectj'

Also I have module logger-client added as dependency to logger-service inside IntelliJ. Unfortunately, the advice which should be injected before every method is not injected when it's inside different module. It works only when I move Aspect class inside logger-service module.

I tried using annotations. I created "@Logger" annotation inside logger-client module, added proper pointcut to advice and typed this annotation before "public void testing()" but same, Aspect is not injected properly.


Solution

  • I solved the problem. Adding "@Component" before Aspect class did job. So now Aspect class looks like:

     @Aspect
     @Component
     public class AspectTesting {
          @Pointcut("execution(* * (..))")
          public void allServices2() {
          }
    
          @Before("allServices2()")
          public void executee(JoinPoint joinPoint) {
              System.out.println("Advice woken" + joinPoint.getSignature().getName());
          }
    }