-
Notifications
You must be signed in to change notification settings - Fork 38.4k
Native image ignoring annotations when actual bean type is not exposed in the @Bean method #32527
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
I've had a look at your application and unfortunately it's too large for us to consider it in this issue. I think the main misunderstanding here is that the
This means that if a bean is not present when the application is built, its definition will not be present at runtime in native mode, even if th environment changes. Removing unused code paths is a key feature in GraalVM - if we were to somewhat work around that, this would mean that applications would get significantly larger as we would ship way more code than expected. This is being tracked in #21497 but we haven't made progress on this task as the existing tradeoffs work quite well from what we're seeing. The other issues listed here might be linked to AOT best practices not being followed - if the behavior is too dynamic, the AOT engine cannot infer bean information and won't be able to have the behavior you're expecting. If you'd like to continue here, let's consider issues one by one and please provide minimal samples for each. This makes the process a bit more cumbersome for you, but isolating problems is the best way to be on the same page. Thanks! |
Thank you for your quick response. I don't mind creating a minimal reproducible project for you to illustrate the "bugs". But after reading through the AOT best practices docs you linked, I just don't think what I'm trying to achieve is possible. If I cannot return an interface and expect the actual implementation to have its annotations processed, most of my points are moot. I'll have to create both beans at all times and then decide which one to use at runtime. If you think it's worth having a sample project, I can set it up, but knowing what I know now, it would likely be a waste of both our time. The only thing I found to be interesting is |
Things are processed at build-time so you can't hide the actual implementation. If you do, then usual containers callbacks won't be processed on your types. That said, there are several ways, depending on the actua callback to address this issue.
I have no idea what that means. Perhaps there is still a misunderstanding and a sample would help still? If you don't intend to share it, please close the issue. Otherwhise, make it as focused as possible so that we know we're on the same page. |
Thanks for working with me! I have managed to create a small project illustrating my use-case. Given this project, Output from native image
Output from AOT build:
|
Alright. Yes, if you use annotations in an implementation you hide from us, there is no way to find out about the stereotypes you want us to process in a native image. For this particular case:
I've done both of that in and this outputs the following in a Native image for me:
I've pushed the code I've changed so that you can review it: https://github.com/Schaka/native-example/pull/1 With all that said, I wouldn't recommend that setup if you work with Native images as it requires too many manual steps and we're already document that's not going to change. Hope that helps. |
I recently migrated my project to native images. As
@ConditionalOnProperty
doesn't work with native images, I manually created my beans in@Configuration
classes dependent on properties.The
@Bean
method returns an interface, like suchThe RadarrRestService has a
@Cacheable
annotation on one method,@PostConstruct
on another and@RegisterReflectionForBinding
on the class itself.None of the annotations are being processed by Spring.
Here are some observations as to what works:
ServarrService
and annotating the interface with@PostConstruct
makes it get calledRadarrRestService
class with@Service
results in 2 beans, but BOTH will have their@Cacheable
processed, including the one created via@Bean
@RegisterReflectionForBinding
gets processed only whenRadarrRestService
is annotated with@Service
Other attempts/observations:
I had tried letting Spring create my beans with
@Service
annotations and only letting my config class device which already injected bean gets returned by the@Bean
method - like here.Unfortunately, this would throw a startup exception inside the native image (but not local AOT application startup), because RadarrService already had a CGLib class (proxy?) surrounding it. I don't have the exact exception anymore, unfortunately.
From what I can tell, this isn't actually intended behavior. It almost feels like there's only a small switch that was forgotten so that would allow
@Bean
produced beans to have their annotations (on all classes and subclasses) processed correctly.The text was updated successfully, but these errors were encountered: