Spring Autowire By Name

By | | Updated : 2021-05-09 | Viewed : 57 times

Spring Autowire By Name

The current article shows how the spring bean autowire by name. The current examples show the autowire concept with annotation and XML.

Spring Autowire By Name

Spring provides autowire for dependency injection of three types. one of the options is Spring autowire by name. Here spring will inject the dependencies based on the name.

Spring Autowire By Name Xml

We will see the spring autowire by name with xml. Notice below given code snippets.

ASpringBean.java
public class ASpringBean {

    private String aSpringBeanName;

    private BSpringBean bSpringBean;

}
BSpringBean.java
public class BSpringBean {

    private String bSpringBeanName;

}
beans.xml
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="aSpringBean" class="com.docsconsole.tutorials.byname.xml.ASpringBean" autowire="byName">
		<property name="aSpringBeanName" value="aSpringBean"></property>
	</bean>

	<bean id="bSpringBean" class="com.docsconsole.tutorials.byname.xml.BSpringBean" >
		<property name="bSpringBeanName" value="bSpringBean"></property>
	</bean>

	<bean id="bSpringBean1" class="com.docsconsole.tutorials.byname.xml.BSpringBean" >
		<property name="bSpringBeanName" value="bSpringBean1"></property>
	</bean>

</beans> 

Notice beans1.xml. We have added property autowire="byName" and we have configured two bean xml tags for the same class BSpringBean. If you observe the ASpringBean it contains BSpringBean. So here BSpringBean with id="bSpringBean" will be injected into the ASpringBean.

Please below MainClient to test the autowire concept.

SpringMainClient.java
public class SpringMainClient {

	public static void main(String[] args) {
		

		ApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml");

		ASpringBean aSpringBean = context.getBean("aSpringBean", ASpringBean.class);

		aSpringBean.displayASpringBean();
		aSpringBean.getbSpringBean().displayBSpringBean();
		
	}
}

Spring Autowire By Name Annotation

Now we will look at annotation on how to handle the spring bean autowire by name. Please notice the below-given examples.

ASpringBean.java
@Getter
@Setter
public class ASpringBean {

    private String aSpringBeanName;

    @Autowired
    private BSpringBean bSpringBean;

	//other methods

}
BSpringBean.java
@Getter
@Setter
public class BSpringBean {

    private String bSpringBeanName;
	//other methods
}
SpringAppConfig.java
@Configuration
public class SpringAppConfig {

    @Bean
    public ASpringBean aSpringBean() {
        return new ASpringBean("ASpringBean");
    }

    @Bean("bSpringBean")
    public BSpringBean bSpringBean() {
        return new BSpringBean("BSpringBean");
    }

    @Bean("bSpringBean1")
    public BSpringBean bSpringBean1() {
        return new BSpringBean("BSpringBean1");
    }

}

Notice SpringAppConfig. BSpringBean configured with two names. One is bSpringBean and another one is bSpringBean1. So when you try to instantiate ASpringBean, then BSpringBean will be created for the name bSpringBean. BSpringBean with the name bSpringBean1 will not be created.

To test the above SpringBeans you can use MainClient as given below.

SpringAppClient
public class SpringAppClient {

    public static void main(String[] args) {

        ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(SpringAppConfig.class);
        context.registerShutdownHook();

        //
        ASpringBean aSpringBean = context.getBean(ASpringBean.class);
        aSpringBean.displayASpringBean();
        aSpringBean.getBSpringBean().displayBSpringBean();


    }
}

For code repo, you can refer Spring-Autowiring-Example-App.

Leave A Reply