Spring Bean Aliases with Programmatic Configuration

Categories: Java

Just a trivial note about defining aliases for beans with the Spring framework.

It can be convenient to give a bean an alternate name. In XML this is easy and well-documented:

<alias bean="foo" alias="foo2"/>

When using code-based configuration, it isn’t so well documented but also easy:

public class SomeConfigClass {
    public BeanFactoryPostProcessor defineAliasForFoo() {
        return (beanFactory) -> beanFactory.registerAlias("foo", "foo2");