Polish "Use RootBeanDefinition if possible"

This commit adapts code that was using GenericBeanDefinition to use
RootBeanDefinition instead. Spring Framework recommend to use
RootBeanDefinition if it's pre-determined as root bean.

See gh-42611
This commit is contained in:
Stéphane Nicoll 2024-10-15 09:40:02 +02:00
parent 7427304b3d
commit 0c0acb4339
8 changed files with 18 additions and 20 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
@ -28,7 +28,7 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
@ -159,7 +159,7 @@ public class EntityScanPackages {
}
static class EntityScanPackagesBeanDefinition extends GenericBeanDefinition {
static class EntityScanPackagesBeanDefinition extends RootBeanDefinition {
private final Set<String> packageNames = new LinkedHashSet<>();

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
@ -22,7 +22,6 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
@ -70,10 +69,9 @@ class SharedMetadataReaderFactoryContextInitializerTests {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) context.getBeanFactory();
ConfigurationClassPostProcessor configurationAnnotationPostProcessor = mock(
ConfigurationClassPostProcessor.class);
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder
.rootBeanDefinition(ConfigurationClassPostProcessor.class)
BeanDefinition beanDefinition = BeanDefinitionBuilder
.rootBeanDefinition(ConfigurationClassPostProcessor.class, () -> configurationAnnotationPostProcessor)
.getBeanDefinition();
beanDefinition.setInstanceSupplier(() -> configurationAnnotationPostProcessor);
registry.registerBeanDefinition(AnnotationConfigUtils.CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME,
beanDefinition);
CachingMetadataReaderFactoryPostProcessor postProcessor = new CachingMetadataReaderFactoryPostProcessor(

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.

View File

@ -80,8 +80,8 @@ public class BoundConfigurationProperties {
Assert.notNull(registry, "Registry must not be null");
if (!registry.containsBeanDefinition(BEAN_NAME)) {
BeanDefinition definition = BeanDefinitionBuilder.rootBeanDefinition(BoundConfigurationProperties.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE)
.getBeanDefinition();
definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
registry.registerBeanDefinition(BEAN_NAME, definition);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
@ -24,7 +24,7 @@ import java.util.function.Supplier;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
@ -79,7 +79,7 @@ class ServletComponentScanRegistrar implements ImportBeanDefinitionRegistrar {
return packagesToScan;
}
static final class ServletComponentRegisteringPostProcessorBeanDefinition extends GenericBeanDefinition {
static final class ServletComponentRegisteringPostProcessorBeanDefinition extends RootBeanDefinition {
private final Set<String> packageNames = new LinkedHashSet<>();

View File

@ -24,7 +24,7 @@ import org.springframework.aop.scope.ScopedProxyFactoryBean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.context.properties.bind.BindMethod;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
@ -60,7 +60,7 @@ class ConfigurationPropertiesBeanRegistrarTests {
@Test
void registerWhenAlreadyContainsNameDoesNotReplace() {
String beanName = "beancp-" + BeanConfigurationProperties.class.getName();
this.registry.registerBeanDefinition(beanName, new GenericBeanDefinition());
this.registry.registerBeanDefinition(beanName, new RootBeanDefinition());
this.registrar.register(BeanConfigurationProperties.class);
BeanDefinition definition = this.registry.getBeanDefinition(beanName);
assertThat(definition).isNotNull();

View File

@ -51,8 +51,8 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.properties.bind.BindException;
@ -397,9 +397,9 @@ class ConfigurationPropertiesTests {
};
this.context.register(WithFactoryBeanConfiguration.class);
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(FactoryBeanTester.class);
beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
BeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(FactoryBeanTester.class)
.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE)
.getBeanDefinition();
this.context.registerBeanDefinition("test", beanDefinition);
this.context.refresh();
assertThat(WithFactoryBeanConfiguration.factoryBeanInitialized).as("Not Initialized").isTrue();