Reputation: 97
How to alias a bean outside the bean definition using Java config in Spring Boot?
Upvotes: 6
Views: 1263
Reputation: 3814
I have this as well, and solved it like this:
@Component
public class AliasConfiguration implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
beanFactory.registerAlias("originalBeanName", "newAlias");
beanFactory.registerAlias("originalBeanName", "newAlias2");
beanFactory.registerAlias("otherOriginalBeanName", "newAlias3");
}
}
Upvotes: 6
Reputation: 1462
You want to alias a bean which is already defined somewhere else, this feature is not supported in spring yet.
Along with that aliasing a bean is not allowed in @Component
, @Service
and @Repository
.
Either you can alias a bean while defining in XML configuration or while using @Bean(name = {"alias1", "alias2"})
. But as you mentioned in you case bean is already defined in another JAR, it's not possible to alias it.
A similar(not exactly similar) issue is open to spring-framework
.
Upvotes: 0