Polish ConfigurationPropertiesBean

Closes gh-35640
pull/35611/head
Phillip Webb 2 years ago committed by Andy Wilkinson
parent bfbae581d7
commit 581a32b107

@ -65,17 +65,13 @@ public final class ConfigurationPropertiesBean {
private final Object instance; private final Object instance;
private final ConfigurationProperties annotation;
private final Bindable<?> bindTarget; private final Bindable<?> bindTarget;
private final BindMethod bindMethod; private final BindMethod bindMethod;
private ConfigurationPropertiesBean(String name, Object instance, ConfigurationProperties annotation, private ConfigurationPropertiesBean(String name, Object instance, Bindable<?> bindTarget, BindMethod bindMethod) {
Bindable<?> bindTarget, BindMethod bindMethod) {
this.name = name; this.name = name;
this.instance = instance; this.instance = instance;
this.annotation = annotation;
this.bindTarget = bindTarget; this.bindTarget = bindTarget;
this.bindMethod = (bindMethod != null) ? bindMethod : BindMethod.get(bindTarget); this.bindMethod = (bindMethod != null) ? bindMethod : BindMethod.get(bindTarget);
} }
@ -119,7 +115,7 @@ public final class ConfigurationPropertiesBean {
* @return the configuration properties annotation * @return the configuration properties annotation
*/ */
public ConfigurationProperties getAnnotation() { public ConfigurationProperties getAnnotation() {
return this.annotation; return this.bindTarget.getAnnotation(ConfigurationProperties.class);
} }
/** /**
@ -145,10 +141,10 @@ public final class ConfigurationPropertiesBean {
return getAll(configurableContext); return getAll(configurableContext);
} }
Map<String, ConfigurationPropertiesBean> propertiesBeans = new LinkedHashMap<>(); Map<String, ConfigurationPropertiesBean> propertiesBeans = new LinkedHashMap<>();
applicationContext.getBeansWithAnnotation(ConfigurationProperties.class).forEach((beanName, bean) -> { applicationContext.getBeansWithAnnotation(ConfigurationProperties.class).forEach((name, instance) -> {
ConfigurationPropertiesBean propertiesBean = get(applicationContext, bean, beanName); ConfigurationPropertiesBean propertiesBean = get(applicationContext, instance, name);
if (propertiesBean != null) { if (propertiesBean != null) {
propertiesBeans.put(beanName, propertiesBean); propertiesBeans.put(name, propertiesBean);
} }
}); });
return propertiesBeans; return propertiesBeans;
@ -163,8 +159,7 @@ public final class ConfigurationPropertiesBean {
if (isConfigurationPropertiesBean(beanFactory, beanName)) { if (isConfigurationPropertiesBean(beanFactory, beanName)) {
try { try {
Object bean = beanFactory.getBean(beanName); Object bean = beanFactory.getBean(beanName);
BindMethod bindMethod = BindMethodAttribute.get(beanFactory, beanName); ConfigurationPropertiesBean propertiesBean = get(applicationContext, bean, beanName);
ConfigurationPropertiesBean propertiesBean = get(applicationContext, bean, beanName, bindMethod);
if (propertiesBean != null) { if (propertiesBean != null) {
propertiesBeans.put(beanName, propertiesBean); propertiesBeans.put(beanName, propertiesBean);
} }
@ -206,13 +201,19 @@ public final class ConfigurationPropertiesBean {
* {@link ConfigurationProperties @ConfigurationProperties} * {@link ConfigurationProperties @ConfigurationProperties}
*/ */
public static ConfigurationPropertiesBean get(ApplicationContext applicationContext, Object bean, String beanName) { public static ConfigurationPropertiesBean get(ApplicationContext applicationContext, Object bean, String beanName) {
return get(applicationContext, bean, beanName, null);
}
private static ConfigurationPropertiesBean get(ApplicationContext applicationContext, Object bean, String beanName,
BindMethod bindMethod) {
Method factoryMethod = findFactoryMethod(applicationContext, beanName); Method factoryMethod = findFactoryMethod(applicationContext, beanName);
return create(beanName, bean, bean.getClass(), factoryMethod, bindMethod); Bindable<Object> bindTarget = createBindTarget(bean, bean.getClass(), factoryMethod);
if (bindTarget == null) {
return null;
}
BindMethod bindMethod = BindMethodAttribute.get(applicationContext, beanName);
if (bindMethod == null && factoryMethod != null) {
bindMethod = BindMethod.JAVA_BEAN;
}
if (bindTarget != null && bindMethod != BindMethod.VALUE_OBJECT) {
bindTarget = bindTarget.withExistingValue(bean);
}
return create(beanName, bean, bindTarget, bindMethod);
} }
private static Method findFactoryMethod(ApplicationContext applicationContext, String beanName) { private static Method findFactoryMethod(ApplicationContext applicationContext, String beanName) {
@ -260,30 +261,28 @@ public final class ConfigurationPropertiesBean {
return factoryMethod.get(); return factoryMethod.get();
} }
static ConfigurationPropertiesBean forValueObject(Class<?> beanClass, String beanName) { static ConfigurationPropertiesBean forValueObject(Class<?> beanType, String beanName) {
ConfigurationPropertiesBean propertiesBean = create(beanName, null, beanClass, null, null); Bindable<Object> bindTarget = createBindTarget(null, beanType, null);
ConfigurationPropertiesBean propertiesBean = create(beanName, null, bindTarget, null);
Assert.state(propertiesBean != null && propertiesBean.getBindMethod() == BindMethod.VALUE_OBJECT, Assert.state(propertiesBean != null && propertiesBean.getBindMethod() == BindMethod.VALUE_OBJECT,
() -> "Bean '" + beanName + "' is not a @ConfigurationProperties value object"); () -> "Bean '" + beanName + "' is not a @ConfigurationProperties value object");
return propertiesBean; return propertiesBean;
} }
private static ConfigurationPropertiesBean create(String name, Object instance, Class<?> type, Method factory, private static Bindable<Object> createBindTarget(Object bean, Class<?> beanType, Method factoryMethod) {
BindMethod bindMethod) { ResolvableType type = (factoryMethod != null) ? ResolvableType.forMethodReturnType(factoryMethod)
: ResolvableType.forClass(beanType);
Annotation[] annotations = findAnnotations(bean, beanType, factoryMethod);
return (annotations != null) ? Bindable.of(type).withAnnotations(annotations) : null;
}
private static Annotation[] findAnnotations(Object instance, Class<?> type, Method factory) {
ConfigurationProperties annotation = findAnnotation(instance, type, factory, ConfigurationProperties.class); ConfigurationProperties annotation = findAnnotation(instance, type, factory, ConfigurationProperties.class);
if (annotation == null) { if (annotation == null) {
return null; return null;
} }
Validated validated = findAnnotation(instance, type, factory, Validated.class); Validated validated = findAnnotation(instance, type, factory, Validated.class);
Annotation[] annotations = (validated != null) ? new Annotation[] { annotation, validated } return (validated != null) ? new Annotation[] { annotation, validated } : new Annotation[] { annotation };
: new Annotation[] { annotation };
ResolvableType bindType = (factory != null) ? ResolvableType.forMethodReturnType(factory)
: ResolvableType.forClass(type);
Bindable<Object> bindable = Bindable.of(bindType).withAnnotations(annotations);
bindMethod = (factory != null) ? BindMethod.JAVA_BEAN : bindMethod;
if (instance != null && bindMethod != BindMethod.VALUE_OBJECT) {
bindable = bindable.withExistingValue(instance);
}
return new ConfigurationPropertiesBean(name, instance, annotation, bindable, bindMethod);
} }
private static <A extends Annotation> A findAnnotation(Object instance, Class<?> type, Method factory, private static <A extends Annotation> A findAnnotation(Object instance, Class<?> type, Method factory,
@ -308,6 +307,11 @@ public final class ConfigurationPropertiesBean {
: MergedAnnotation.missing(); : MergedAnnotation.missing();
} }
private static ConfigurationPropertiesBean create(String name, Object instance, Bindable<Object> bindTarget,
BindMethod bindMethod) {
return (bindTarget != null) ? new ConfigurationPropertiesBean(name, instance, bindTarget, bindMethod) : null;
}
/** /**
* The binding method that is used for the bean. * The binding method that is used for the bean.
*/ */

Loading…
Cancel
Save