Spring Autowire By Constructor
By AmarSivas | | Updated : 2021-05-27 | Viewed : 214 times

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.
Table of Contents:
Spring Autowire By Constructor
Spring allows us to autowire/inject the dependencies by
Spring Autowire By Constructor XML
Let\'s get started to write the code for
public class ASpringBean {
private ASpringBean(BSpringBean bSpringBean){
this.bSpringBean2 = bSpringBean;
}
private String aSpringBeanName;
private BSpringBean bSpringBean2;
public void displayASpringBean() {
System.out.println(this.aSpringBeanName);
}
}
public class BSpringBean {
private String bSpringBeanName;
public void displayBSpringBean() {
System.out.println(this.bSpringBeanName);
}
}
<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
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 Annotation
To start the code for
@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);
}
}
@Getter
@Setter
public class BSpringBean {
private String bSpringBeanName;
public BSpringBean(String bSpringBeanName) {
this.bSpringBeanName = bSpringBeanName;
}
public void displayBSpringBean() {
System.out.println(this.bSpringBeanName);
}
}
@Configuration
public class SpringAppConfig {
@Bean
public ASpringBean aSpringBean() {
return new ASpringBean("ASpringBean", bSpringBean());
}
@Bean
public BSpringBean bSpringBean() {
return new BSpringBean("BSpringBean");
}
}
Notice
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 constructor-arg
We have another option to autowire by constructor which is a
<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
To get the entire code repo you can refer the Spring-Autowiring-Example-App