Merge branch '3.0.x'

Closes gh-35639
pull/35655/head
Andy Wilkinson 2 years ago
commit fa49e2b6c6

@ -0,0 +1,61 @@
/*
* Copyright 2012-2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.context.properties;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.context.properties.ConfigurationPropertiesBean.BindMethod;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.AttributeAccessor;
/**
* Allows a {@link BindMethod} value to be stored and retrieved from an
* {@link AttributeAccessor}.
*
* @author Phillip Webb
*/
final class BindMethodAttribute {
static final String NAME = BindMethod.class.getName();
private BindMethodAttribute() {
}
static BindMethod get(ApplicationContext applicationContext, String beanName) {
return (applicationContext instanceof ConfigurableApplicationContext configurableApplicationContext)
? get(configurableApplicationContext.getBeanFactory(), beanName) : null;
}
static BindMethod get(ConfigurableListableBeanFactory beanFactory, String beanName) {
return (!beanFactory.containsBeanDefinition(beanName)) ? null : get(beanFactory.getBeanDefinition(beanName));
}
static BindMethod get(BeanDefinitionRegistry beanDefinitionRegistry, String beanName) {
return (!beanDefinitionRegistry.containsBeanDefinition(beanName)) ? null
: get(beanDefinitionRegistry.getBeanDefinition(beanName));
}
static BindMethod get(AttributeAccessor attributes) {
return (BindMethod) attributes.getAttribute(NAME);
}
static void set(AttributeAccessor attributes, BindMethod bindMethod) {
attributes.setAttribute(NAME, bindMethod);
}
}

@ -163,7 +163,7 @@ public final class ConfigurationPropertiesBean {
if (isConfigurationPropertiesBean(beanFactory, beanName)) {
try {
Object bean = beanFactory.getBean(beanName);
BindMethod bindMethod = getBindMethod(beanFactory, beanName);
BindMethod bindMethod = BindMethodAttribute.get(beanFactory, beanName);
ConfigurationPropertiesBean propertiesBean = get(applicationContext, bean, beanName, bindMethod);
if (propertiesBean != null) {
propertiesBeans.put(beanName, propertiesBean);
@ -176,16 +176,6 @@ public final class ConfigurationPropertiesBean {
return propertiesBeans;
}
private static BindMethod getBindMethod(ConfigurableListableBeanFactory beanFactory, String beanName) {
try {
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
return (BindMethod) beanDefinition.getAttribute(BindMethod.class.getName());
}
catch (NoSuchBeanDefinitionException ex) {
return null;
}
}
private static boolean isConfigurationPropertiesBean(ConfigurableListableBeanFactory beanFactory, String beanName) {
try {
if (beanFactory.getBeanDefinition(beanName).isAbstract()) {

@ -92,7 +92,7 @@ final class ConfigurationPropertiesBeanRegistrar {
private BeanDefinition createBeanDefinition(String beanName, Class<?> type) {
BindMethod bindMethod = BindMethod.get(type);
RootBeanDefinition definition = new RootBeanDefinition(type);
definition.setAttribute(BindMethod.class.getName(), bindMethod);
BindMethodAttribute.set(definition, bindMethod);
if (bindMethod == BindMethod.VALUE_OBJECT) {
definition.setInstanceSupplier(() -> ConstructorBound.from(this.beanFactory, beanName, type));
}

@ -56,16 +56,12 @@ class ConfigurationPropertiesBeanRegistrationAotProcessor implements BeanRegistr
}
private boolean isImmutableConfigurationPropertiesBeanDefinition(BeanDefinition beanDefinition) {
return beanDefinition.hasAttribute(BindMethod.class.getName())
&& BindMethod.VALUE_OBJECT.equals(beanDefinition.getAttribute(BindMethod.class.getName()));
return BindMethod.VALUE_OBJECT.equals(BindMethodAttribute.get(beanDefinition));
}
private static class ConfigurationPropertiesBeanRegistrationCodeFragments
extends BeanRegistrationCodeFragmentsDecorator {
private static final Predicate<String> INCLUDE_BIND_METHOD_ATTRIBUTE_FILTER = (name) -> name
.equals(BindMethod.class.getName());
private static final String REGISTERED_BEAN_PARAMETER_NAME = "registeredBean";
private final RegisteredBean registeredBean;
@ -81,7 +77,7 @@ class ConfigurationPropertiesBeanRegistrationAotProcessor implements BeanRegistr
BeanRegistrationCode beanRegistrationCode, RootBeanDefinition beanDefinition,
Predicate<String> attributeFilter) {
return super.generateSetBeanDefinitionPropertiesCode(generationContext, beanRegistrationCode,
beanDefinition, INCLUDE_BIND_METHOD_ATTRIBUTE_FILTER.or(attributeFilter));
beanDefinition, attributeFilter.or(BindMethodAttribute.NAME::equals));
}
@Override

@ -82,8 +82,7 @@ public class ConfigurationPropertiesBindingPostProcessor
}
private boolean hasBoundValueObject(String beanName) {
return this.registry.containsBeanDefinition(beanName) && BindMethod.VALUE_OBJECT
.equals(this.registry.getBeanDefinition(beanName).getAttribute(BindMethod.class.getName()));
return BindMethod.VALUE_OBJECT.equals(BindMethodAttribute.get(this.registry, beanName));
}
private void bind(ConfigurationPropertiesBean bean) {

Loading…
Cancel
Save