Hello!
Have a query regarding Spring AOP Native support. I have the following aspect:
package com.example.aop;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.example.service.LogService;
@Aspect
@Component
public class LogAspect {
@Autowired
private LogService logService;
@Around("@annotation(com.example .aop.Log)")
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
Object returned = joinPoint.proceed();
logService.log(joinPoint, returned);
return returned;
}
}
Which should be triggered once a controller method is annotated by Log, as follows:
Interface:
package com.example.aop;
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 Log {
}
Controller:
package com.example.controller.impl;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.Log;
@RestController
@RequestMapping(ExampleControllerImpl.PATH)
public class ExampleControllerImpl {
@PostMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
@Log
public ResponseEntity<String> hello() {
return ResponseEntity.ok("done!");
}
}
The aspect LogAspect is triggered successfully non native.
However if I build this application natively mvn -Pnative spring-boot:build-image, the image is built and run successfully however the aspect is not triggered. Is there any additional configuration to be done?
Also tried create a class which implements RuntimeHintsRegistrar for Log annotation interface and LogAspect as a result to register proxy interface and LogAspect constructor and methods, but still no success.
pom.xml:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath />
</parent>
<groupId>com.example</groupId>
<artifactId>example-project</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>example-project</name>
<dependencies>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
<build>
<finalName>example-project</finalName>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Thanks in advance.
Comment From: mhalbritter
Unfortunately, aspects are not fully supported on native yet. See https://github.com/spring-projects/spring-framework/issues/28711 and https://github.com/spring-projects/spring-framework/issues/29765 for details.
Spring Framework 6.0.4 contains https://github.com/spring-projects/spring-framework/issues/29519, which improves things a lot. Please give this problem of yours with the next Spring Boot version (3.0.2) a try when it's released. Hopefully it then works.
Comment From: trcoelho
Hello @mhalbritter , thanks for quickly reply!
Have updated to Spring Boot 3.0.2 and aspect stills not triggered.
Should additionals configurations (properties, hints etc) should be added as a result to have it working or do we need to wait an enhancement by Spring team to work on it (if so, do we have a release that it will be working)?
Best regards!
Comment From: mhalbritter
https://github.com/spring-projects/spring-framework/issues/28711 is still open, and I guess this is the main reason for it not working. I don't have an estimate in which release this will land, sorry. You can try to use the workaround from the linked ticket: https://github.com/spring-projects/spring-framework/issues/28711#issuecomment-1221931162
Comment From: sdeleuze
This is planned to be fixed in Spring Framework 6.0.5, which should match with Spring Boot 3.0.3.
Comment From: mhalbritter
Boot 3.0.3 is planned for February 23, 2023.
Comment From: wghdir
Spring Boot 3.0.3 and aspect stills not triggered
Comment From: wilkinsona
@wghdir Please read the comments above yours and the issues to which they link. https://github.com/spring-projects/spring-framework/issues/28711 is the issue that will fix this. Unfortunately, it did not make it into Framework 6.0.5 and remains open.
Comment From: SammieAra
Still appears aspects not triggered in Spring Boot 3.0.6
Comment From: scottfrederick
@SammieAra Please see the comments above that reference the Spring Framework issue https://github.com/spring-projects/spring-framework/issues/28711. That Framework issue is still open, so It is expected that it's still not working with newer Spring Boot releases.