@@ -10,33 +10,25 @@ pre-instantiation of a singleton bean by marking the bean definition as being
10
10
lazy-initialized. A lazy-initialized bean tells the IoC container to create a bean
11
11
instance when it is first requested, rather than at startup.
12
12
13
- In XML, this behavior is controlled by the `lazy-init` attribute on the `<bean/>`
14
- element, as the following example shows:
13
+ This behavior is controlled by the `@Lazy` annotation or in XML the ` lazy-init` attribute on the `<bean/>` element, as
14
+ the following example shows:
15
15
16
- [source,xml,indent=0,subs="verbatim,quotes"]
17
- ----
18
- <bean id="lazy" class="com.something.ExpensiveToCreateBean" lazy-init="true"/>
19
- <bean name="not.lazy" class="com.something.AnotherBean"/>
20
- ----
16
+ include-code::./ApplicationConfiguration[tag=snippet,indent=0]
21
17
22
18
When the preceding configuration is consumed by an `ApplicationContext`, the `lazy` bean
23
19
is not eagerly pre-instantiated when the `ApplicationContext` starts,
24
- whereas the `not.lazy` bean is eagerly pre-instantiated.
20
+ whereas the `notLazy` one is eagerly pre-instantiated.
25
21
26
22
However, when a lazy-initialized bean is a dependency of a singleton bean that is
27
23
not lazy-initialized, the `ApplicationContext` creates the lazy-initialized bean at
28
24
startup, because it must satisfy the singleton's dependencies. The lazy-initialized bean
29
25
is injected into a singleton bean elsewhere that is not lazy-initialized.
30
26
31
- You can also control lazy-initialization at the container level by using the
32
- `default-lazy-init` attribute on the `<beans/>` element, as the following example shows:
27
+ You can also control lazy-initialization for a set of beans by using the `@Lazy` annotation on your `@Configuration`
28
+ annotated class or in XML using the `default-lazy-init` attribute on the `<beans/>` element, as the following example
29
+ shows:
33
30
34
- [source,xml,indent=0,subs="verbatim,quotes"]
35
- ----
36
- <beans default-lazy-init="true">
37
- <!-- no beans will be pre-instantiated... -->
38
- </beans>
39
- ----
31
+ include-code::./LazyConfiguration[tag=snippet,indent=0]
40
32
41
33
42
34
0 commit comments