This post shows how to assign multiple names or aliases to a bean in Spring.
Contents
Item We Used
- Spring Boot 2.0.6.RELEASE
- IntelliJ IDEA
- JDK 8
- Using Java Configuration
Bean Name Aliasing
Spring allows a bean to have multiple names.
Using XML Configuration
In XML, we can achieve aliasing by specifying a space-, comma, or semi-colon delimited string of names in the name attribute of the bean’s <bean> tag.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="idBean1" name="bean1 beanA,beanUno;b1" class="com.turreta.springboot.springcore.beanalias.xmlconfig.comturretaspringbootspringcorebeanaliasxmlconfig.BeanOne"/> <alias name="bean1" alias="bA"/> <bean id="idBean2" name="bean2 beanB,beanDos;b2" class="com.turreta.springboot.springcore.beanalias.xmlconfig.comturretaspringbootspringcorebeanaliasxmlconfig.BeanTwo"/> <alias name="bean2" alias="bB"/> </beans> |
We can also use the <alias> tag.
Referencing Aliases
We can now reference the same bean using different aliases.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.turreta.springboot.springcore.beanalias.xmlconfig.comturretaspringbootspringcorebeanaliasxmlconfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; public class BeanTwo { @Autowired @Qualifier("b1") private BeanOne beanOneA; @Autowired @Qualifier("bA") private BeanOne beanOneB; @Autowired @Qualifier("idBean1") private BeanOne beanOneC; @Override public String toString() { return "BeanTwo using " + beanOneA + " and " + beanOneB + " and " + beanOneC; } } |
Output
1 |
BeanTwo using BeanOne and BeanOne and BeanOne |
Please download the codes here.
Using Java Configuration
In Java configuration, we can achieve aliasing by using an array of names as a parameter to the @Bean’s name attribute.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.turreta.springboot.springcore.beanalias.javaconfig.comturretaspringbootspringcorebeanaliasjavaconfig; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportResource; import org.springframework.core.annotation.AliasFor; @Configuration public class MyConfiguration { @Bean(name = {"idBean1", "bean1", "beanA", "beanUno", "b1", "bA"}) // OR @Bean({"idBean1", "bean1", "beanA", "beanUno", "b1", "bA"}) public BeanOne c() { return new BeanOne(); } @Bean({"idBean2", "bean2", "beanB", "beanDos", "b2", "bB"}) public BeanTwo d() { return new BeanTwo(); } } |
Output
1 |
BeanTwo using BeanOne and BeanOne and BeanOne |
Please download the codes here.
what happens if the bean already exists? for instance say I want to do:
but “foo” already exists elsewhere.
Should I do?
@Bean(“bar”)
public Object bar(@Qualifier(“foo”) Object foo) {
return foo;
}
the only other way I could see to do this is to implement a BeanPostProcessor and call the registerAlias method, but then I’m stuck not being able to reference “bar” within this Configuration.
I think that is fine as long as it refers to the same object. You could create a separate Configuration class just for aliasing stuff like this.