简体   繁体   中英

Using aspects from other jars

What I'm trying to accomplish is the following:

I have a server with the following structure.

bin
   apis
   services
   etc...

I want to define an API that contains an aspect to 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 annotate the methods I want with @Authorization and it gets pointcut by that aspect.

Things you should know:

  • Services only use the API to compile the code, therefore the scope is "provided", since the API will be already in the server.
  • Services JARs are loaded dynamically, so they will reside in another classloader.

My question is, how can I do this? How do I define my maven artifacts to accomplish that?

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

Thanks in advance,

Rui

Take a look at how it's done in jcabi-aspects . You declare/compile your aspects in the API and then use this JAR as we com.jcabi:jcabi-aspects is being used 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's OK to have your aspects JAR in provided (or runtime ) scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM