Nosfert
Nosfert

Reputation: 542

Aspect annotation links

New to aspect oriented development.

Two part question coming up.

  1. Do you have any good sites that contain tutorial and code that runs? So far i have seen many tutorials but with fragmented code and there is nothing that i can piece together so that it works locally.

  2. Im trying to create a framework with an aspect and a aspectj class that should intercept all the method calls that are annotated with the aspect. It works great in my local project but when i try to use the aspect in another project it does not seem to be working.

Code example: Aspect interceptor

@Aspect
public class InterceptCallAspect {
    @Around("execution(* *(@InterceptCall (*)));")
    public void record(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //Before
        System.out.println("Before");
        proceedingJoinPoint.proceed();
        System.out.println("After");
        //After
    }
}

The Aspect

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface InterceptAspectAnnotation {
}

So when i annotate my testcase in my project i get the sysout in the right places. But when i create my jar and bundle it in another project it don't do anything.

My pom file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>InterceptCall</artifactId>
<groupId>testing</groupId>
<packaging>jar</packaging>
<version>0.0.2-SNAPSHOT</version>    
<dependencies>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.7</version>
            <configuration>
                <complianceLevel>1.8</complianceLevel>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Upvotes: 0

Views: 99

Answers (1)

sheltem
sheltem

Reputation: 3825

I can only address your second question, so I will leave the first for others.

If you supply the aspect via an external library (like your built jar), you need to tell the aspectj-maven-plugin where to find the aspects to use for weaving. The configuration-tag needs to contain a tag called aspectLibraries with aspectLibrary-tags for each lib to use:

        <aspectLibraries>
           <aspectLibrary>
              <groupId>com.your.example.util</groupId>
              <artifactId>tracing-aspect</artifactId>
           </aspectLibrary>
        </aspectLibraries>

Upvotes: 2

Related Questions