spring-projects / spring-projects/spring-boot

Align the time at which AOT-based context initialization is performed more closely with the standard case

Open
#41,600 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

theme: aot type: enhancement
Dominant language
Java
Stars
81.5k
Forks
42.7k
Avg merge
2d 4h
Merged PRs (30d)
65

Description

We currently add a custom initialiser for AOT and place it first in the application's list of initializers:

https://github.com/spring-projects/spring-boot/blob/12ec18fd31ecc4c9e2f777fe17ef20b4ad6261b1/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java#L437-L449

A consequence of this is that the use of AOT changes the state of the context when the other initialisers are called. In the standard non-AOT case, the other initializers see an almost empty context that only contains its standard beans. In the AOT case, the other initializers see all of the application's bean. We should try to address this and reduce the difference between the two. The simplest option would be to add the AOT initializer last so that it runs after any other initializers but this doesn't minimise the difference.

In the standard case, the application's beans start to appear in the context when SpringApplication loads the sources. This is done towards the end of prepareContext and immediately before contextLoaded is called on the run listeners. This will typically result in the application's main class being registered as a bean. All of the other beans are then registered as part of refreshing the context which SpringApplication does immediately after calling contextLoaded. In the AOT case, where the initialization happens much earlier, all of the application's beans are visible to:

  • All other context initializers
  • The context prepared callback, including listeners to the ApplicationContextInitializedEvent
  • The context loaded callback, including listeners to the ApplicationPreparedEvent

To minimise the difference between the standard case and the AOT case, I think the AOT initializer should be applied to the context either immediately before contextLoaded is called on the run listeners or immediately before refresh is called on the context. The former will result in the listeners seeing more beans in the context in the AOT case. The latter will result in the listeners not seeing the beans for the application's sources in the AOT case.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in SpringApplication.java at the linked custom AOT initializer and trace prepareContext, contextLoaded, and refresh. Compare when application beans become visible in standard and AOT startup, then choose and implement an initialization point that reduces the difference described in the issue. Done means the AOT path no longer exposes all application beans to the earlier initializer and callback stages.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring-boot
Domain
backend
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.