Note on multiple @Bean methods for same bean

Issue: SPR-13438
master
Juergen Hoeller 9 years ago
parent 48576f2815
commit 93f77f5d51
  1. 11
      src/asciidoc/core-beans.adoc

@ -5419,11 +5419,18 @@ factory methods as you see fit in non-`@Configuration` classes and also for stat
methods anywhere. However, regular `@Bean` methods in `@Configuration` classes need methods anywhere. However, regular `@Bean` methods in `@Configuration` classes need
to be overridable, i.e. they must not be declared as `private` or `final`. to be overridable, i.e. they must not be declared as `private` or `final`.
Finally, `@Bean` methods will also be discovered on base classes of a given component `@Bean` methods will also be discovered on base classes of a given component or
or configuration class, as well as on Java 8 default methods declared in interfaces configuration class, as well as on Java 8 default methods declared in interfaces
implemented by the component or configuration class. This allows for a lot of implemented by the component or configuration class. This allows for a lot of
flexibility in composing complex configuration arrangements, with even multiple flexibility in composing complex configuration arrangements, with even multiple
inheritance being possible through Java 8 default methods as of Spring 4.2. inheritance being possible through Java 8 default methods as of Spring 4.2.
Finally, note that a single class may hold multiple `@Bean` methods for the same
bean, as an arrangement of multiple factory methods to use depending on available
dependencies at runtime. This is the same algorithm as for choosing the "greediest"
constructor or factory method in other configuration scenarios: The variant with
the largest number of satisfiable dependencies will be picked at construction time,
analogous to how the container selects between multiple `@Autowired` constructors.
==== ====

Loading…
Cancel
Save