I have been working on a project A where I am adding aspects around AdminController for validating those urls when called but i have all the custom annotations in a separate repo B which i have added as an external jar in A as form of dependency.
Following are the files of project B where i have defined custom annotations.
Project B
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomAuth {
}
@Component
@Aspect
public class AuthAspect {
@Before(value = "@annotation(com.company.services.annotations.CustomAuth)")
public void doBefore(final JoinPoint joinPoint) {
System.out.println("Validating the admin url");
}
@Around(value = "@annotation(com.company.services.annotations.CustomAuth)")
public Object doAuthorize(final ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("During this");
long start = System.currentTimeMillis();
Object proceed = proceedingJoinPoint.proceed(); // Continue with the method execution
long executionTime = System.currentTimeMillis() - start;
System.out.println(proceedingJoinPoint.getSignature() + " executed in " + executionTime + "ms");
return proceed;
}
}
pom.xml of B
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Below are the set of files i have in project A where AdminController is there.
AdminController.java
@RestController
@RequestMapping("/{version}/admin")
public interface AdminController {
@CustomAuth
@GetMapping(value = "/node/status",
produces = {MediaType.APPLICATION_JSON})
void getStatus();
}
pom.xml of A (Inherit dependency of B)
<dependencies>
<dependency>
<groupId>com.company.service</groupId>
<artifactId>filter</artifactId>
<version>0.0.18</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
<exclusion>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.7</version>
</dependency>
</dependencies>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<configuration>
<aspectLibraries>
<aspectLibrary>
<groupId>com.company.service</groupId>
<artifactId>filter</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
</plugin>
<plugins>
Main SpringBoot file
@Configuration
@SpringBootApplication
@EnableAspectJAutoProxy
@ComponentScan(value = {"com.company"})
@EntityScan({"com.company"})
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class, args);
}
}
With this configuration, I am not able to do load time weaving for aspect on custom annotation. Any idea why this is not working or what i am missing here?