I'm trying to get my Aspect class to work but it gets completely ignored.
I have following files:
MyAnnotation.java
package annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
}
MyAspect.java
package annotations;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class MyAspect {
@Before("@annotation(annotations.MyAnnotation)*")
public void interceptMethods(final JoinPoint thisJoinPoint) {
System.out.println(thisJoinPoint);
}
}
MyClass.java
package annotations;
import org.springframework.stereotype.Service;
@Service
public class MyClass {
@MyAnnotation
public int myMethod(final int i) {
System.out.println("method: " + i);
return i;
}
}
MyRestController.java
package annotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyRestController {
@Autowired
MyClass myClass;
@GetMapping("/myClass")
private int callMyMethod() {
return myClass.myMethod(1);
}
}
Application.java
package annotations;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>experiments</groupId>
<artifactId>annotations</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.2.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.11.RELEASE</version>
</dependency>
</dependencies>
</project>
Does anyone see the problem and how to fix it?
I've tried multiple Before expressions ("execution(* annotations..(..))") but I just can't see to get it working.
I've tried Around instead of Before.
I've tried Pointcuts with Before.
I've been through articles: AspectJ @Before annotation issue Spring AspectJ, pointcut before method execution where method OR class is annotated