没有将建议汇总到各个模块的类文件中

时间:2018-07-23 08:17:18

标签: java aspect

我对AspectJ有问题,我想创建2个jar文件。一是具有方面和逻辑的文件,二是具有MVC服务的文件。我有两个模块:

logger-client
logger-service

内部模块客户端

@Aspect
public class AspectTesting {

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

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

和模块服务

 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");
    }
 }

所有内容都是用gradle构建的。我在模块logger-service中添加了依赖项

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

并且我在两个gradle文件中都添加了AspectJ

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

我还添加了模块logger-client作为IntelliJ中对logger-service的依赖。 不幸的是,当建议在不同模块中时,应该在未注入每种方法之前注入建议。仅当我将Aspect类移到logger-service模块中时,它才起作用。

我尝试使用注释。我在logger-client模块内创建了“ @Logger”注释,在通知中添加了适当的切入点,并在“ public void testing()”之前键入了该注释 但同样,Aspect注入不正确。

1 个答案:

答案 0 :(得分:0)

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());
      }
}