DefaultListableBeanFactory checks for alias circle on registerAlias (avoiding endless loop; SPR-7274)
This commit is contained in:
parent
433b4eff8f
commit
11330baf77
|
|
@ -723,6 +723,20 @@ public class DefaultListableBeanFactoryTests {
|
||||||
assertTrue("Test bean age is 48", tb.getAge() == 48);
|
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
|
@Test
|
||||||
public void testBeanDefinitionOverriding() {
|
public void testBeanDefinitionOverriding() {
|
||||||
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,7 @@ public class SimpleAliasRegistry implements AliasRegistry {
|
||||||
name + "': It is already registered for name '" + registeredName + "'.");
|
name + "': It is already registered for name '" + registeredName + "'.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
checkForAliasCircle(name, alias);
|
||||||
this.aliasMap.put(alias, name);
|
this.aliasMap.put(alias, name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -128,6 +129,7 @@ public class SimpleAliasRegistry implements AliasRegistry {
|
||||||
"') for name '" + resolvedName + "': It is already registered for name '" +
|
"') for name '" + resolvedName + "': It is already registered for name '" +
|
||||||
registeredName + "'.");
|
registeredName + "'.");
|
||||||
}
|
}
|
||||||
|
checkForAliasCircle(resolvedName, resolvedAlias);
|
||||||
this.aliasMap.remove(alias);
|
this.aliasMap.remove(alias);
|
||||||
this.aliasMap.put(resolvedAlias, resolvedName);
|
this.aliasMap.put(resolvedAlias, resolvedName);
|
||||||
}
|
}
|
||||||
|
|
@ -157,4 +159,20 @@ public class SimpleAliasRegistry implements AliasRegistry {
|
||||||
return canonicalName;
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue