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

How do I use @Aspect in the native-image? #29765

Closed
wangliang181230 opened this issue Jan 4, 2023 · 4 comments
Closed

How do I use @Aspect in the native-image? #29765

wangliang181230 opened this issue Jan 4, 2023 · 4 comments
Assignees
Labels
status: duplicate A duplicate of another issue

Comments

@wangliang181230
Copy link

wangliang181230 commented Jan 4, 2023

How do I use @Aspect in the native-image?

The annotation:

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}

The aspect:

@Aspect
@Component
public class MyAspect {
	private static final Logger LOGGER = LoggerFactory.getLogger(MyAspect.class);

	@Pointcut(value = "@annotation(cn.wangliang181230.studynativeimage.MyAnnotation)")
	public void pointCut() {
	}

	@Around("pointCut()")
	public Object beforePointCut(ProceedingJoinPoint joinPoint) throws Throwable {
		MethodSignature signature = (MethodSignature)joinPoint.getSignature();
		Method method = signature.getMethod();

		LOGGER.info("aspect: {}", method.getName());

		return joinPoint.proceed();
	}

}

The controller:

@RestController
public class TestController2 {
	private static final Logger LOGGER = LoggerFactory.getLogger(TestController2.class);

	@MyAnnotation
	@GetMapping("/test")
	public String test() {
		LOGGER.info("test controller");
		return "test";
	}
}

The application:

@SpringBootApplication
@EnableAspectJAutoProxy
public class StudyNativeImageBySpringBoot3Application {

	public static void main(String[] args) {
		SpringApplication.run(StudyNativeImageBySpringBoot3Application.class, args);
	}
}

mvn clean package -Pnative -e

Then start application.exe and visit http://localhost:8080/test, but the aspect did not take effect in the native-image.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 4, 2023
@wilkinsona
Copy link
Member

We have a smoke test for @Aspect that works. One difference here is your use of an annotation. It may be that there there is insufficient reflection metadata available in the native image for the annotation's presence to be detected by the AOP infrastructure.

Support for @Aspect is a Spring Framework feature so we'll transfer this issue to the Framework team so that they can investigate.

@bclozel bclozel transferred this issue from spring-projects/spring-boot Jan 4, 2023
@wangliang181230
Copy link
Author

wangliang181230 commented Jan 4, 2023

When I change @annotation(cn.wangliang181230.studynativeimage.MyAnnotation) to execution(* cn.wangliang181230.studynativeimage.TestController2.*(..)), and hints.reflection().registerType(TestController2.class);.
It still doesn't work.

图片

@sdeleuze sdeleuze self-assigned this Jan 4, 2023
@sdeleuze
Copy link
Contributor

sdeleuze commented Jan 4, 2023

Aspect could work for simple use cases like in the smoke test, but there are not fully support on native yet, this is already tracked by #28711, so I will close this one as a duplicate

@sdeleuze sdeleuze closed this as not planned Won't fix, can't repro, duplicate, stale Jan 4, 2023
@wangliang181230
Copy link
Author

OK,Thank you.

@bclozel bclozel added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 4, 2023
@wangliang181230 wangliang181230 changed the title How do I use @Aspect in the native-image? How do I use @Aspect in the native-image? Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

5 participants