-
Notifications
You must be signed in to change notification settings - Fork 40.8k
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
Prevent initializers from being applied twice in AOT mode #32262
Comments
I'm not sure we can skip running Perhaps the initializer iteself should be updated to not attempt bean registration if running in AOT mode? |
If this becomes a common pattern, perhaps we can provide a convenience mechanism to do this such as an annotation to add to the initializer class or an interface for it to implement. |
This is a problem that has a wider scope. Based on the experience of integrating features in Spring Native, I see the following:
While the first one looks harmless (just replaying what the AOT-generated code has done), it really isn't. If there is an instance supplier, AOT can't optimize it. If there isn't, then it won't work in a native image as the bean definition contributed by the (replayed) initializer will require reflection. If we don't run the initializer, then the change to the environment or the presence of singletons is lost. To me, this looks like something that should be addressed in framework somehow. We need to guide users what they can or cannot do if they want to optimize their context Ahead-of-Time as well as the escape hatch if they chose to use something that we can't optimize. |
See #32422 for another example that is test-specific. We have Rather than contributing hints for something that shouldn't run, I went down the route of using |
I've raised spring-projects/spring-framework#29484 to see if we can have some framework support for this. |
Running the following application prints 2 times
Foo constructor
at startup while a single time as expected in regular mode.A look at the generated code shows that
Foo__BeanDefinitions
generated during AOT transformations based on the initializer is created as expected, but it seems the second invocation comes from the fact thatSpringApplication#applyInitializers
is still invoked at runtime.Notice that on native, this second invocation fails with
Runtime reflection is not supported for public com.example.commandlinerunner.Foo()
because unlike the AOT generated one, it requiresExecutableMode#INVOKE
reflection hints (not inferred because not needed by the AOT generated code which is fine withExecutableMode#INTROSPECT
).Both issues should be solved by removing the second invocation of the initializers, but this need to be explored because it could involve unwanted side effects with some initializers.
The text was updated successfully, but these errors were encountered: