Polish BeanDefinitionRegistrar
This commit is contained in:
parent
8c5a407a7d
commit
42d114534b
|
@ -40,7 +40,9 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
import org.springframework.core.MethodIntrospector;
|
import org.springframework.core.MethodIntrospector;
|
||||||
import org.springframework.core.ResolvableType;
|
import org.springframework.core.ResolvableType;
|
||||||
|
import org.springframework.core.log.LogMessage;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
|
@ -66,8 +68,6 @@ public final class BeanDefinitionRegistrar {
|
||||||
@Nullable
|
@Nullable
|
||||||
private final ResolvableType beanType;
|
private final ResolvableType beanType;
|
||||||
|
|
||||||
private final BeanDefinitionBuilder builder;
|
|
||||||
|
|
||||||
private final List<Consumer<RootBeanDefinition>> customizers;
|
private final List<Consumer<RootBeanDefinition>> customizers;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
|
@ -81,11 +81,9 @@ public final class BeanDefinitionRegistrar {
|
||||||
this.beanName = beanName;
|
this.beanName = beanName;
|
||||||
this.beanClass = beanClass;
|
this.beanClass = beanClass;
|
||||||
this.beanType = beanType;
|
this.beanType = beanType;
|
||||||
this.builder = BeanDefinitionBuilder.rootBeanDefinition(beanClass);
|
|
||||||
this.customizers = new ArrayList<>();
|
this.customizers = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize the registration of a bean with the specified name and type.
|
* Initialize the registration of a bean with the specified name and type.
|
||||||
* @param beanName the name of the bean
|
* @param beanName the name of the bean
|
||||||
|
@ -124,16 +122,6 @@ public final class BeanDefinitionRegistrar {
|
||||||
return new BeanDefinitionRegistrar(null, beanType, null);
|
return new BeanDefinitionRegistrar(null, beanType, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Customize the {@link RootBeanDefinition} using the specified consumer.
|
|
||||||
* @param bd a consumer for the bean definition
|
|
||||||
* @return {@code this}, to facilitate method chaining
|
|
||||||
*/
|
|
||||||
public BeanDefinitionRegistrar customize(ThrowableConsumer<RootBeanDefinition> bd) {
|
|
||||||
this.customizers.add(bd);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Specify the factory method to use to instantiate the bean.
|
* Specify the factory method to use to instantiate the bean.
|
||||||
* @param declaredType the {@link Method#getDeclaringClass() declared type}
|
* @param declaredType the {@link Method#getDeclaringClass() declared type}
|
||||||
|
@ -177,19 +165,25 @@ public final class BeanDefinitionRegistrar {
|
||||||
return customize(beanDefinition -> beanDefinition.setInstanceSupplier(instanceSupplier));
|
return customize(beanDefinition -> beanDefinition.setInstanceSupplier(instanceSupplier));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Customize the {@link RootBeanDefinition} using the specified consumer.
|
||||||
|
* @param bd a consumer for the bean definition
|
||||||
|
* @return {@code this}, to facilitate method chaining
|
||||||
|
*/
|
||||||
|
public BeanDefinitionRegistrar customize(ThrowableConsumer<RootBeanDefinition> bd) {
|
||||||
|
this.customizers.add(bd);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the {@link RootBeanDefinition} defined by this instance to
|
* Register the {@link RootBeanDefinition} defined by this instance to
|
||||||
* the specified bean factory.
|
* the specified bean factory.
|
||||||
* @param beanFactory the bean factory to use
|
* @param beanFactory the bean factory to use
|
||||||
*/
|
*/
|
||||||
public void register(DefaultListableBeanFactory beanFactory) {
|
public void register(DefaultListableBeanFactory beanFactory) {
|
||||||
if (logger.isDebugEnabled()) {
|
|
||||||
logger.debug("Register bean definition with name '" + this.beanName + "'");
|
|
||||||
}
|
|
||||||
BeanDefinition beanDefinition = toBeanDefinition();
|
BeanDefinition beanDefinition = toBeanDefinition();
|
||||||
if (this.beanName == null) {
|
Assert.state(this.beanName != null, () -> "Bean name not set. Could not register " + beanDefinition);
|
||||||
throw new IllegalStateException("Bean name not set. Could not register " + beanDefinition);
|
logger.debug(LogMessage.format("Register bean definition with name '%s'", this.beanName));
|
||||||
}
|
|
||||||
beanFactory.registerBeanDefinition(this.beanName, beanDefinition);
|
beanFactory.registerBeanDefinition(this.beanName, beanDefinition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,7 +202,8 @@ public final class BeanDefinitionRegistrar {
|
||||||
}
|
}
|
||||||
|
|
||||||
private RootBeanDefinition createBeanDefinition() {
|
private RootBeanDefinition createBeanDefinition() {
|
||||||
RootBeanDefinition bd = (RootBeanDefinition) this.builder.getBeanDefinition();
|
RootBeanDefinition bd = (RootBeanDefinition) BeanDefinitionBuilder
|
||||||
|
.rootBeanDefinition(this.beanClass).getBeanDefinition();
|
||||||
if (this.beanType != null) {
|
if (this.beanType != null) {
|
||||||
bd.setTargetType(this.beanType);
|
bd.setTargetType(this.beanType);
|
||||||
}
|
}
|
||||||
|
@ -259,6 +254,7 @@ public final class BeanDefinitionRegistrar {
|
||||||
return Arrays.stream(parameterTypes).map(Class::getName).collect(Collectors.joining(", "));
|
return Arrays.stream(parameterTypes).map(Class::getName).collect(Collectors.joining(", "));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Callback interface used by instance suppliers that need to resolve
|
* Callback interface used by instance suppliers that need to resolve
|
||||||
* dependencies for the {@link Executable} used to create the instance
|
* dependencies for the {@link Executable} used to create the instance
|
||||||
|
@ -276,9 +272,9 @@ public final class BeanDefinitionRegistrar {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a bean instance using the specified {@code factory}.
|
* Return the bean instance using the {@code factory}.
|
||||||
* @param beanFactory the bean factory to use
|
* @param beanFactory the bean factory to use
|
||||||
* @param factory a function that returns a bean instance based on
|
* @param factory a function that returns the bean instance based on
|
||||||
* the resolved attributes required by its instance creator
|
* the resolved attributes required by its instance creator
|
||||||
* @param <T> the type of the bean
|
* @param <T> the type of the bean
|
||||||
* @return the bean instance
|
* @return the bean instance
|
||||||
|
@ -321,9 +317,7 @@ public final class BeanDefinitionRegistrar {
|
||||||
|
|
||||||
private Field getField(String fieldName, Class<?> fieldType) {
|
private Field getField(String fieldName, Class<?> fieldType) {
|
||||||
Field field = ReflectionUtils.findField(this.beanType, fieldName, fieldType);
|
Field field = ReflectionUtils.findField(this.beanType, fieldName, fieldType);
|
||||||
if (field == null) {
|
Assert.notNull(field, () -> "No field '" + fieldName + "' with type " + fieldType.getName() + " found on " + this.beanType);
|
||||||
throw new IllegalArgumentException("No field '" + fieldName + "' with type " + fieldType.getName() + " found on " + this.beanType);
|
|
||||||
}
|
|
||||||
return field;
|
return field;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue