-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Destroy method not found in native image if concrete bean type is not exposed #29545
Comments
Thanks for the sample. I am afraid there's nothing we can do as the actual type is not exposed for AOT inference. This sample works on the JVM with AOT. It also works in Native if you add the following to the application:
It also works, as you found out yourself, if you expose the actual type (which is what you should be doing regardless of AOT).
I can see how that can be problematic but it is the very nature of building a native image where things have to be known at build time. The The only thing I can see us doing is failing early if we detect a shutdown method on a type that isn't concrete. Flagging for team attention to see what the rest of the team thinks. |
@joshlong, are you aware that the You can disable that inference by annotating your bean factory method with If you run the following test class, you will not see @SpringJUnitConfig
class ShutdownTests {
@Test
void test() {
}
@Configuration
static class Config {
@Bean(destroyMethod = "")
Controller myController() {
return new ControllerManager();
}
}
interface Controller {
void shutdown();
}
static class ControllerManager implements Controller {
@Override
public void shutdown() {
System.err.println("ControllerManager#shutdown() invoked!");
}
}
} |
Hello, I am facing a similar issue while trying to migrate an application to GraalVM native image and would like to give an alternate view as this issue appears to me to be much more troublesome then mentioned here. I am trying to migrate an application using Spring Boot 3.0.4 and Hazelcast 5.2.1. When I boot up the application, I get a similar error for {
"name": "com.hazelcast.core.HazelcastInstance",
"methods": [
{
"name": "shutdown",
"parameterTypes": [ ]
}
]
} Possible workarounds:
Adding the fact there are probably many other auto-configurations that might cause similar issues, I think it's best if this can be solved within Spring. Cause and possible solution: After some trials with GraalVM native images, I have found that defining an interface in reflect-config does work when accessing the method trough the interface but not from the concrete type. Example: reflect-config.json: [
{
"name": "org.example.Printer",
"methods": [
{
"name": "print",
"parameterTypes": [ ]
}
]
}
] interface Printer {
fun print()
}
class HelloWorldPrinter: Printer {
override fun print() {
println("Hello world")
}
}
val printer: Printer = HelloWorldPrinter()
// This works
Printer::class.java.getMethod("print").invoke(printer)
// This doesn't work
HelloWorldPrinter::class.java.getMethod("print").invoke(printer) It looks like Spring is using the second approach while searching for destroy methods. Transitioning to the first approach would most likely fix this issue. The bean definition type looks like is present in |
After this commit, DisposableBeanAdapter can find destruction related methods even when hints are just specified at interface level, which is typically the case when a bean is exposed via one of its interfaces. Closes spring-projectsgh-29545
I have a spring boot 3 snapshots app with nothing on the classpath save
spring-boot-starter
with spring boot 3 aot and graalvm apps.if I use the code:
and then compile/run in a graalvm application, I get the following error:
If I return
ControllerManager
from the@Bean
method, that works.It's still pretty dissatisfying when I have a builder that returns
Controller
, and not the subclass, and I have no way of knowing what subclass it is. Plus, it works on the JVM with no issues even with just the interface.I know there is a best practice that we should use direct subclasses in
@Bean
method return types, but this feels like a different kind of bug, especially sinceshutdown
is visible on the interface and the subclass.The other issue here is that it is not my intent that this
shutdown
be used to clean up the bean when the application context shuts down. That is, I don’t want this to be used as the spring beandestroyMethod
; it’s just a method that happened to be on the class. So to get these inscrutable errors aboutInvalid destruction signature
is weird.The text was updated successfully, but these errors were encountered: