Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AspectJ Support for GraalVM Native Images #33865

Closed
trcoelho opened this issue Jan 18, 2023 · 9 comments
Closed

AspectJ Support for GraalVM Native Images #33865

trcoelho opened this issue Jan 18, 2023 · 9 comments
Labels
for: external-project For an external project and not something we can fix status: duplicate A duplicate of another issue

Comments

@trcoelho
Copy link

trcoelho commented Jan 18, 2023

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 18, 2023
@mhalbritter
Copy link
Contributor

mhalbritter commented Jan 18, 2023

Unfortunately, aspects are not fully supported on native yet. See spring-projects/spring-framework#28711 and spring-projects/spring-framework#29765 for details.

Spring Framework 6.0.4 contains spring-projects/spring-framework#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.

@mhalbritter mhalbritter added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 18, 2023
@mhalbritter mhalbritter closed this as not planned Won't fix, can't repro, duplicate, stale Jan 18, 2023
@wilkinsona wilkinsona added the for: external-project For an external project and not something we can fix label Jan 18, 2023
@trcoelho
Copy link
Author

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!

@mhalbritter
Copy link
Contributor

spring-projects/spring-framework#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: spring-projects/spring-framework#28711 (comment)

@sdeleuze
Copy link
Contributor

This is planned to be fixed in Spring Framework 6.0.5, which should match with Spring Boot 3.0.3.

@mhalbritter
Copy link
Contributor

Boot 3.0.3 is planned for February 23, 2023.

@wghdir
Copy link

wghdir commented Feb 28, 2023

Spring Boot 3.0.3 and aspect stills not triggered

@wilkinsona
Copy link
Member

@wghdir Please read the comments above yours and the issues to which they link. spring-projects/spring-framework#28711 is the issue that will fix this. Unfortunately, it did not make it into Framework 6.0.5 and remains open.

@SammieAra
Copy link

Still appears aspects not triggered in Spring Boot 3.0.6

@scottfrederick
Copy link
Contributor

@SammieAra Please see the comments above that reference the Spring Framework issue spring-projects/spring-framework#28711. That Framework issue is still open, so It is expected that it's still not working with newer Spring Boot releases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

8 participants