DefaultListableBeanFactory checks for alias circle on registerAlias (avoiding endless loop; SPR-7274)

This commit is contained in:
Juergen Hoeller 2010-06-10 21:45:47 +00:00
parent 433b4eff8f
commit 11330baf77
2 changed files with 32 additions and 0 deletions

View File

@ -723,6 +723,20 @@ public class DefaultListableBeanFactoryTests {
assertTrue("Test bean age is 48", tb.getAge() == 48);
}
@Test
public void testAliasCircle() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
lbf.registerAlias("test", "test2");
lbf.registerAlias("test2", "test3");
try {
lbf.registerAlias("test3", "test");
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
}
@Test
public void testBeanDefinitionOverriding() {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();

View File

@ -55,6 +55,7 @@ public class SimpleAliasRegistry implements AliasRegistry {
name + "': It is already registered for name '" + registeredName + "'.");
}
}
checkForAliasCircle(name, alias);
this.aliasMap.put(alias, name);
}
}
@ -128,6 +129,7 @@ public class SimpleAliasRegistry implements AliasRegistry {
"') for name '" + resolvedName + "': It is already registered for name '" +
registeredName + "'.");
}
checkForAliasCircle(resolvedName, resolvedAlias);
this.aliasMap.remove(alias);
this.aliasMap.put(resolvedAlias, resolvedName);
}
@ -157,4 +159,20 @@ public class SimpleAliasRegistry implements AliasRegistry {
return canonicalName;
}
/**
* Check whether the given name points back to given alias as an alias
* in the other direction, catching a circular reference upfront and
* throwing a corresponding IllegalStateException.
* @param name the candidate name
* @param alias the candidate alias
* @see #registerAlias
*/
protected void checkForAliasCircle(String name, String alias) {
if (alias.equals(canonicalName(name))) {
throw new IllegalStateException("Cannot register alias '" + alias +
"' for name '" + name + "': Circular reference - '" +
name + "' is a direct or indirect alias for '" + alias + "' already");
}
}
}