-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Native image is missing CGLIB proxy when @Aspect
is used
#33491
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
Comments
EDIT: my analysis below is incorrect, but I provide an updated analysis in later comments.
I haven't run your sample application, but that is the expected behavior. Your As for why you are encountering that exception, someone from the team will have to take a closer look to assess the cause of that. |
In my example Controller doesn't implement any interface, but in a project where I see this error, all controllers implement interfaces generated from an openapi specification. I wonder if spring-boot-maven-plugin shouldn't output CGLIB proxy for service implementation while running |
Hi @fenuks, Thanks for the feedback. It seems there are a few things we need to sort out... What exactly are you trying to achieve with the following pointcut expression? @Around("within(org.example.demo..*) && execution(public * *(..)) && @target(org.example.demo.aspect.AutomaticLogger)") In your sample application, do you expect public methods to be advised in From what I can tell, your pointcut is too broad: it also attempts to match against the Thus, I would recommend modifying the pointcut to match only against types in subpackages of I believe you should be able to achieve that like this: @Around("within(org.example.demo.*..*) && execution(public * *(..)) && @target(org.example.demo.aspect.AutomaticLogger)")
Spring Boot configures proxying of target classes (using CGLIB) by default for AspectJ. If you want to use dynamic proxies (interface based) by default, add the following to your spring.aop.proxy-target-class=false By making changes along the lines of what I suggested above plus annotating Thanks, Sam p.s., as a side note, we generally recommend |
Hi @sbrannen, thank you for your answer and suggestions!
I want to advice every class within my project iff it is annotated with
I will have to read aspect documentation again because I believed that only classes that are only explicitly marked with I will check if I uses of the aspect will allow me to further reduce its scope to subpackages.
I tried that, but I then got an error I didn't understand back then, but now I do, thanks to your insights.
Yes, I think we have everything covered. I have now a good understanding of the underlying issue. One thing that is not perfectly clear to me: even if my aspect advices some classes without visible effect because no logging will be done due to missing annotation, is it expected behaviour of the spring-boot-maven-plugin to not save CGLIB proxies created by aspect to the disk for the graalvm when
Will do, thank you very much! |
You're very welcome!
OK, but then the methods to be proxied in
To be honest, I was also a bit puzzled by that. I also assumed that only beans annotated with In any case, I'll likely try to clarify that with @jhoeller next week.
Sounds like a good plan.
👍
Great.
The cause of that behavior is a configuration error that is specific to AOT processing. Although it's typically fine for your That is indirectly documented in the Expose The Most Precise Bean Type section of the reference manual. |
Based on that hunch, I determined that switching to @Around("within(org.example.demo.*..*) && execution(public * *(..)) && @within(org.example.demo.aspect.AutomaticLogger)") See if that addresses the issue for you! |
I didn't know that it is advisable to declare beans with concrete types. Will keep that in mind and perhaps it's time to read documentation again. I think my situation is related, but a bit different. When aspect is missing, then ServiceImpl is dynamic proxy, because bean method returns interface, just as you explained. If aspect is present, though, it CGLIB-fies selected classes. In theory I guess, AOP processing could look at aspect and know which classes need to be advised in conjunction with bean definition processing?
It does, thank you very much again! Native version is now up and running with aspect enabled. ;) |
Yes, indeed. Regarding AOT support, it's always advisable to read the latest documentation for tips and best practices.
Your analysis is not quite correct.
That's actually what Spring AOP does when processing the
Awesome! I'm very glad to hear that. Thanks for the feedback. In light of the above, I am closing this issue. |
@Aspect
is used
In order to analyze that myself, I had two modified versions of your original sample application, and I pushed them to a fork of your repository so that you (and others) can experiment/verify on your own.
In Regards, Sam |
Thank you again for a detailed explanation and updated example. Both are very appreciated, I've learned a lot! |
I have project with aspect that automatically logs input and output of public methods, if class is annotated with
@AutomaticLogger
. My controller uses a bean that implements an interface, which makes it JDK proxy, as I understand. When aspect is enabled, it becomes a CGLIB bean, and CGLIB-enhanced code is missing in native image.Attempt to run generated image with aspect ends with error:
Problem can be reproduced with this sample project.
The text was updated successfully, but these errors were encountered: