AnnotationConfigRegistry exposes registerBean with supplier/qualifiers

Closes gh-22457
This commit is contained in:
Juergen Hoeller
2019-03-05 18:26:04 +01:00
parent eeed20d8d9
commit 18f2e6a12d
5 changed files with 297 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -197,6 +197,43 @@ public class AnnotatedBeanDefinitionReader {
doRegisterBean(annotatedClass, null, name, qualifiers);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations, using the given supplier for obtaining a new
* instance (possibly declared as a lambda expression or method reference).
* @param annotatedClass the class of the bean
* @param instanceSupplier a callback for creating an instance of the bean
* (may be {@code null})
* @param qualifiers specific qualifier annotations to consider,
* in addition to qualifiers at the bean class level
* @since 5.2
*/
@SuppressWarnings("unchecked")
public <T> void registerBean(Class<T> annotatedClass, @Nullable Supplier<T> instanceSupplier,
Class<? extends Annotation>... qualifiers) {
doRegisterBean(annotatedClass, instanceSupplier, null, qualifiers);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations, using the given supplier for obtaining a new
* instance (possibly declared as a lambda expression or method reference).
* @param annotatedClass the class of the bean
* @param name an explicit name for the bean
* @param instanceSupplier a callback for creating an instance of the bean
* (may be {@code null})
* @param qualifiers specific qualifier annotations to consider,
* in addition to qualifiers at the bean class level
* @since 5.2
*/
@SuppressWarnings("unchecked")
public <T> void registerBean(Class<T> annotatedClass, String name, @Nullable Supplier<T> instanceSupplier,
Class<? extends Annotation>... qualifiers) {
doRegisterBean(annotatedClass, instanceSupplier, name, qualifiers);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
package org.springframework.context.annotation;
import java.lang.annotation.Annotation;
import java.util.function.Supplier;
import org.springframework.beans.factory.config.BeanDefinitionCustomizer;
@@ -152,7 +153,8 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex
* @see #scan(String...)
* @see #refresh()
*/
public void register(Class<?>... annotatedClasses) {
@Override
public final void register(Class<?>... annotatedClasses) {
Assert.notEmpty(annotatedClasses, "At least one annotated class must be specified");
this.reader.register(annotatedClasses);
}
@@ -165,7 +167,8 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex
* @see #register(Class...)
* @see #refresh()
*/
public void scan(String... basePackages) {
@Override
public final void scan(String... basePackages) {
Assert.notEmpty(basePackages, "At least one base package must be specified");
this.scanner.scan(basePackages);
}
@@ -175,6 +178,65 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex
// Convenient methods for registering individual beans
//---------------------------------------------------------------------
/**
* Register a bean from the given bean class.
* @param annotatedClass the class of the bean
* @since 5.2
*/
public final <T> void registerBean(Class<T> annotatedClass) {
this.reader.doRegisterBean(annotatedClass, null, null, null);
}
/**
* Register a bean from the given bean class, using the given supplier for
* obtaining a new instance (typically declared as a lambda expression or
* method reference).
* @param annotatedClass the class of the bean
* @param supplier a callback for creating an instance of the bean
* @since 5.2
*/
public final <T> void registerBean(Class<T> annotatedClass, Supplier<T> supplier) {
this.reader.doRegisterBean(annotatedClass, supplier, null, null);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations.
* @param annotatedClass the class of the bean
* @param qualifiers specific qualifier annotations to consider,
* in addition to qualifiers at the bean class level (may be empty).
* These can be actual autowire qualifiers as well as {@link Primary}
* and {@link Lazy}.
* @since 5.2
*/
@Override
@SafeVarargs
@SuppressWarnings("varargs")
public final <T> void registerBean(Class<T> annotatedClass, Class<? extends Annotation>... qualifiers) {
this.reader.doRegisterBean(annotatedClass, null, null, qualifiers);
}
/**
* Register a bean from the given bean class, using the given supplier for
* obtaining a new instance (typically declared as a lambda expression or
* method reference).
* @param annotatedClass the class of the bean
* @param supplier a callback for creating an instance of the bean
* @param qualifiers specific qualifier annotations to consider,
* in addition to qualifiers at the bean class level (may be empty).
* These can be actual autowire qualifiers as well as {@link Primary}
* and {@link Lazy}.
* @since 5.2
*/
@Override
@SafeVarargs
@SuppressWarnings("varargs")
public final <T> void registerBean(
Class<T> annotatedClass, Supplier<T> supplier, Class<? extends Annotation>... qualifiers) {
this.reader.doRegisterBean(annotatedClass, supplier, null, qualifiers);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations, and optionally providing explicit constructor
@@ -187,7 +249,7 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex
* (may be {@code null} or empty)
* @since 5.0
*/
public <T> void registerBean(Class<T> annotatedClass, Object... constructorArguments) {
public final <T> void registerBean(Class<T> annotatedClass, Object... constructorArguments) {
registerBean(null, annotatedClass, constructorArguments);
}
@@ -203,7 +265,9 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex
* (may be {@code null} or empty)
* @since 5.0
*/
public <T> void registerBean(@Nullable String beanName, Class<T> annotatedClass, Object... constructorArguments) {
public final <T> void registerBean(
@Nullable String beanName, Class<T> annotatedClass, Object... constructorArguments) {
this.reader.doRegisterBean(annotatedClass, null, beanName, null,
bd -> {
for (Object arg : constructorArguments) {
@@ -213,8 +277,8 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex
}
@Override
public <T> void registerBean(@Nullable String beanName, Class<T> beanClass, @Nullable Supplier<T> supplier,
BeanDefinitionCustomizer... customizers) {
public final <T> void registerBean(@Nullable String beanName, Class<T> beanClass,
@Nullable Supplier<T> supplier, BeanDefinitionCustomizer... customizers) {
this.reader.doRegisterBean(beanClass, supplier, beanName, null, customizers);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,9 @@
package org.springframework.context.annotation;
import java.lang.annotation.Annotation;
import java.util.function.Supplier;
/**
* Common interface for annotation config application contexts,
* defining {@link #register} and {@link #scan} methods.
@@ -40,4 +43,32 @@ public interface AnnotationConfigRegistry {
*/
void scan(String... basePackages);
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations.
* @param annotatedClass the class of the bean
* @param qualifiers specific qualifier annotations to consider,
* in addition to qualifiers at the bean class level (may be empty).
* These can be actual autowire qualifiers as well as {@link Primary}
* and {@link Lazy}.
* @since 5.2
*/
@SuppressWarnings("unchecked")
<T> void registerBean(Class<T> annotatedClass, Class<? extends Annotation>... qualifiers);
/**
* Register a bean from the given bean class, using the given supplier for
* obtaining a new instance (typically declared as a lambda expression or
* method reference).
* @param annotatedClass the class of the bean
* @param supplier a callback for creating an instance of the bean
* @param qualifiers specific qualifier annotations to consider,
* in addition to qualifiers at the bean class level (may be empty).
* These can be actual autowire qualifiers as well as {@link Primary}
* and {@link Lazy}.
* @since 5.2
*/
@SuppressWarnings("unchecked")
<T> void registerBean(Class<T> annotatedClass, Supplier<T> supplier, Class<? extends Annotation>... qualifiers);
}