Sometimes we have use cases that require different codes to reference the same bean using other names or aliases. Fortunately, we can have one bean and give them multiple aliases in Spring. This post shows how to assign multiple names or aliases to a bean in Spring.
Spring Project Requirements
We use the following items for this post
- Spring Boot 2.0.6.RELEASE
- IntelliJ IDEA
- JDK 8
- Using Java Configuration
Multiple Names For a Bean Using XML
Even before the advent of Java configurations, Spring allows a bean to have multiple names using XML configurations. In fact, we can give a bean multiple names by using the name attribute of the bean element. Instead of specifying a single string, we could provide a set of strings delimited by specifying spaces, commas, or semi-colons. Consider the following sample Spring XML configuration.
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> |
Notice we defined two beans with different ids – idBean1 and idBean2. We used the name attribute for each bean to specify a list of string values to serve as their respective aliases. Alternatively, we can use the alias tag.
Multiple Names For a Bean Using Java Configuration
Using XML configuration, especially for large projects, is cumbersome and difficult to maintain. Therefore, we are better off starting new projects using Java configuration classes. Nowadays, most greenfield projects, if not all, are using Java configuration classes. In Java configuration, we can achieve aliasing in Spring by using an array of names as a parameter to the attribute of a bean. Consider the following codes.
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(); } } |
Referencing Bean Aliases In Spring
Referencing Spring bean aliases is straightforward using the Autowired annotation. But when we need to reference a bean with a specific name, we could use the Qualifier. Consider the following codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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; } } |
When we run the codes, we get the following output.
1 | BeanTwo using BeanOne and BeanOne and BeanOne |
The sample codes for our Spring with multiple names projects are available from the following links: Spring XML Config and Spring Java Config.
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.