From 41d9f21ab97b8cda9c3b740fc10b677c4d27fb50 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Wed, 11 Dec 2024 15:03:52 +0100 Subject: [PATCH] Log alias removal in DefaultListableBeanFactory Prior to this commit, information was logged when a bean definition overrode an existing bean definition, but nothing was logged when the registration of a bean definition resulted in the removal of an alias. With this commit, an INFO message is now logged whenever an alias is removed in DefaultListableBeanFactory. Closes gh-34070 --- .../beans/factory/support/DefaultListableBeanFactory.java | 5 +++++ .../beans/factory/DefaultListableBeanFactoryTests.java | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index f2fae6d6737a..e298c5c6958f 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -1170,6 +1170,11 @@ public void registerBeanDefinition(String beanName, BeanDefinition beanDefinitio } } else { + if (logger.isInfoEnabled()) { + logger.info("Removing alias '" + beanName + "' for bean '" + aliasedName + + "' due to registration of bean definition for bean '" + beanName + "': [" + + beanDefinition + "]"); + } removeAlias(beanName); } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java index b8eca28a3884..0621279dc81b 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java @@ -870,10 +870,15 @@ void aliasChaining() { void beanDefinitionOverriding() { lbf.setAllowBeanDefinitionOverriding(true); lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class)); + // Override "test" bean definition. lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class)); + // Temporary "test2" alias for nonexistent bean. lbf.registerAlias("otherTest", "test2"); + // Reassign "test2" alias to "test". lbf.registerAlias("test", "test2"); + // Assign "testX" alias to "test" as well. lbf.registerAlias("test", "testX"); + // Register new "testX" bean definition which also removes the "testX" alias for "test". lbf.registerBeanDefinition("testX", new RootBeanDefinition(TestBean.class)); assertThat(lbf.getBean("test")).isInstanceOf(NestedTestBean.class);