Skip to content

Commit 18f2e6a

Browse files
committed
AnnotationConfigRegistry exposes registerBean with supplier/qualifiers
Closes gh-22457
1 parent eeed20d commit 18f2e6a

File tree

5 files changed

+297
-18
lines changed

5 files changed

+297
-18
lines changed

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

+38-1
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.
@@ -197,6 +197,43 @@ public void registerBean(Class<?> annotatedClass, String name, Class<? extends A
197197
doRegisterBean(annotatedClass, null, name, qualifiers);
198198
}
199199

200+
/**
201+
* Register a bean from the given bean class, deriving its metadata from
202+
* class-declared annotations, using the given supplier for obtaining a new
203+
* instance (possibly declared as a lambda expression or method reference).
204+
* @param annotatedClass the class of the bean
205+
* @param instanceSupplier a callback for creating an instance of the bean
206+
* (may be {@code null})
207+
* @param qualifiers specific qualifier annotations to consider,
208+
* in addition to qualifiers at the bean class level
209+
* @since 5.2
210+
*/
211+
@SuppressWarnings("unchecked")
212+
public <T> void registerBean(Class<T> annotatedClass, @Nullable Supplier<T> instanceSupplier,
213+
Class<? extends Annotation>... qualifiers) {
214+
215+
doRegisterBean(annotatedClass, instanceSupplier, null, qualifiers);
216+
}
217+
218+
/**
219+
* Register a bean from the given bean class, deriving its metadata from
220+
* class-declared annotations, using the given supplier for obtaining a new
221+
* instance (possibly declared as a lambda expression or method reference).
222+
* @param annotatedClass the class of the bean
223+
* @param name an explicit name for the bean
224+
* @param instanceSupplier a callback for creating an instance of the bean
225+
* (may be {@code null})
226+
* @param qualifiers specific qualifier annotations to consider,
227+
* in addition to qualifiers at the bean class level
228+
* @since 5.2
229+
*/
230+
@SuppressWarnings("unchecked")
231+
public <T> void registerBean(Class<T> annotatedClass, String name, @Nullable Supplier<T> instanceSupplier,
232+
Class<? extends Annotation>... qualifiers) {
233+
234+
doRegisterBean(annotatedClass, instanceSupplier, name, qualifiers);
235+
}
236+
200237
/**
201238
* Register a bean from the given bean class, deriving its metadata from
202239
* class-declared annotations.

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

+71-7
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.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.context.annotation;
1818

19+
import java.lang.annotation.Annotation;
1920
import java.util.function.Supplier;
2021

2122
import org.springframework.beans.factory.config.BeanDefinitionCustomizer;
@@ -152,7 +153,8 @@ public void setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver
152153
* @see #scan(String...)
153154
* @see #refresh()
154155
*/
155-
public void register(Class<?>... annotatedClasses) {
156+
@Override
157+
public final void register(Class<?>... annotatedClasses) {
156158
Assert.notEmpty(annotatedClasses, "At least one annotated class must be specified");
157159
this.reader.register(annotatedClasses);
158160
}
@@ -165,7 +167,8 @@ public void register(Class<?>... annotatedClasses) {
165167
* @see #register(Class...)
166168
* @see #refresh()
167169
*/
168-
public void scan(String... basePackages) {
170+
@Override
171+
public final void scan(String... basePackages) {
169172
Assert.notEmpty(basePackages, "At least one base package must be specified");
170173
this.scanner.scan(basePackages);
171174
}
@@ -175,6 +178,65 @@ public void scan(String... basePackages) {
175178
// Convenient methods for registering individual beans
176179
//---------------------------------------------------------------------
177180

181+
/**
182+
* Register a bean from the given bean class.
183+
* @param annotatedClass the class of the bean
184+
* @since 5.2
185+
*/
186+
public final <T> void registerBean(Class<T> annotatedClass) {
187+
this.reader.doRegisterBean(annotatedClass, null, null, null);
188+
}
189+
190+
/**
191+
* Register a bean from the given bean class, using the given supplier for
192+
* obtaining a new instance (typically declared as a lambda expression or
193+
* method reference).
194+
* @param annotatedClass the class of the bean
195+
* @param supplier a callback for creating an instance of the bean
196+
* @since 5.2
197+
*/
198+
public final <T> void registerBean(Class<T> annotatedClass, Supplier<T> supplier) {
199+
this.reader.doRegisterBean(annotatedClass, supplier, null, null);
200+
}
201+
202+
/**
203+
* Register a bean from the given bean class, deriving its metadata from
204+
* class-declared annotations.
205+
* @param annotatedClass the class of the bean
206+
* @param qualifiers specific qualifier annotations to consider,
207+
* in addition to qualifiers at the bean class level (may be empty).
208+
* These can be actual autowire qualifiers as well as {@link Primary}
209+
* and {@link Lazy}.
210+
* @since 5.2
211+
*/
212+
@Override
213+
@SafeVarargs
214+
@SuppressWarnings("varargs")
215+
public final <T> void registerBean(Class<T> annotatedClass, Class<? extends Annotation>... qualifiers) {
216+
this.reader.doRegisterBean(annotatedClass, null, null, qualifiers);
217+
}
218+
219+
/**
220+
* Register a bean from the given bean class, using the given supplier for
221+
* obtaining a new instance (typically declared as a lambda expression or
222+
* method reference).
223+
* @param annotatedClass the class of the bean
224+
* @param supplier a callback for creating an instance of the bean
225+
* @param qualifiers specific qualifier annotations to consider,
226+
* in addition to qualifiers at the bean class level (may be empty).
227+
* These can be actual autowire qualifiers as well as {@link Primary}
228+
* and {@link Lazy}.
229+
* @since 5.2
230+
*/
231+
@Override
232+
@SafeVarargs
233+
@SuppressWarnings("varargs")
234+
public final <T> void registerBean(
235+
Class<T> annotatedClass, Supplier<T> supplier, Class<? extends Annotation>... qualifiers) {
236+
237+
this.reader.doRegisterBean(annotatedClass, supplier, null, qualifiers);
238+
}
239+
178240
/**
179241
* Register a bean from the given bean class, deriving its metadata from
180242
* class-declared annotations, and optionally providing explicit constructor
@@ -187,7 +249,7 @@ public void scan(String... basePackages) {
187249
* (may be {@code null} or empty)
188250
* @since 5.0
189251
*/
190-
public <T> void registerBean(Class<T> annotatedClass, Object... constructorArguments) {
252+
public final <T> void registerBean(Class<T> annotatedClass, Object... constructorArguments) {
191253
registerBean(null, annotatedClass, constructorArguments);
192254
}
193255

@@ -203,7 +265,9 @@ public <T> void registerBean(Class<T> annotatedClass, Object... constructorArgum
203265
* (may be {@code null} or empty)
204266
* @since 5.0
205267
*/
206-
public <T> void registerBean(@Nullable String beanName, Class<T> annotatedClass, Object... constructorArguments) {
268+
public final <T> void registerBean(
269+
@Nullable String beanName, Class<T> annotatedClass, Object... constructorArguments) {
270+
207271
this.reader.doRegisterBean(annotatedClass, null, beanName, null,
208272
bd -> {
209273
for (Object arg : constructorArguments) {
@@ -213,8 +277,8 @@ public <T> void registerBean(@Nullable String beanName, Class<T> annotatedClass,
213277
}
214278

215279
@Override
216-
public <T> void registerBean(@Nullable String beanName, Class<T> beanClass, @Nullable Supplier<T> supplier,
217-
BeanDefinitionCustomizer... customizers) {
280+
public final <T> void registerBean(@Nullable String beanName, Class<T> beanClass,
281+
@Nullable Supplier<T> supplier, BeanDefinitionCustomizer... customizers) {
218282

219283
this.reader.doRegisterBean(beanClass, supplier, beanName, null, customizers);
220284
}

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

+32-1
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.
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.context.annotation;
1818

19+
import java.lang.annotation.Annotation;
20+
import java.util.function.Supplier;
21+
1922
/**
2023
* Common interface for annotation config application contexts,
2124
* defining {@link #register} and {@link #scan} methods.
@@ -40,4 +43,32 @@ public interface AnnotationConfigRegistry {
4043
*/
4144
void scan(String... basePackages);
4245

46+
/**
47+
* Register a bean from the given bean class, deriving its metadata from
48+
* class-declared annotations.
49+
* @param annotatedClass the class of the bean
50+
* @param qualifiers specific qualifier annotations to consider,
51+
* in addition to qualifiers at the bean class level (may be empty).
52+
* These can be actual autowire qualifiers as well as {@link Primary}
53+
* and {@link Lazy}.
54+
* @since 5.2
55+
*/
56+
@SuppressWarnings("unchecked")
57+
<T> void registerBean(Class<T> annotatedClass, Class<? extends Annotation>... qualifiers);
58+
59+
/**
60+
* Register a bean from the given bean class, using the given supplier for
61+
* obtaining a new instance (typically declared as a lambda expression or
62+
* method reference).
63+
* @param annotatedClass the class of the bean
64+
* @param supplier a callback for creating an instance of the bean
65+
* @param qualifiers specific qualifier annotations to consider,
66+
* in addition to qualifiers at the bean class level (may be empty).
67+
* These can be actual autowire qualifiers as well as {@link Primary}
68+
* and {@link Lazy}.
69+
* @since 5.2
70+
*/
71+
@SuppressWarnings("unchecked")
72+
<T> void registerBean(Class<T> annotatedClass, Supplier<T> supplier, Class<? extends Annotation>... qualifiers);
73+
4374
}

0 commit comments

Comments
 (0)