Skip to content

Commit 3f889e9

Browse files
committed
Clarify RootBeanDefinition vs GenericBeanDefinition
Closes gh-30444
1 parent 6622a3c commit 3f889e9

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/GenericBeanDefinition.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,15 +21,17 @@
2121
import org.springframework.util.ObjectUtils;
2222

2323
/**
24-
* GenericBeanDefinition is a one-stop shop for standard bean definition purposes.
25-
* Like any bean definition, it allows for specifying a class plus optionally
24+
* GenericBeanDefinition is a one-stop shop for declarative bean definition purposes.
25+
* Like all common bean definitions, it allows for specifying a class plus optionally
2626
* constructor argument values and property values. Additionally, deriving from a
2727
* parent bean definition can be flexibly configured through the "parentName" property.
2828
*
2929
* <p>In general, use this {@code GenericBeanDefinition} class for the purpose of
30-
* registering user-visible bean definitions (which a post-processor might operate on,
31-
* potentially even reconfiguring the parent name). Use {@code RootBeanDefinition} /
32-
* {@code ChildBeanDefinition} where parent/child relationships happen to be pre-determined.
30+
* registering declarative bean definitions (e.g. XML definitions which a bean
31+
* post-processor might operate on, potentially even reconfiguring the parent name).
32+
* Use {@code RootBeanDefinition}/{@code ChildBeanDefinition} where parent/child
33+
* relationships happen to be pre-determined, and prefer {@link RootBeanDefinition}
34+
* specifically for programmatic definitions derived from factory methods/suppliers.
3335
*
3436
* @author Juergen Hoeller
3537
* @since 2.5

spring-beans/src/main/java/org/springframework/beans/factory/support/RootBeanDefinition.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,23 @@
3535
import org.springframework.util.Assert;
3636

3737
/**
38-
* A root bean definition represents the merged bean definition that backs
39-
* a specific bean in a Spring BeanFactory at runtime. It might have been created
40-
* from multiple original bean definitions that inherit from each other,
41-
* typically registered as {@link GenericBeanDefinition GenericBeanDefinitions}.
38+
* A root bean definition represents the <b>merged bean definition at runtime</b>
39+
* that backs a specific bean in a Spring BeanFactory. It might have been created
40+
* from multiple original bean definitions that inherit from each other, e.g.
41+
* {@link GenericBeanDefinition GenericBeanDefinitions} from XML declarations.
4242
* A root bean definition is essentially the 'unified' bean definition view at runtime.
4343
*
44-
* <p>Root bean definitions may also be used for registering individual bean definitions
45-
* in the configuration phase. However, since Spring 2.5, the preferred way to register
46-
* bean definitions programmatically is the {@link GenericBeanDefinition} class.
47-
* GenericBeanDefinition has the advantage that it allows to dynamically define
48-
* parent dependencies, not 'hard-coding' the role as a root bean definition.
44+
* <p>Root bean definitions may also be used for <b>registering individual bean
45+
* definitions in the configuration phase.</b> This is particularly applicable for
46+
* programmatic definitions derived from factory methods (e.g. {@code @Bean} methods)
47+
* and instance suppliers (e.g. lambda expressions) which come with extra type metadata
48+
* (see {@link #setTargetType(ResolvableType)}/{@link #setResolvedFactoryMethod(Method)}).
49+
*
50+
* <p>Note: The preferred choice for bean definitions derived from declarative sources
51+
* (e.g. XML definitions) is the flexible {@link GenericBeanDefinition} variant.
52+
* GenericBeanDefinition comes with the advantage that it allows for dynamically
53+
* defining parent dependencies, not 'hard-coding' the role as a root bean definition,
54+
* even supporting parent relationship changes in the bean post-processor phase.
4955
*
5056
* @author Rod Johnson
5157
* @author Juergen Hoeller

0 commit comments

Comments
 (0)