Skip to content

Commit 04944a1

Browse files
committed
Modernize the lazy-initialized beans refdoc section
Closes gh-32767
1 parent c6459b4 commit 04944a1

File tree

9 files changed

+197
-16
lines changed

9 files changed

+197
-16
lines changed

Diff for: framework-docs/modules/ROOT/pages/core/beans/dependencies/factory-lazy-init.adoc

+8-16
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,25 @@ pre-instantiation of a singleton bean by marking the bean definition as being
1010
lazy-initialized. A lazy-initialized bean tells the IoC container to create a bean
1111
instance when it is first requested, rather than at startup.
1212

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:
1515

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]
2117

2218
When the preceding configuration is consumed by an `ApplicationContext`, the `lazy` bean
2319
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.
2521

2622
However, when a lazy-initialized bean is a dependency of a singleton bean that is
2723
not lazy-initialized, the `ApplicationContext` creates the lazy-initialized bean at
2824
startup, because it must satisfy the singleton's dependencies. The lazy-initialized bean
2925
is injected into a singleton bean elsewhere that is not lazy-initialized.
3026

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:
3330

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]
4032

4133

4234

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.beans.dependencies.beansfactorylazyinit;
18+
19+
public class AnotherBean {
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.beans.dependencies.beansfactorylazyinit;
18+
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.context.annotation.Lazy;
22+
23+
@Configuration
24+
public class ApplicationConfiguration {
25+
26+
// tag::snippet[]
27+
@Bean
28+
@Lazy
29+
ExpensiveToCreateBean lazy() {
30+
return new ExpensiveToCreateBean();
31+
}
32+
33+
@Bean
34+
AnotherBean notLazy() {
35+
return new AnotherBean();
36+
}
37+
// end::snippet[]
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.beans.dependencies.beansfactorylazyinit;
18+
19+
public class ExpensiveToCreateBean {
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.beans.dependencies.beansfactorylazyinit;
18+
19+
import org.springframework.context.annotation.Configuration;
20+
import org.springframework.context.annotation.Lazy;
21+
22+
// tag::snippet[]
23+
@Configuration
24+
@Lazy
25+
public class LazyConfiguration {
26+
// No bean will be pre-instantiated...
27+
}
28+
// end::snippet[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.docs.core.beans.dependencies.beansfactorylazyinit
18+
19+
import org.springframework.context.annotation.Bean
20+
import org.springframework.context.annotation.Configuration
21+
import org.springframework.context.annotation.Lazy
22+
23+
@Configuration
24+
class ApplicationConfiguration {
25+
26+
// tag::snippet[]
27+
@Bean
28+
@Lazy
29+
fun lazy(): ExpensiveToCreateBean {
30+
return ExpensiveToCreateBean()
31+
}
32+
33+
@Bean
34+
fun notLazy(): AnotherBean {
35+
return AnotherBean()
36+
}
37+
// end::snippet[]
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.docs.core.beans.dependencies.beansfactorylazyinit
17+
18+
import org.springframework.context.annotation.Configuration
19+
import org.springframework.context.annotation.Lazy
20+
21+
// tag::snippet[]
22+
@Configuration
23+
@Lazy
24+
class LazyConfiguration {
25+
// No bean will be pre-instantiated...
26+
}
27+
// end::snippet[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<beans xmlns="http://www.springframework.org/schema/beans"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://www.springframework.org/schema/beans
4+
https://www.springframework.org/schema/beans/spring-beans.xsd">
5+
6+
<!-- tag::snippet[] -->
7+
<bean id="lazy" class="com.something.ExpensiveToCreateBean" lazy-init="true"/>
8+
9+
<bean name="notLazy" class="com.something.AnotherBean"/>
10+
<!-- end::snippet[] -->
11+
12+
</beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!-- tag::snippet[] -->
2+
<beans default-lazy-init="true">
3+
4+
<!-- No bean will be pre-instantiated... -->
5+
</beans>
6+
<!-- end::snippet[] -->

0 commit comments

Comments
 (0)