Spring Autowire By Constructor

By | | Updated : 2021-05-27 | Viewed : 30 times

Spring Autowire By Constructor

We have learned spring autowire by name and type so far. Now we will be focusing on the constructor. We will try with xml and annotations.

Spring Autowire By Constructor

Spring allows us to autowire/inject the dependencies by Spring Autowire By Constructor. We will see that how can we inject the dependencies by the constructor. we will learn this concept with examples here.

Spring Autowire By Constructor XML

Let\'s get started to write the code for Spring Autowire By Constructor XML. Please see the below-given code snippets.

ASpringBean.java
public class ASpringBean {

    private ASpringBean(BSpringBean bSpringBean){
        this.bSpringBean2 = bSpringBean;
    }
    private String aSpringBeanName;

    private BSpringBean bSpringBean2;

    public void displayASpringBean() {
        System.out.println(this.aSpringBeanName);
    }

    
}
BSpringBean.java
public class BSpringBean {

    private String bSpringBeanName;

    public void displayBSpringBean() {
        System.out.println(this.bSpringBeanName);
    }
   
}
beans2.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.byconstructor.xml.constructor.ASpringBean" autowire="constructor">
		<property name="aSpringBeanName" value="aSpringBean"></property>
	</bean>

	<bean id="bSpringBean2" class="com.docsconsole.tutorials.byconstructor.xml.constructor.BSpringBean" >
		<property name="bSpringBeanName" value="bSpringBean2"></property>
	</bean>

</beans>

Notice the beans2.xml file here. We autowire the BSpringBean into ASpringBean using an attribute called autowire="constructor" and we created a constructor in ASpringBean with BSpringBean. Notice the code in ASpringBean.

SpringMainClient.java
public class SpringMainClient {

	public static void main(String[] args) {
		

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

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

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

}

Use the client code to test the Spring Autowire By Constructor XML.

Spring Autowire By Constructor Annotation

To start the code for Spring Autowire By Constructor Annotation we will first write the spring beans as given below with annotations.

ASpringBean.java
@Getter
@Setter
public class ASpringBean {

    private String aSpringBeanName;

    private BSpringBean bSpringBean;

    @Autowired
    public ASpringBean(String aSpringBeanName, BSpringBean bSpringBean) {
        this.aSpringBeanName = aSpringBeanName;
        this.bSpringBean = bSpringBean;
    }

    public void displayASpringBean() {
        System.out.println(this.aSpringBeanName);
    }

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

    private String bSpringBeanName;

    public BSpringBean(String bSpringBeanName) {
        this.bSpringBeanName = bSpringBeanName;
    }
    public void displayBSpringBean() {
        System.out.println(this.bSpringBeanName);
    }
}
SpringAppConfig.java
@Configuration
public class SpringAppConfig {

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

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

}

Notice ASpringBean class we autowired BSpringBean using the constructor. We configured the same in aSpringBean() of SpringAppConfig for the same.

SpringAppClient.java
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();


    }
}

Use the above client code to test the Spring Autowire By Constructor Annotation

Spring Autowire By Constructor constructor-arg

We have another option to autowire by constructor which is a constructor-arg tag. So here we required to pass the bean ref. So when you create the dependent bean it automatically creates and autowire the dependency bean.

beans3.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.byconstructor.xml.constructorarg.ASpringBean" >
		<property name="aSpringBeanName" value="aSpringBean"></property>
		<constructor-arg ref="bSpringBean2"/>
	</bean>

	<bean id="bSpringBean2" class="com.docsconsole.tutorials.byconstructor.xml.constructorarg.BSpringBean" >
		<property name="bSpringBeanName" value="bSpringBean2"></property>
	</bean>

</beans>

Please notice the above XML file. Here we are autowiring the BSpringBean into ASpringBean using

To get the entire code repo you can refer the Spring-Autowiring-Example-App

Leave A Reply