Using Aspects from Other Cans

I am trying to do the following:

I have a server with the following structure.

  bin
    apis
    services
    etc ...

I want to define an API that contains an aspect that will be used by services. Say:

@Aspect public class AuthorizationAspect { @Pointcut("call(* *()) && @annotation(Authorization)") public void cutAuthorize() { } @Before("cutAuthorize()") public void callFromAuthorizeBefore() { System.out.println("Test"); } } 

Then I define the service and comment on the methods I want with @Authorization, and it gets a pointcut on this aspect.

Things you should know:

  • Services use the API only to compile code, so the scope is "provided" since the API will already be on the server.
  • JAR services load dynamically, so they will be in another class loader.

My question is: how can I do this? How to identify my maven artifacts for this?

I noticed that the aspectj plugin has a weaveDependencies section, but it will also include all classes in this API in the service JAR (something I want to avoid). Is it correct?

Thanks in advance,

Rui

+6
source share
1 answer

See how this is done in the jcabi aspects . You declare / compile your aspects in the API and then use this JAR as we use com.jcabi:jcabi-aspects in the example:

 <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <configuration> <aspectLibraries> <aspectLibrary> <groupId>com.jcabi</groupId> <artifactId>jcabi-aspects</artifactId> </aspectLibrary> </aspectLibraries> </configuration> </plugin> 

It is good that your JAR aspects are in the provided (or runtime ) area.

+3
source

Source: https://habr.com/ru/post/888656/


All Articles