Skip to content

Commit 89a10d5

Browse files
committed
Reference documentation for @bean(bootstrap=BACKGROUND)
See gh-13410
1 parent 4ac5216 commit 89a10d5

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,10 +503,42 @@ way, navigating `@Configuration` classes and their dependencies becomes no diffe
503503
than the usual process of navigating interface-based code.
504504
--
505505

506-
TIP: If you want to influence the startup creation order of certain beans, consider
507-
declaring some of them as `@Lazy` (for creation on first access instead of on startup)
508-
or as `@DependsOn` certain other beans (making sure that specific other beans are
509-
created before the current bean, beyond what the latter's direct dependencies imply).
506+
507+
[[beans-java-startup]]
508+
== Influencing the Startup of `@Bean`-defined Singletons
509+
510+
If you want to influence the startup creation order of certain singleton beans, consider
511+
declaring some of them as `@Lazy` for creation on first access instead of on startup.
512+
513+
`@DependsOn` forces certain other beans to be initialized first, making sure that
514+
the specified beans are created before the current bean, beyond what the latter's
515+
direct dependencies imply.
516+
517+
[[beans-java-startup-background]]
518+
=== Background Initialization
519+
520+
As of 6.2, there is a background initialization option: `@Bean(bootstrap=BACKGROUND)`
521+
allows for singling out specific beans for background initialization, covering the
522+
entire bean creation step for each such bean on context startup.
523+
524+
Dependent beans with non-lazy injection points automatically wait for the bean instance
525+
to be completed. All regular background initializations are forced to complete at the
526+
end of context startup. Only for beans additionally marked as `@Lazy`, the completion
527+
is allowed to happen later (up until first actual access).
528+
529+
Background initialization typically goes together with `@Lazy` (or `ObjectProvider`)
530+
injection points in dependent beans. Otherwise, the main bootstrap thread is going to
531+
block when an actual background-initialized bean instance needs to be injected early.
532+
533+
This form of concurrent startup applies to individual beans: If such a bean depends on
534+
other beans, they need to have been initialized already, either simply through being
535+
declared earlier or through `@DependsOn` which is enforcing initialization in the main
536+
bootstrap thread before background initialization for the affected bean is triggered.
537+
538+
Note that a `bootstrapExecutor` bean of type `Executor` needs to be declared for
539+
background bootstrapping to be actually active. Otherwise, the background markers are
540+
going to be ignored at runtime. The bootstrap executor may be a bounded executor just
541+
for startup purposes or a shared thread pool which serves for other purposes as well.
510542

511543

512544
[[beans-java-conditional]]

0 commit comments

Comments
 (0)