Skip to content

Commit 7d126d3

Browse files
committed
Improve documentation regarding "annotated classes"
See gh-23638
1 parent e9dc516 commit 7d126d3

File tree

11 files changed

+201
-182
lines changed

11 files changed

+201
-182
lines changed

spring-context/src/main/java/org/springframework/context/annotation/AnnotatedBeanDefinitionReader.java

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -34,8 +34,9 @@
3434
import org.springframework.util.Assert;
3535

3636
/**
37-
* Convenient adapter for programmatic registration of annotated bean classes.
38-
* This is an alternative to {@link ClassPathBeanDefinitionScanner}, applying
37+
* Convenient adapter for programmatic registration of bean classes.
38+
*
39+
* <p>This is an alternative to {@link ClassPathBeanDefinitionScanner}, applying
3940
* the same resolution of annotations but for explicitly registered classes only.
4041
*
4142
* @author Juergen Hoeller
@@ -58,7 +59,7 @@ public class AnnotatedBeanDefinitionReader {
5859

5960
/**
6061
* Create a new {@code AnnotatedBeanDefinitionReader} for the given registry.
61-
* If the registry is {@link EnvironmentCapable}, e.g. is an {@code ApplicationContext},
62+
* <p>If the registry is {@link EnvironmentCapable}, e.g. is an {@code ApplicationContext},
6263
* the {@link Environment} will be inherited, otherwise a new
6364
* {@link StandardEnvironment} will be created and used.
6465
* @param registry the {@code BeanFactory} to load bean definitions into,
@@ -71,8 +72,8 @@ public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry) {
7172
}
7273

7374
/**
74-
* Create a new {@code AnnotatedBeanDefinitionReader} for the given registry and using
75-
* the given {@link Environment}.
75+
* Create a new {@code AnnotatedBeanDefinitionReader} for the given registry,
76+
* using the given {@link Environment}.
7677
* @param registry the {@code BeanFactory} to load bean definitions into,
7778
* in the form of a {@code BeanDefinitionRegistry}
7879
* @param environment the {@code Environment} to use when evaluating bean definition
@@ -89,14 +90,14 @@ public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environmen
8990

9091

9192
/**
92-
* Return the BeanDefinitionRegistry that this scanner operates on.
93+
* Get the BeanDefinitionRegistry that this reader operates on.
9394
*/
9495
public final BeanDefinitionRegistry getRegistry() {
9596
return this.registry;
9697
}
9798

9899
/**
99-
* Set the Environment to use when evaluating whether
100+
* Set the {@code Environment} to use when evaluating whether
100101
* {@link Conditional @Conditional}-annotated component classes should be registered.
101102
* <p>The default is a {@link StandardEnvironment}.
102103
* @see #registerBean(Class, String, Class...)
@@ -106,15 +107,15 @@ public void setEnvironment(Environment environment) {
106107
}
107108

108109
/**
109-
* Set the BeanNameGenerator to use for detected bean classes.
110+
* Set the {@code BeanNameGenerator} to use for detected bean classes.
110111
* <p>The default is a {@link AnnotationBeanNameGenerator}.
111112
*/
112113
public void setBeanNameGenerator(@Nullable BeanNameGenerator beanNameGenerator) {
113114
this.beanNameGenerator = (beanNameGenerator != null ? beanNameGenerator : new AnnotationBeanNameGenerator());
114115
}
115116

116117
/**
117-
* Set the ScopeMetadataResolver to use for detected bean classes.
118+
* Set the {@code ScopeMetadataResolver} to use for registered component classes.
118119
* <p>The default is an {@link AnnotationScopeMetadataResolver}.
119120
*/
120121
public void setScopeMetadataResolver(@Nullable ScopeMetadataResolver scopeMetadataResolver) {
@@ -124,83 +125,83 @@ public void setScopeMetadataResolver(@Nullable ScopeMetadataResolver scopeMetada
124125

125126

126127
/**
127-
* Register one or more annotated classes to be processed.
128+
* Register one or more component classes to be processed.
128129
* <p>Calls to {@code register} are idempotent; adding the same
129-
* annotated class more than once has no additional effect.
130-
* @param annotatedClasses one or more annotated classes,
130+
* component class more than once has no additional effect.
131+
* @param componentClasses one or more component classes,
131132
* e.g. {@link Configuration @Configuration} classes
132133
*/
133-
public void register(Class<?>... annotatedClasses) {
134-
for (Class<?> annotatedClass : annotatedClasses) {
135-
registerBean(annotatedClass);
134+
public void register(Class<?>... componentClasses) {
135+
for (Class<?> componentClass : componentClasses) {
136+
registerBean(componentClass);
136137
}
137138
}
138139

139140
/**
140141
* Register a bean from the given bean class, deriving its metadata from
141142
* class-declared annotations.
142-
* @param annotatedClass the class of the bean
143+
* @param beanClass the class of the bean
143144
*/
144-
public void registerBean(Class<?> annotatedClass) {
145-
doRegisterBean(annotatedClass, null, null, null);
145+
public void registerBean(Class<?> beanClass) {
146+
doRegisterBean(beanClass, null, null, null);
146147
}
147148

148149
/**
149150
* Register a bean from the given bean class, deriving its metadata from
150151
* class-declared annotations, using the given supplier for obtaining a new
151152
* instance (possibly declared as a lambda expression or method reference).
152-
* @param annotatedClass the class of the bean
153+
* @param beanClass the class of the bean
153154
* @param instanceSupplier a callback for creating an instance of the bean
154155
* (may be {@code null})
155156
* @since 5.0
156157
*/
157-
public <T> void registerBean(Class<T> annotatedClass, @Nullable Supplier<T> instanceSupplier) {
158-
doRegisterBean(annotatedClass, instanceSupplier, null, null);
158+
public <T> void registerBean(Class<T> beanClass, @Nullable Supplier<T> instanceSupplier) {
159+
doRegisterBean(beanClass, instanceSupplier, null, null);
159160
}
160161

161162
/**
162163
* Register a bean from the given bean class, deriving its metadata from
163164
* class-declared annotations, using the given supplier for obtaining a new
164165
* instance (possibly declared as a lambda expression or method reference).
165-
* @param annotatedClass the class of the bean
166+
* @param beanClass the class of the bean
166167
* @param name an explicit name for the bean
167168
* @param instanceSupplier a callback for creating an instance of the bean
168169
* (may be {@code null})
169170
* @since 5.0
170171
*/
171-
public <T> void registerBean(Class<T> annotatedClass, String name, @Nullable Supplier<T> instanceSupplier) {
172-
doRegisterBean(annotatedClass, instanceSupplier, name, null);
172+
public <T> void registerBean(Class<T> beanClass, String name, @Nullable Supplier<T> instanceSupplier) {
173+
doRegisterBean(beanClass, instanceSupplier, name, null);
173174
}
174175

175176
/**
176177
* Register a bean from the given bean class, deriving its metadata from
177178
* class-declared annotations.
178-
* @param annotatedClass the class of the bean
179+
* @param beanClass the class of the bean
179180
* @param qualifiers specific qualifier annotations to consider,
180181
* in addition to qualifiers at the bean class level
181182
*/
182183
@SuppressWarnings("unchecked")
183-
public void registerBean(Class<?> annotatedClass, Class<? extends Annotation>... qualifiers) {
184-
doRegisterBean(annotatedClass, null, null, qualifiers);
184+
public void registerBean(Class<?> beanClass, Class<? extends Annotation>... qualifiers) {
185+
doRegisterBean(beanClass, null, null, qualifiers);
185186
}
186187

187188
/**
188189
* Register a bean from the given bean class, deriving its metadata from
189190
* class-declared annotations.
190-
* @param annotatedClass the class of the bean
191+
* @param beanClass the class of the bean
191192
* @param name an explicit name for the bean
192193
* @param qualifiers specific qualifier annotations to consider,
193194
* in addition to qualifiers at the bean class level
194195
*/
195196
@SuppressWarnings("unchecked")
196-
public void registerBean(Class<?> annotatedClass, String name, Class<? extends Annotation>... qualifiers) {
197-
doRegisterBean(annotatedClass, null, name, qualifiers);
197+
public void registerBean(Class<?> beanClass, String name, Class<? extends Annotation>... qualifiers) {
198+
doRegisterBean(beanClass, null, name, qualifiers);
198199
}
199200

200201
/**
201202
* Register a bean from the given bean class, deriving its metadata from
202203
* class-declared annotations.
203-
* @param annotatedClass the class of the bean
204+
* @param beanClass the class of the bean
204205
* @param instanceSupplier a callback for creating an instance of the bean
205206
* (may be {@code null})
206207
* @param name an explicit name for the bean
@@ -210,10 +211,10 @@ public void registerBean(Class<?> annotatedClass, String name, Class<? extends A
210211
* factory's {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
211212
* @since 5.0
212213
*/
213-
<T> void doRegisterBean(Class<T> annotatedClass, @Nullable Supplier<T> instanceSupplier, @Nullable String name,
214+
<T> void doRegisterBean(Class<T> beanClass, @Nullable Supplier<T> instanceSupplier, @Nullable String name,
214215
@Nullable Class<? extends Annotation>[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) {
215216

216-
AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
217+
AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(beanClass);
217218
if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
218219
return;
219220
}

spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -27,19 +27,20 @@
2727
import org.springframework.util.Assert;
2828

2929
/**
30-
* Standalone application context, accepting annotated classes as input - in particular
31-
* {@link Configuration @Configuration}-annotated classes, but also plain
30+
* Standalone application context, accepting <em>component classes</em> as input &mdash;
31+
* in particular {@link Configuration @Configuration}-annotated classes, but also plain
3232
* {@link org.springframework.stereotype.Component @Component} types and JSR-330 compliant
33-
* classes using {@code javax.inject} annotations. Allows for registering classes one by
34-
* one using {@link #register(Class...)} as well as for classpath scanning using
35-
* {@link #scan(String...)}.
33+
* classes using {@code javax.inject} annotations.
3634
*
37-
* <p>In case of multiple {@code @Configuration} classes, @{@link Bean} methods defined in
38-
* later classes will override those defined in earlier classes. This can be leveraged to
39-
* deliberately override certain bean definitions via an extra {@code @Configuration}
40-
* class.
35+
* <p>Allows for registering classes one by one using {@link #register(Class...)}
36+
* as well as for classpath scanning using {@link #scan(String...)}.
4137
*
42-
* <p>See @{@link Configuration}'s javadoc for usage examples.
38+
* <p>In case of multiple {@code @Configuration} classes, {@link Bean @Bean} methods
39+
* defined in later classes will override those defined in earlier classes. This can
40+
* be leveraged to deliberately override certain bean definitions via an extra
41+
* {@code @Configuration} class.
42+
*
43+
* <p>See {@link Configuration @Configuration}'s javadoc for usage examples.
4344
*
4445
* @author Juergen Hoeller
4546
* @author Chris Beams
@@ -78,20 +79,21 @@ public AnnotationConfigApplicationContext(DefaultListableBeanFactory beanFactory
7879

7980
/**
8081
* Create a new AnnotationConfigApplicationContext, deriving bean definitions
81-
* from the given annotated classes and automatically refreshing the context.
82-
* @param annotatedClasses one or more annotated classes,
83-
* e.g. {@link Configuration @Configuration} classes
82+
* from the given component classes and automatically refreshing the context.
83+
* @param componentClasses one or more component classes &mdash; for example,
84+
* {@link Configuration @Configuration} classes
8485
*/
85-
public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
86+
public AnnotationConfigApplicationContext(Class<?>... componentClasses) {
8687
this();
87-
register(annotatedClasses);
88+
register(componentClasses);
8889
refresh();
8990
}
9091

9192
/**
92-
* Create a new AnnotationConfigApplicationContext, scanning for bean definitions
93-
* in the given packages and automatically refreshing the context.
94-
* @param basePackages the packages to check for annotated classes
93+
* Create a new AnnotationConfigApplicationContext, scanning for components
94+
* in the given packages, registering bean definitions for those components,
95+
* and automatically refreshing the context.
96+
* @param basePackages the packages to scan for component classes
9597
*/
9698
public AnnotationConfigApplicationContext(String... basePackages) {
9799
this();
@@ -101,7 +103,7 @@ public AnnotationConfigApplicationContext(String... basePackages) {
101103

102104

103105
/**
104-
* Propagates the given custom {@code Environment} to the underlying
106+
* Propagate the given custom {@code Environment} to the underlying
105107
* {@link AnnotatedBeanDefinitionReader} and {@link ClassPathBeanDefinitionScanner}.
106108
*/
107109
@Override
@@ -128,7 +130,7 @@ public void setBeanNameGenerator(BeanNameGenerator beanNameGenerator) {
128130
}
129131

130132
/**
131-
* Set the {@link ScopeMetadataResolver} to use for detected bean classes.
133+
* Set the {@link ScopeMetadataResolver} to use for registered component classes.
132134
* <p>The default is an {@link AnnotationScopeMetadataResolver}.
133135
* <p>Any call to this method must occur prior to calls to {@link #register(Class...)}
134136
* and/or {@link #scan(String...)}.
@@ -144,27 +146,29 @@ public void setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver
144146
//---------------------------------------------------------------------
145147

146148
/**
147-
* Register one or more annotated classes to be processed.
149+
* Register one or more component classes to be processed.
148150
* <p>Note that {@link #refresh()} must be called in order for the context
149151
* to fully process the new classes.
150-
* @param annotatedClasses one or more annotated classes,
151-
* e.g. {@link Configuration @Configuration} classes
152+
* @param componentClasses one or more component classes &mdash; for example,
153+
* {@link Configuration @Configuration} classes
152154
* @see #scan(String...)
153155
* @see #refresh()
154156
*/
155-
public void register(Class<?>... annotatedClasses) {
156-
Assert.notEmpty(annotatedClasses, "At least one annotated class must be specified");
157-
this.reader.register(annotatedClasses);
157+
@Override
158+
public void register(Class<?>... componentClasses) {
159+
Assert.notEmpty(componentClasses, "At least one component class must be specified");
160+
this.reader.register(componentClasses);
158161
}
159162

160163
/**
161164
* Perform a scan within the specified base packages.
162165
* <p>Note that {@link #refresh()} must be called in order for the context
163166
* to fully process the new classes.
164-
* @param basePackages the packages to check for annotated classes
167+
* @param basePackages the packages to scan for component classes
165168
* @see #register(Class...)
166169
* @see #refresh()
167170
*/
171+
@Override
168172
public void scan(String... basePackages) {
169173
Assert.notEmpty(basePackages, "At least one base package must be specified");
170174
this.scanner.scan(basePackages);
@@ -180,31 +184,31 @@ public void scan(String... basePackages) {
180184
* class-declared annotations, and optionally providing explicit constructor
181185
* arguments for consideration in the autowiring process.
182186
* <p>The bean name will be generated according to annotated component rules.
183-
* @param annotatedClass the class of the bean
187+
* @param beanClass the class of the bean
184188
* @param constructorArguments argument values to be fed into Spring's
185189
* constructor resolution algorithm, resolving either all arguments or just
186190
* specific ones, with the rest to be resolved through regular autowiring
187191
* (may be {@code null} or empty)
188192
* @since 5.0
189193
*/
190-
public <T> void registerBean(Class<T> annotatedClass, Object... constructorArguments) {
191-
registerBean(null, annotatedClass, constructorArguments);
194+
public <T> void registerBean(Class<T> beanClass, Object... constructorArguments) {
195+
registerBean(null, beanClass, constructorArguments);
192196
}
193197

194198
/**
195199
* Register a bean from the given bean class, deriving its metadata from
196200
* class-declared annotations, and optionally providing explicit constructor
197201
* arguments for consideration in the autowiring process.
198202
* @param beanName the name of the bean (may be {@code null})
199-
* @param annotatedClass the class of the bean
203+
* @param beanClass the class of the bean
200204
* @param constructorArguments argument values to be fed into Spring's
201205
* constructor resolution algorithm, resolving either all arguments or just
202206
* specific ones, with the rest to be resolved through regular autowiring
203207
* (may be {@code null} or empty)
204208
* @since 5.0
205209
*/
206-
public <T> void registerBean(@Nullable String beanName, Class<T> annotatedClass, Object... constructorArguments) {
207-
this.reader.doRegisterBean(annotatedClass, null, beanName, null,
210+
public <T> void registerBean(@Nullable String beanName, Class<T> beanClass, Object... constructorArguments) {
211+
this.reader.doRegisterBean(beanClass, null, beanName, null,
208212
bd -> {
209213
for (Object arg : constructorArguments) {
210214
bd.getConstructorArgumentValues().addGenericArgumentValue(arg);

spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigRegistry.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -26,17 +26,17 @@
2626
public interface AnnotationConfigRegistry {
2727

2828
/**
29-
* Register one or more annotated classes to be processed.
29+
* Register one or more component classes to be processed.
3030
* <p>Calls to {@code register} are idempotent; adding the same
31-
* annotated class more than once has no additional effect.
32-
* @param annotatedClasses one or more annotated classes,
31+
* component class more than once has no additional effect.
32+
* @param componentClasses one or more component classes,
3333
* e.g. {@link Configuration @Configuration} classes
3434
*/
35-
void register(Class<?>... annotatedClasses);
35+
void register(Class<?>... componentClasses);
3636

3737
/**
3838
* Perform a scan within the specified base packages.
39-
* @param basePackages the packages to check for annotated classes
39+
* @param basePackages the packages to scan for component classes
4040
*/
4141
void scan(String... basePackages);
4242

spring-context/src/main/java/org/springframework/context/annotation/Configuration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,8 @@
352352
*
353353
* <p>The Spring <em>TestContext framework</em> available in the {@code spring-test} module
354354
* provides the {@code @ContextConfiguration} annotation which can accept an array of
355-
* {@code @Configuration} {@code Class} objects:
355+
* <em>component class</em> references &mdash; typically {@code @Configuration} or
356+
* {@code @Component} classes.
356357
*
357358
* <pre class="code">
358359
* &#064;RunWith(SpringRunner.class)

0 commit comments

Comments
 (0)