From e0465f73577f082d7f599b2b855dc5d59d626dfc Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 21 Sep 2021 16:36:45 +0200 Subject: [PATCH] Use RootBeanDefinition consistently Closes gh-28077 --- .../ConditionalOnMissingBeanTests.java | 6 ++--- ...ertyPlaceholderAutoConfigurationTests.java | 6 ++--- .../ConfigurationPropertiesBinder.java | 17 ++++++-------- ...urationPropertiesBindingPostProcessor.java | 4 +--- ...aseInitializationDependencyConfigurer.java | 2 +- ...itializationDependencyConfigurerTests.java | 23 ++++++++----------- 6 files changed, 25 insertions(+), 33 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBeanTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBeanTests.java index c5a01cc90a..f7087dd233 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBeanTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBeanTests.java @@ -449,7 +449,7 @@ class ConditionalOnMissingBeanTests { @Override public void registerBeanDefinitions(AnnotationMetadata meta, BeanDefinitionRegistry registry) { - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(NonspecificFactoryBean.class); + BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(NonspecificFactoryBean.class); builder.addConstructorArgValue("foo"); builder.getBeanDefinition().setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, ExampleBean.class); registry.registerBeanDefinition("exampleBeanFactoryBean", builder.getBeanDefinition()); @@ -467,7 +467,7 @@ class ConditionalOnMissingBeanTests { @Override public void registerBeanDefinitions(AnnotationMetadata meta, BeanDefinitionRegistry registry) { - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(NonspecificFactoryBean.class); + BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(NonspecificFactoryBean.class); builder.addConstructorArgValue("foo"); builder.getBeanDefinition().setAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE, ExampleBean.class.getName()); registry.registerBeanDefinition("exampleBeanFactoryBean", builder.getBeanDefinition()); @@ -485,7 +485,7 @@ class ConditionalOnMissingBeanTests { @Override public void registerBeanDefinitions(AnnotationMetadata meta, BeanDefinitionRegistry registry) { - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ExampleFactoryBean.class); + BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ExampleFactoryBean.class); builder.addConstructorArgValue("foo"); registry.registerBeanDefinition("exampleBeanFactoryBean", builder.getBeanDefinition()); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/PropertyPlaceholderAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/PropertyPlaceholderAutoConfigurationTests.java index c0041e0d90..139a14fd3e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/PropertyPlaceholderAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/PropertyPlaceholderAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 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. @@ -86,8 +86,8 @@ class PropertyPlaceholderAutoConfigurationTests { private void definePlaceholderBean(ConfigurableApplicationContext context) { ((BeanDefinitionRegistry) context.getBeanFactory()).registerBeanDefinition("placeholderBean", - BeanDefinitionBuilder.genericBeanDefinition(PlaceholderBean.class) - .addConstructorArgValue("${fruit:apple}").getBeanDefinition()); + BeanDefinitionBuilder.rootBeanDefinition(PlaceholderBean.class).addConstructorArgValue("${fruit:apple}") + .getBeanDefinition()); } @Configuration(proxyBeanMethods = false) 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 4adac0a168..597b67bc5c 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 @@ -26,7 +26,6 @@ import org.springframework.beans.PropertyEditorRegistry; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.boot.context.properties.bind.AbstractBindHandler; @@ -113,7 +112,7 @@ class ConfigurationPropertiesBinder { private BindHandler getBindHandler(Bindable target, ConfigurationProperties annotation) { List validators = getValidators(target); BindHandler handler = getHandler(); - handler = new ConfigurationPropertiesBindHander(handler); + handler = new ConfigurationPropertiesBindHandler(handler); if (annotation.ignoreInvalidFields()) { handler = new IgnoreErrorsBindHandler(handler); } @@ -193,16 +192,14 @@ class ConfigurationPropertiesBinder { static void register(BeanDefinitionRegistry registry) { if (!registry.containsBeanDefinition(FACTORY_BEAN_NAME)) { - AbstractBeanDefinition definition = BeanDefinitionBuilder - .genericBeanDefinition(ConfigurationPropertiesBinder.Factory.class, - ConfigurationPropertiesBinder.Factory::new) - .getBeanDefinition(); + 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)) { - AbstractBeanDefinition definition = BeanDefinitionBuilder - .genericBeanDefinition(ConfigurationPropertiesBinder.class, + BeanDefinition definition = BeanDefinitionBuilder + .rootBeanDefinition(ConfigurationPropertiesBinder.class, () -> ((BeanFactory) registry) .getBean(FACTORY_BEAN_NAME, ConfigurationPropertiesBinder.Factory.class).create()) .getBeanDefinition(); @@ -240,9 +237,9 @@ class ConfigurationPropertiesBinder { * {@link BindHandler} to deal with * {@link ConfigurationProperties @ConfigurationProperties} concerns. */ - private static class ConfigurationPropertiesBindHander extends AbstractBindHandler { + private static class ConfigurationPropertiesBindHandler extends AbstractBindHandler { - ConfigurationPropertiesBindHander(BindHandler handler) { + ConfigurationPropertiesBindHandler(BindHandler handler) { super(handler); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java index a5f57c6086..f6d53b9224 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindingPostProcessor.java @@ -108,9 +108,7 @@ public class ConfigurationPropertiesBindingPostProcessor Assert.notNull(registry, "Registry must not be null"); if (!registry.containsBeanDefinition(BEAN_NAME)) { BeanDefinition definition = BeanDefinitionBuilder - .rootBeanDefinition(ConfigurationPropertiesBindingPostProcessor.class, - ConfigurationPropertiesBindingPostProcessor::new) - .getBeanDefinition(); + .rootBeanDefinition(ConfigurationPropertiesBindingPostProcessor.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/sql/init/dependency/DatabaseInitializationDependencyConfigurer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/sql/init/dependency/DatabaseInitializationDependencyConfigurer.java index a542f5b1ab..6b1b02cfaf 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/sql/init/dependency/DatabaseInitializationDependencyConfigurer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/sql/init/dependency/DatabaseInitializationDependencyConfigurer.java @@ -73,7 +73,7 @@ public class DatabaseInitializationDependencyConfigurer implements ImportBeanDef public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { String name = DependsOnDatabaseInitializationPostProcessor.class.getName(); if (!registry.containsBeanDefinition(name)) { - BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition( + BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition( DependsOnDatabaseInitializationPostProcessor.class, this::createDependsOnDatabaseInitializationPostProcessor); registry.registerBeanDefinition(name, builder.getBeanDefinition()); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/sql/init/dependency/DatabaseInitializationDependencyConfigurerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/sql/init/dependency/DatabaseInitializationDependencyConfigurerTests.java index 55427a21db..f4d40ffe19 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/sql/init/dependency/DatabaseInitializationDependencyConfigurerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/sql/init/dependency/DatabaseInitializationDependencyConfigurerTests.java @@ -81,10 +81,8 @@ class DatabaseInitializationDependencyConfigurerTests { void beanFactoryPostProcessorHasOrderAllowingSubsequentPostProcessorsToFineTuneDependencies() { performDetection(Arrays.asList(MockDatabaseInitializerDetector.class, MockedDependsOnDatabaseInitializationDetector.class), (context) -> { - BeanDefinition alpha = BeanDefinitionBuilder.genericBeanDefinition(String.class) - .getBeanDefinition(); - BeanDefinition bravo = BeanDefinitionBuilder.genericBeanDefinition(String.class) - .getBeanDefinition(); + BeanDefinition alpha = BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition(); + BeanDefinition bravo = BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition(); context.register(DependsOnCaptor.class); context.register(DependencyConfigurerConfiguration.class); context.registerBeanDefinition("alpha", alpha); @@ -104,8 +102,7 @@ class DatabaseInitializationDependencyConfigurerTests { void whenDetectorsAreCreatedThenTheEnvironmentCanBeInjected() { performDetection(Arrays.asList(ConstructorInjectionDatabaseInitializerDetector.class, ConstructorInjectionDependsOnDatabaseInitializationDetector.class), (context) -> { - BeanDefinition alpha = BeanDefinitionBuilder.genericBeanDefinition(String.class) - .getBeanDefinition(); + BeanDefinition alpha = BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition(); context.registerBeanDefinition("alpha", alpha); context.register(DependencyConfigurerConfiguration.class); context.refresh(); @@ -117,8 +114,8 @@ class DatabaseInitializationDependencyConfigurerTests { @Test void whenDependenciesAreConfiguredThenBeansThatDependUponDatabaseInitializationDependUponDetectedDatabaseInitializers() { - BeanDefinition alpha = BeanDefinitionBuilder.genericBeanDefinition(String.class).getBeanDefinition(); - BeanDefinition bravo = BeanDefinitionBuilder.genericBeanDefinition(String.class).getBeanDefinition(); + BeanDefinition alpha = BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition(); + BeanDefinition bravo = BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition(); performDetection(Arrays.asList(MockDatabaseInitializerDetector.class, MockedDependsOnDatabaseInitializationDetector.class), (context) -> { context.registerBeanDefinition("alpha", alpha); @@ -140,11 +137,11 @@ class DatabaseInitializationDependencyConfigurerTests { @Test void whenDependenciesAreConfiguredDetectedDatabaseInitializersAreInitializedInCorrectOrder() { - BeanDefinition alpha = BeanDefinitionBuilder.genericBeanDefinition(String.class).getBeanDefinition(); - BeanDefinition bravo1 = BeanDefinitionBuilder.genericBeanDefinition(String.class).getBeanDefinition(); - BeanDefinition bravo2 = BeanDefinitionBuilder.genericBeanDefinition(String.class).getBeanDefinition(); - BeanDefinition charlie = BeanDefinitionBuilder.genericBeanDefinition(String.class).getBeanDefinition(); - BeanDefinition delta = BeanDefinitionBuilder.genericBeanDefinition(String.class).getBeanDefinition(); + BeanDefinition alpha = BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition(); + BeanDefinition bravo1 = BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition(); + BeanDefinition bravo2 = BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition(); + BeanDefinition charlie = BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition(); + BeanDefinition delta = BeanDefinitionBuilder.rootBeanDefinition(String.class).getBeanDefinition(); performDetection( Arrays.asList(MockDatabaseInitializerDetector.class, OrderedLowestMockDatabaseInitializerDetector.class, OrderedNearLowestMockDatabaseInitializerDetector.class,