Search code examples
aopspring-aopaspect-ratiointerceptaspect

Unable to use aspectJ interceptor in a non-spring project


I am trying to use Aspectj to execute some code after some method execution. I cannot use spring AOP as the project is a non-spring project and at this point of time I cannot change it to spring project. I have tried with a very simple implementation as below but it is not at all working: POM of my project

<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>
<groupId>test.aspectj</groupId>
<artifactId>HelloAspectj</artifactId>
<version>0.0.1-SNAPSHOT</version>

<properties>
    <maven.compiler.plugin.version>3.5.1</maven.compiler.plugin.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.9.6</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.6</version>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.plugin.version}</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

    </plugins>
</build>
</project>

A normal class and method after which the aspect methods will run:

package tester;

public class HelloWorld {
    private String name;

    public void setName(String name) {
        this.name = name;
    }

    public void printHello() {
        System.out.println("Spring 3 : Hello ! " + name);
    }
}

Aspect class

package tester;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
 
@Aspect
public class TestAspect {
 
    @Before("   call(void java.io.PrintStream.println(String)) " +
            "&& !within(net.andrewewhite.aspects..*)")
    public void beforePrintlnCall() {
        System.out.println("About to make call to print Hello World");
    }
 
    @After("    call(void java.io.PrintStream.println(String)) " +
           "&&  !within(net.andrewewhite.aspects..*)")
    public void afterPrintlnCall() {
        System.out.println("Just made call to print Hello World");
    }
}

Main class

package tester;

public class MainApp {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println("Hello World");
    }
}

aop.xml

<aspectj>
    <aspects>
        <aspect name="tester.TestAspect"/>
    </aspects>
</aspectj>

Project Structure:

Now i am expecting that it will price About to make call to print Hello World & Just made call to print Hello World BUt it is only printing Hello World can someone help here..


Solution

  • If you want to use compile-time weaving, use

    • Mojohaus AspectJ Maven plugin until Java 8 and AspectJ 1.8.x or
    • Nick Wongdev's AspectJ Maven fork for Java 9+.

    Javac via Maven Compiler plugin is not enough.

    If you wish to use load-time weaving (LTW), it should be okay to compile your aspects with Javac via Maven Compiler plugin, as long as you only use annotation-driven @AspectJ syntax. For native AspectJ syntax you always need the AspectJ compiler Ajc via AspectJ Maven plugin, no matter what. For LTW you also need the weaving agent on the Java command line via -javaagent:/path/to/aspectjweaver.jar. There is also a hot-attachment option, but that is advanced stuff and you need to know what you are doing and the application must know that it wants to attach the weaver, so let's not talk about this here, as you are clearly a beginner.

    All of this has been documented on the AspectJ website and the AspectJ Maven website. I have also answered numerous questions about AspectJ + Maven here, you should easily find some. Before asking questions, you should really search first. This website does not replace manuals and tutorials.