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-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,9 +16,11 @@
package org.springframework.web.context.support;
import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Supplier;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@@ -94,6 +96,8 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe
private final Set<String> basePackages = new LinkedHashSet<>();
private final Set<BeanRegistration> registeredBeans = new LinkedHashSet<>();
/**
* Set a custom {@link BeanNameGenerator} for use with {@link AnnotatedBeanDefinitionReader}
@@ -140,14 +144,14 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe
* Register one or more annotated classes to be processed.
* <p>Note that {@link #refresh()} must be called in order for the context
* to fully process the new classes.
* @param annotatedClasses one or more annotated classes,
* e.g. {@link org.springframework.context.annotation.Configuration @Configuration} classes
* @param annotatedClasses one or more annotated classes, e.g.
* {@link org.springframework.context.annotation.Configuration @Configuration} classes
* @see #scan(String...)
* @see #loadBeanDefinitions(DefaultListableBeanFactory)
* @see #setConfigLocation(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");
Collections.addAll(this.annotatedClasses, annotatedClasses);
}
@@ -157,16 +161,58 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe
* <p>Note that {@link #refresh()} must be called in order for the context
* to fully process the new classes.
* @param basePackages the packages to check for annotated classes
* @see #loadBeanDefinitions(DefaultListableBeanFactory)
* @see #register(Class...)
* @see #setConfigLocation(String)
* @see #loadBeanDefinitions(DefaultListableBeanFactory)
* @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");
Collections.addAll(this.basePackages, basePackages);
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations.
* <p>Note that {@link #refresh()} must be called in order for the context
* to fully process the new classes.
* @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)
* @since 5.2
* @see #register(Class...)
* @see #loadBeanDefinitions(DefaultListableBeanFactory)
* @see #refresh()
*/
@Override
@SafeVarargs
@SuppressWarnings("varargs")
public final <T> void registerBean(Class<T> annotatedClass, Class<? extends Annotation>... qualifiers) {
this.registeredBeans.add(new BeanRegistration(annotatedClass, null, qualifiers));
}
/**
* Register a bean from the given bean class, deriving its metadata from
* class-declared annotations.
* <p>Note that {@link #refresh()} must be called in order for the context
* to fully process the new classes.
* @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)
* @since 5.2
* @see #register(Class...)
* @see #loadBeanDefinitions(DefaultListableBeanFactory)
* @see #refresh()
*/
@Override
@SafeVarargs
@SuppressWarnings("varargs")
public final <T> void registerBean(
Class<T> annotatedClass, Supplier<T> supplier, Class<? extends Annotation>... qualifiers) {
this.registeredBeans.add(new BeanRegistration(annotatedClass, supplier, qualifiers));
}
/**
* Register a {@link org.springframework.beans.factory.config.BeanDefinition} for
@@ -191,6 +237,7 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe
* @see ClassPathBeanDefinitionScanner
*/
@Override
@SuppressWarnings("unchecked")
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) {
AnnotatedBeanDefinitionReader reader = getAnnotatedBeanDefinitionReader(beanFactory);
ClassPathBeanDefinitionScanner scanner = getClassPathBeanDefinitionScanner(beanFactory);
@@ -224,6 +271,15 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe
scanner.scan(StringUtils.toStringArray(this.basePackages));
}
if (!this.registeredBeans.isEmpty()) {
if (logger.isDebugEnabled()) {
logger.debug("Registering supplied beans: [" +
StringUtils.collectionToCommaDelimitedString(this.registeredBeans) + "]");
}
this.registeredBeans.forEach(reg ->
reader.registerBean(reg.getAnnotatedClass(), reg.getSupplier(), reg.getQualifiers()));
}
String[] configLocations = getConfigLocations();
if (configLocations != null) {
for (String configLocation : configLocations) {
@@ -277,4 +333,46 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe
return new ClassPathBeanDefinitionScanner(beanFactory, true, getEnvironment());
}
/**
* Holder for a programmatic bean registration.
* @see #registerBean(Class, Class[])
* @see #registerBean(Class, Supplier, Class[])
*/
private static class BeanRegistration {
private final Class<?> annotatedClass;
@Nullable
private final Supplier<?> supplier;
private final Class<? extends Annotation>[] qualifiers;
public BeanRegistration(
Class<?> annotatedClass, @Nullable Supplier<?> supplier, Class<? extends Annotation>[] qualifiers) {
this.annotatedClass = annotatedClass;
this.supplier = supplier;
this.qualifiers = qualifiers;
}
public Class<?> getAnnotatedClass() {
return this.annotatedClass;
}
@Nullable
@SuppressWarnings("rawtypes")
public Supplier getSupplier() {
return this.supplier;
}
public Class<? extends Annotation>[] getQualifiers() {
return this.qualifiers;
}
@Override
public String toString() {
return this.annotatedClass.getName();
}
}
}