Polishing BeanOverrideBeanFactoryPostProcessor to also copy isFallback

This commit is contained in:
Simon Baslé 2024-05-16 11:29:31 +02:00
parent 6f6e25bd5b
commit b17d1c5124
2 changed files with 6 additions and 2 deletions

View File

@ -101,10 +101,12 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
* Copy certain details of a {@link BeanDefinition} to the definition created by
* this processor for a given {@link OverrideMetadata}.
* <p>The default implementation copies the {@linkplain BeanDefinition#isPrimary()
* primary flag} and the {@linkplain BeanDefinition#getScope() scope}.
* primary flag}, @{@linkplain BeanDefinition#isFallback() fallback flag}
* and the {@linkplain BeanDefinition#getScope() scope}.
*/
protected void copyBeanDefinitionDetails(BeanDefinition from, RootBeanDefinition to) {
to.setPrimary(from.isPrimary());
to.setFallback(from.isFallback());
to.setScope(from.getScope());
}

View File

@ -156,12 +156,13 @@ class BeanOverrideBeanFactoryPostProcessorTests {
}
@Test
void copyDefinitionPrimaryAndScope() {
void copyDefinitionPrimaryFallbackAndScope() {
AnnotationConfigApplicationContext context = createContext(SingletonBean.class);
context.getBeanFactory().registerScope("customScope", new SimpleThreadScope());
RootBeanDefinition definition = new RootBeanDefinition(String.class, () -> "ORIGINAL");
definition.setScope("customScope");
definition.setPrimary(true);
definition.setFallback(true);
context.registerBeanDefinition("singleton", definition);
context.register(SingletonBean.class);
@ -169,6 +170,7 @@ class BeanOverrideBeanFactoryPostProcessorTests {
assertThat(context.getBeanDefinition("singleton"))
.isNotSameAs(definition)
.matches(BeanDefinition::isPrimary, "isPrimary")
.matches(BeanDefinition::isFallback, "isFallback")
.satisfies(d -> assertThat(d.getScope()).isEqualTo("customScope"))
.matches(Predicate.not(BeanDefinition::isSingleton), "!isSingleton")
.matches(Predicate.not(BeanDefinition::isPrototype), "!isPrototype");