简体   繁体   中英

Adding aspects to maven project

I created aspectJ class in seperate Maven project:

@Aspect
public class AspectE {

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

    @Before("defineEntryPoint()")
    public void setThreadName(JoinPoint joinPoint) {
       ...
    }

    @After("defineEntryPoint()")
    public void removeThreadName(JoinPoint joinPoint) {
        ...
    }
}

Then in second project I annotated several methods and added to pom.xml :

    <dependency>
        <groupId>first-project</groupId>
        <artifactId>first-project</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.7.0</version>
    </dependency>

But still aspects aren't seen at all. Am I missing some steps? What should I do?

Did you take a look at this?

AspectJ compiler Maven Plugin - Usage

In order to weave correctly your code with your libraries, you should declare them within your dependencies AND within the aspectj weaver:

<dependencies>
    <!-- Aspectj lib  -->
    <dependency>
        <groupId>com.my.group</groupId>
        <artifactId>my-aspect-lib</artifactId>
        <version>1.0</version>
    </dependency>

    <!-- Other dependencies -->

</dependencies>

<build>
    <!-- Specific build configuration -->

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <configuration>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>com.my.group</groupId>
                        <artifactId>my-aspect-lib</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
        </plugin>

        <!-- Other plugins configuration -->

    </plugins>
</build>

<!-- Other settings -->

You have to weave the aspects with the code. This can be done in 2 ways:

Load-time weaving is a bit more versatile, but can be a bit challenging to set up properly. It consumes more CPU during startup (when the weaving happens), and also has a memory footprint. Compile-time weaving consumes more CPU during the compilation, obviously, but then you don't pay the price on each restart.

I had the same problem ... but after I added this maven repo it's working

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.9</version>
</dependency>

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