Artem Bilan opened SPR-16837 and commented

The simple test-case to reproduce:

@Test
public void aliasesConflict() {
    GenericApplicationContext applicationContext = new GenericApplicationContext();
    applicationContext.setAllowBeanDefinitionOverriding(false);

    String beanName = "foo";
    String alias = "bar";

    applicationContext.registerBean(beanName, String.class);
    applicationContext.registerAlias(beanName, alias);

    applicationContext.removeBeanDefinition(beanName);

    applicationContext.registerBean("foo2", String.class);
    applicationContext.registerAlias("foo2", alias);

    applicationContext.close();
}

Ends up with an exception:

java.lang.IllegalStateException: Cannot register alias 'bar' for name 'foo2': It is already registered for name 'foo'.

    at org.springframework.core.SimpleAliasRegistry.registerAlias(SimpleAliasRegistry.java:60)
    at org.springframework.context.support.GenericApplicationContext.registerAlias(GenericApplicationContext.java:341)

The point is: there is no a bean for that alias any more, so why do we keep it?

Thanks


Affects: 5.0.6

Referenced from: pull request https://github.com/spring-projects/spring-framework/pull/1837

Comment From: spring-projects-issues

xin commented

It sames that:

When GenericApplicationContext.removeBeanDefinition(), it should remove this bean had register alias in SimpleAliasRegistry.aliasMap.

Comment From: snicoll

I don't think that is accurate. Your test shows that registering the bean and an alias to it are two separate operations. From that perspective, removal should follow suit.