From f0b04d6782bb14bd3724d53cc0293f8781dffb5e Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Wed, 18 Jan 2023 09:40:16 +0100 Subject: [PATCH] Remove instance suppliers in bean definitions --- ...tiveWebServerFactoryAutoConfiguration.java | 9 +-- ...vletWebServerFactoryAutoConfiguration.java | 11 ++-- .../WebServicesAutoConfiguration.java | 1 + .../BoundConfigurationProperties.java | 3 +- .../ConfigurationPropertiesBinder.java | 58 ++++++++----------- 5 files changed, 34 insertions(+), 48 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfiguration.java index f16b57e8723c..f85deb6cdd20 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryAutoConfiguration.java @@ -16,8 +16,6 @@ package org.springframework.boot.autoconfigure.web.reactive; -import java.util.function.Supplier; - import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; @@ -102,14 +100,13 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, return; } registerSyntheticBeanIfMissing(registry, "webServerFactoryCustomizerBeanPostProcessor", - WebServerFactoryCustomizerBeanPostProcessor.class, - WebServerFactoryCustomizerBeanPostProcessor::new); + WebServerFactoryCustomizerBeanPostProcessor.class); } private void registerSyntheticBeanIfMissing(BeanDefinitionRegistry registry, String name, - Class beanClass, Supplier instanceSupplier) { + Class beanClass) { if (ObjectUtils.isEmpty(this.beanFactory.getBeanNamesForType(beanClass, true, false))) { - RootBeanDefinition beanDefinition = new RootBeanDefinition(beanClass, instanceSupplier); + RootBeanDefinition beanDefinition = new RootBeanDefinition(beanClass); beanDefinition.setSynthetic(true); registry.registerBeanDefinition(name, beanDefinition); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfiguration.java index 624383b59a75..c35c92ef6ca1 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfiguration.java @@ -16,8 +16,6 @@ package org.springframework.boot.autoconfigure.web.servlet; -import java.util.function.Supplier; - import jakarta.servlet.DispatcherType; import jakarta.servlet.ServletRequest; @@ -139,16 +137,15 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, return; } registerSyntheticBeanIfMissing(registry, "webServerFactoryCustomizerBeanPostProcessor", - WebServerFactoryCustomizerBeanPostProcessor.class, - WebServerFactoryCustomizerBeanPostProcessor::new); + WebServerFactoryCustomizerBeanPostProcessor.class); registerSyntheticBeanIfMissing(registry, "errorPageRegistrarBeanPostProcessor", - ErrorPageRegistrarBeanPostProcessor.class, ErrorPageRegistrarBeanPostProcessor::new); + ErrorPageRegistrarBeanPostProcessor.class); } private void registerSyntheticBeanIfMissing(BeanDefinitionRegistry registry, String name, - Class beanClass, Supplier instanceSupplier) { + Class beanClass) { if (ObjectUtils.isEmpty(this.beanFactory.getBeanNamesForType(beanClass, true, false))) { - RootBeanDefinition beanDefinition = new RootBeanDefinition(beanClass, instanceSupplier); + RootBeanDefinition beanDefinition = new RootBeanDefinition(beanClass); beanDefinition.setSynthetic(true); registry.registerBeanDefinition(name, beanDefinition); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfiguration.java index 69edbc4eded8..259d1686e03b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/webservices/WebServicesAutoConfiguration.java @@ -123,6 +123,7 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) private void registerBeans(String location, String pattern, Class type, Function beanSupplier, BeanDefinitionRegistry registry) { for (Resource resource : getResources(location, pattern)) { + // TODO MH: How to fix that? BeanDefinition beanDefinition = BeanDefinitionBuilder .genericBeanDefinition(type, () -> beanSupplier.apply(resource)).getBeanDefinition(); registry.registerBeanDefinition(StringUtils.stripFilenameExtension(resource.getFilename()), diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/BoundConfigurationProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/BoundConfigurationProperties.java index 075463c1bb14..f4c1da70de40 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/BoundConfigurationProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/BoundConfigurationProperties.java @@ -81,8 +81,7 @@ public static BoundConfigurationProperties get(ApplicationContext context) { static void register(BeanDefinitionRegistry registry) { Assert.notNull(registry, "Registry must not be null"); if (!registry.containsBeanDefinition(BEAN_NAME)) { - BeanDefinition definition = BeanDefinitionBuilder - .genericBeanDefinition(BoundConfigurationProperties.class, BoundConfigurationProperties::new) + BeanDefinition definition = BeanDefinitionBuilder.genericBeanDefinition(BoundConfigurationProperties.class) .getBeanDefinition(); definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); registry.registerBeanDefinition(BEAN_NAME, definition); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBinder.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBinder.java index 5fe6661c04bd..5a4da250aa39 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBinder.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBinder.java @@ -50,6 +50,7 @@ import org.springframework.core.annotation.MergedAnnotations; import org.springframework.core.convert.ConversionService; import org.springframework.core.env.PropertySources; +import org.springframework.util.Assert; import org.springframework.validation.Validator; import org.springframework.validation.annotation.Validated; @@ -64,8 +65,6 @@ class ConfigurationPropertiesBinder { private static final String BEAN_NAME = "org.springframework.boot.context.internalConfigurationPropertiesBinder"; - private static final String FACTORY_BEAN_NAME = "org.springframework.boot.context.internalConfigurationPropertiesBinderFactory"; - private static final String VALIDATOR_BEAN_NAME = EnableConfigurationProperties.VALIDATOR_BEAN_NAME; private final ApplicationContext applicationContext; @@ -189,18 +188,9 @@ private Consumer getPropertyEditorInitializer() { } static void register(BeanDefinitionRegistry registry) { - if (!registry.containsBeanDefinition(FACTORY_BEAN_NAME)) { - BeanDefinition definition = BeanDefinitionBuilder - .rootBeanDefinition(ConfigurationPropertiesBinder.Factory.class).getBeanDefinition(); - definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); - registry.registerBeanDefinition(ConfigurationPropertiesBinder.FACTORY_BEAN_NAME, definition); - } if (!registry.containsBeanDefinition(BEAN_NAME)) { BeanDefinition definition = BeanDefinitionBuilder - .rootBeanDefinition(ConfigurationPropertiesBinder.class, - () -> ((BeanFactory) registry) - .getBean(FACTORY_BEAN_NAME, ConfigurationPropertiesBinder.Factory.class).create()) - .getBeanDefinition(); + .rootBeanDefinition(ConfigurationPropertiesBinderFactory.class).getBeanDefinition(); definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); registry.registerBeanDefinition(ConfigurationPropertiesBinder.BEAN_NAME, definition); } @@ -210,27 +200,6 @@ static ConfigurationPropertiesBinder get(BeanFactory beanFactory) { return beanFactory.getBean(BEAN_NAME, ConfigurationPropertiesBinder.class); } - /** - * Factory bean used to create the {@link ConfigurationPropertiesBinder}. The bean - * needs to be {@link ApplicationContextAware} since we can't directly inject an - * {@link ApplicationContext} into the constructor without causing eager - * {@link FactoryBean} initialization. - */ - static class Factory implements ApplicationContextAware { - - private ApplicationContext applicationContext; - - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.applicationContext = applicationContext; - } - - ConfigurationPropertiesBinder create() { - return new ConfigurationPropertiesBinder(this.applicationContext); - } - - } - /** * {@link BindHandler} to deal with * {@link ConfigurationProperties @ConfigurationProperties} concerns. @@ -253,4 +222,27 @@ private boolean isConfigurationProperties(Class target) { } + static class ConfigurationPropertiesBinderFactory + implements FactoryBean, ApplicationContextAware { + + private ApplicationContext applicationContext; + + @Override + public ConfigurationPropertiesBinder getObject() throws Exception { + Assert.notNull(this.applicationContext, "applicationContext must not be null"); + return new ConfigurationPropertiesBinder(this.applicationContext); + } + + @Override + public Class getObjectType() { + return ConfigurationPropertiesBinder.class; + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + + } + }