Skip to content

Prevent initializers from being applied twice in AOT mode #32262

Open
@sdeleuze

Description

@sdeleuze

Running the following application prints 2 times Foo constructor at startup while a single time as expected in regular mode.

@SpringBootApplication
public class CommandlinerunnerApplication {

	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(CommandlinerunnerApplication.class);
		app.addInitializers((ApplicationContextInitializer<GenericApplicationContext>) context ->
				context.registerBean(Foo.class));
		app.run(args);
	}
}

public class Foo {

	public Foo() {
		System.out.println("Foo constructor");
	}
}

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 that SpringApplication#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 requires ExecutableMode#INVOKE reflection hints (not inferred because not needed by the AOT generated code which is fine with ExecutableMode#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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: blockedAn issue that's blocked on an external project changetheme: aotAn issue related to Ahead-of-time processingtype: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions