Skip to content

Commit 9de8367

Browse files
committed
Complete set of registerBean methods on AnnotatedBeanDefinitionReader
Includes bringing registerBean constructor-vararg variants up to GenericApplicationContext, making AnnotationConfigApplicationContext a straightforward subclass with a single template method to override. See gh-22457
1 parent b90e6ef commit 9de8367

File tree

3 files changed

+114
-83
lines changed

3 files changed

+114
-83
lines changed

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

Lines changed: 69 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -143,34 +143,19 @@ public void register(Class<?>... annotatedClasses) {
143143
* @param annotatedClass the class of the bean
144144
*/
145145
public void registerBean(Class<?> annotatedClass) {
146-
doRegisterBean(annotatedClass, null, null, null);
146+
doRegisterBean(annotatedClass, null, null, null, null);
147147
}
148148

149149
/**
150150
* Register a bean from the given bean class, deriving its metadata from
151-
* class-declared annotations, using the given supplier for obtaining a new
152-
* instance (possibly declared as a lambda expression or method reference).
153-
* @param annotatedClass the class of the bean
154-
* @param instanceSupplier a callback for creating an instance of the bean
155-
* (may be {@code null})
156-
* @since 5.0
157-
*/
158-
public <T> void registerBean(Class<T> annotatedClass, @Nullable Supplier<T> instanceSupplier) {
159-
doRegisterBean(annotatedClass, instanceSupplier, null, null);
160-
}
161-
162-
/**
163-
* Register a bean from the given bean class, deriving its metadata from
164-
* class-declared annotations, using the given supplier for obtaining a new
165-
* instance (possibly declared as a lambda expression or method reference).
151+
* class-declared annotations.
166152
* @param annotatedClass the class of the bean
167153
* @param name an explicit name for the bean
168-
* @param instanceSupplier a callback for creating an instance of the bean
169-
* (may be {@code null})
170-
* @since 5.0
154+
* (or {@code null} for generating a default bean name)
155+
* @since 5.2
171156
*/
172-
public <T> void registerBean(Class<T> annotatedClass, String name, @Nullable Supplier<T> instanceSupplier) {
173-
doRegisterBean(annotatedClass, instanceSupplier, name, null);
157+
public void registerBean(Class<?> annotatedClass, @Nullable String name) {
158+
doRegisterBean(annotatedClass, name, null, null, null);
174159
}
175160

176161
/**
@@ -182,44 +167,94 @@ public <T> void registerBean(Class<T> annotatedClass, String name, @Nullable Sup
182167
*/
183168
@SuppressWarnings("unchecked")
184169
public void registerBean(Class<?> annotatedClass, Class<? extends Annotation>... qualifiers) {
185-
doRegisterBean(annotatedClass, null, null, qualifiers);
170+
doRegisterBean(annotatedClass, null, qualifiers, null, null);
186171
}
187172

188173
/**
189174
* Register a bean from the given bean class, deriving its metadata from
190175
* class-declared annotations.
191176
* @param annotatedClass the class of the bean
192177
* @param name an explicit name for the bean
178+
* (or {@code null} for generating a default bean name)
193179
* @param qualifiers specific qualifier annotations to consider,
194180
* in addition to qualifiers at the bean class level
195181
*/
196182
@SuppressWarnings("unchecked")
197-
public void registerBean(Class<?> annotatedClass, String name, Class<? extends Annotation>... qualifiers) {
198-
doRegisterBean(annotatedClass, null, name, qualifiers);
183+
public void registerBean(Class<?> annotatedClass, @Nullable String name,
184+
Class<? extends Annotation>... qualifiers) {
185+
186+
doRegisterBean(annotatedClass, name, qualifiers, null, null);
187+
}
188+
189+
/**
190+
* Register a bean from the given bean class, deriving its metadata from
191+
* class-declared annotations, using the given supplier for obtaining a new
192+
* instance (possibly declared as a lambda expression or method reference).
193+
* @param annotatedClass the class of the bean
194+
* @param supplier a callback for creating an instance of the bean
195+
* (may be {@code null})
196+
* @since 5.0
197+
*/
198+
public <T> void registerBean(Class<T> annotatedClass, @Nullable Supplier<T> supplier) {
199+
doRegisterBean(annotatedClass, null, null, supplier, null);
200+
}
201+
202+
/**
203+
* Register a bean from the given bean class, deriving its metadata from
204+
* class-declared annotations, using the given supplier for obtaining a new
205+
* instance (possibly declared as a lambda expression or method reference).
206+
* @param annotatedClass the class of the bean
207+
* @param name an explicit name for the bean
208+
* (or {@code null} for generating a default bean name)
209+
* @param supplier a callback for creating an instance of the bean
210+
* (may be {@code null})
211+
* @since 5.0
212+
*/
213+
public <T> void registerBean(Class<T> annotatedClass, @Nullable String name, @Nullable Supplier<T> supplier) {
214+
doRegisterBean(annotatedClass, name, null, supplier, null);
199215
}
200216

201217
/**
202218
* Register a bean from the given bean class, deriving its metadata from
203219
* class-declared annotations.
204220
* @param annotatedClass the class of the bean
205-
* @param instanceSupplier a callback for creating an instance of the bean
221+
* @param name an explicit name for the bean
222+
* (or {@code null} for generating a default bean name)
223+
* @param supplier a callback for creating an instance of the bean
206224
* (may be {@code null})
225+
* @param customizers one or more callbacks for customizing the factory's
226+
* {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
227+
* @since 5.2
228+
*/
229+
public <T> void registerBean(Class<T> annotatedClass, @Nullable String name, @Nullable Supplier<T> supplier,
230+
BeanDefinitionCustomizer... customizers) {
231+
232+
doRegisterBean(annotatedClass, name, null, supplier, customizers);
233+
}
234+
235+
/**
236+
* Register a bean from the given bean class, deriving its metadata from
237+
* class-declared annotations.
238+
* @param annotatedClass the class of the bean
207239
* @param name an explicit name for the bean
240+
* @param supplier a callback for creating an instance of the bean
241+
* (may be {@code null})
208242
* @param qualifiers specific qualifier annotations to consider, if any,
209243
* in addition to qualifiers at the bean class level
210-
* @param definitionCustomizers one or more callbacks for customizing the
211-
* factory's {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
244+
* @param customizers one or more callbacks for customizing the factory's
245+
* {@link BeanDefinition}, e.g. setting a lazy-init or primary flag
212246
* @since 5.0
213247
*/
214-
<T> void doRegisterBean(Class<T> annotatedClass, @Nullable Supplier<T> instanceSupplier, @Nullable String name,
215-
@Nullable Class<? extends Annotation>[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) {
248+
private <T> void doRegisterBean(Class<T> annotatedClass, @Nullable String name,
249+
@Nullable Class<? extends Annotation>[] qualifiers, @Nullable Supplier<T> supplier,
250+
@Nullable BeanDefinitionCustomizer[] customizers) {
216251

217252
AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
218253
if (this.conditionEvaluator.shouldSkip(abd.getMetadata())) {
219254
return;
220255
}
221256

222-
abd.setInstanceSupplier(instanceSupplier);
257+
abd.setInstanceSupplier(supplier);
223258
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(abd);
224259
abd.setScope(scopeMetadata.getScopeName());
225260
String beanName = (name != null ? name : this.beanNameGenerator.generateBeanName(abd, this.registry));
@@ -238,8 +273,10 @@ else if (Lazy.class == qualifier) {
238273
}
239274
}
240275
}
241-
for (BeanDefinitionCustomizer customizer : definitionCustomizers) {
242-
customizer.customize(abd);
276+
if (customizers != null) {
277+
for (BeanDefinitionCustomizer customizer : customizers) {
278+
customizer.customize(abd);
279+
}
243280
}
244281

245282
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);

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

Lines changed: 5 additions & 42 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.
@@ -172,51 +172,14 @@ public void scan(String... basePackages) {
172172

173173

174174
//---------------------------------------------------------------------
175-
// Convenient methods for registering individual beans
175+
// Adapt superclass registerBean calls to AnnotatedBeanDefinitionReader
176176
//---------------------------------------------------------------------
177177

178-
/**
179-
* Register a bean from the given bean class, deriving its metadata from
180-
* class-declared annotations, and optionally providing explicit constructor
181-
* arguments for consideration in the autowiring process.
182-
* <p>The bean name will be generated according to annotated component rules.
183-
* @param annotatedClass the class of the bean
184-
* @param constructorArguments argument values to be fed into Spring's
185-
* constructor resolution algorithm, resolving either all arguments or just
186-
* specific ones, with the rest to be resolved through regular autowiring
187-
* (may be {@code null} or empty)
188-
* @since 5.0
189-
*/
190-
public <T> void registerBean(Class<T> annotatedClass, Object... constructorArguments) {
191-
registerBean(null, annotatedClass, constructorArguments);
192-
}
193-
194-
/**
195-
* Register a bean from the given bean class, deriving its metadata from
196-
* class-declared annotations, and optionally providing explicit constructor
197-
* arguments for consideration in the autowiring process.
198-
* @param beanName the name of the bean (may be {@code null})
199-
* @param annotatedClass the class of the bean
200-
* @param constructorArguments argument values to be fed into Spring's
201-
* constructor resolution algorithm, resolving either all arguments or just
202-
* specific ones, with the rest to be resolved through regular autowiring
203-
* (may be {@code null} or empty)
204-
* @since 5.0
205-
*/
206-
public <T> void registerBean(@Nullable String beanName, Class<T> annotatedClass, Object... constructorArguments) {
207-
this.reader.doRegisterBean(annotatedClass, null, beanName, null,
208-
bd -> {
209-
for (Object arg : constructorArguments) {
210-
bd.getConstructorArgumentValues().addGenericArgumentValue(arg);
211-
}
212-
});
213-
}
214-
215178
@Override
216-
public <T> void registerBean(@Nullable String beanName, Class<T> beanClass, @Nullable Supplier<T> supplier,
217-
BeanDefinitionCustomizer... customizers) {
179+
public <T> void registerBean(@Nullable String beanName, Class<T> beanClass,
180+
@Nullable Supplier<T> supplier, BeanDefinitionCustomizer... customizers) {
218181

219-
this.reader.doRegisterBean(beanClass, supplier, beanName, null, customizers);
182+
this.reader.registerBean(beanClass, beanName, supplier, customizers);
220183
}
221184

222185
}

spring-context/src/main/java/org/springframework/context/support/GenericApplicationContext.java

Lines changed: 40 additions & 9 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.
@@ -358,10 +358,43 @@ public boolean isAlias(String beanName) {
358358
// Convenient methods for registering individual beans
359359
//---------------------------------------------------------------------
360360

361+
/**
362+
* Register a bean from the given bean class, optionally providing explicit
363+
* constructor arguments for consideration in the autowiring process.
364+
* @param beanClass the class of the bean
365+
* @param constructorArgs custom argument values to be fed into Spring's
366+
* constructor resolution algorithm, resolving either all arguments or just
367+
* specific ones, with the rest to be resolved through regular autowiring
368+
* (may be {@code null} or empty)
369+
* @since 5.2 (since 5.0 on the AnnotationConfigApplicationContext subclass)
370+
*/
371+
public <T> void registerBean(Class<T> beanClass, Object... constructorArgs) {
372+
registerBean(null, beanClass, constructorArgs);
373+
}
374+
375+
/**
376+
* Register a bean from the given bean class, optionally providing explicit
377+
* constructor arguments for consideration in the autowiring process.
378+
* @param beanName the name of the bean (may be {@code null})
379+
* @param beanClass the class of the bean
380+
* @param constructorArgs custom argument values to be fed into Spring's
381+
* constructor resolution algorithm, resolving either all arguments or just
382+
* specific ones, with the rest to be resolved through regular autowiring
383+
* (may be {@code null} or empty)
384+
* @since 5.2 (since 5.0 on the AnnotationConfigApplicationContext subclass)
385+
*/
386+
public <T> void registerBean(@Nullable String beanName, Class<T> beanClass, Object... constructorArgs) {
387+
registerBean(beanName, beanClass, (Supplier<T>) null,
388+
bd -> {
389+
for (Object arg : constructorArgs) {
390+
bd.getConstructorArgumentValues().addGenericArgumentValue(arg);
391+
}
392+
});
393+
}
394+
361395
/**
362396
* Register a bean from the given bean class, optionally customizing its
363-
* bean definition metadata (typically declared as a lambda expression
364-
* or method reference).
397+
* bean definition metadata (typically declared as a lambda expression).
365398
* @param beanClass the class of the bean (resolving a public constructor
366399
* to be autowired, possibly simply the default constructor)
367400
* @param customizers one or more callbacks for customizing the factory's
@@ -374,10 +407,8 @@ public final <T> void registerBean(Class<T> beanClass, BeanDefinitionCustomizer.
374407
}
375408

376409
/**
377-
* Register a bean from the given bean class, using the given supplier for
378-
* obtaining a new instance (typically declared as a lambda expression or
379-
* method reference), optionally customizing its bean definition metadata
380-
* (again typically declared as a lambda expression or method reference).
410+
* Register a bean from the given bean class, optionally customizing its
411+
* bean definition metadata (typically declared as a lambda expression).
381412
* @param beanName the name of the bean (may be {@code null})
382413
* @param beanClass the class of the bean (resolving a public constructor
383414
* to be autowired, possibly simply the default constructor)
@@ -396,7 +427,7 @@ public final <T> void registerBean(
396427
* Register a bean from the given bean class, using the given supplier for
397428
* obtaining a new instance (typically declared as a lambda expression or
398429
* method reference), optionally customizing its bean definition metadata
399-
* (again typically declared as a lambda expression or method reference).
430+
* (again typically declared as a lambda expression).
400431
* @param beanClass the class of the bean
401432
* @param supplier a callback for creating an instance of the bean
402433
* @param customizers one or more callbacks for customizing the factory's
@@ -414,7 +445,7 @@ public final <T> void registerBean(
414445
* Register a bean from the given bean class, using the given supplier for
415446
* obtaining a new instance (typically declared as a lambda expression or
416447
* method reference), optionally customizing its bean definition metadata
417-
* (again typically declared as a lambda expression or method reference).
448+
* (again typically declared as a lambda expression).
418449
* <p>This method can be overridden to adapt the registration mechanism for
419450
* all {@code registerBean} methods (since they all delegate to this one).
420451
* @param beanName the name of the bean (may be {@code null})

0 commit comments

Comments
 (0)