I have the following @Before Aspect where if a method is annotated with my Custom Annotation, I would land into the Aspect which does a print.
The Aspect works fine but it prints twice, meaning I enter the Aspect twice.
Could I get some advice on why this happens twice when it should only be happening once. Thanks.
Note: This is not in Spring, this is implemeneted in plain Java.
Class with method annotated to trigger Aspect
public class ClassUsingUser {
@RequestValidation(isEnabled = true)
public String doSomething(User user) {
return user.toString();
}
}
Custom Annotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RequestValidation {
boolean isEnabled() default false;
}
Aspect class
@Aspect
public class RequestValidationAspect {
@Before("@annotation(RequestValidation)")
public void before(JoinPoint joinPoint) {
// this is the issue where I get this printed twice.
// but expecting it to happen only once.
System.out.println("Entered Aspect!!");
}
}
Main method to just trigger the method and Aspect
public class SomeMainClass {
public static void main(String[] args) {
User user1 = new User();
user1.setName("Fer");
ClassUsingUser classUsingUser = new ClassUsingUser();
// based on debug, I seem to hit the following doSomething() line, go to Aspect @Before,
// come back here and go into Aspect @Before again thus printing twice.
String s = classUsingUser.doSomething(user1);
System.out.println(s);
}
}
Dependencies and Plugins in POM File
<dependencies>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>6.0.2.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.9</version>
</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>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<Xlint>ignore</Xlint>
<encoding>UTF-8 </encoding>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>