In this article we will learn about the Autowiring by Constructor. (Thanks to Basanta for this Article)
Autowiring byConstructor means whenever spring finds any bean to autowire it will search for compatible constructor with given number of parameter.
Spring Autowiring byConstructor Example
Address.java
package jbt.bean;
public class Address {
@Override
public String toString() {
return "Address [streetNo=" + streetNo + ", houseNo=" + houseNo
+ ", city=" + city + ", state=" + state + "]";
}
String streetNo;
String houseNo;
String city;
String state;
public String getStreetNo() {
return streetNo;
}
public void setStreetNo(String streetNo) {
this.streetNo = streetNo;
}
public String getHouseNo() {
return houseNo;
}
public void setHouseNo(String houseNo) {
this.houseNo = houseNo;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
Company.java
package jbt.bean;
public class Company {
/*
* Below constructor will be used for Autowiring with COnstructor.
*/
public Company(String companyName, String companyCEO, Address address) {
super();
this.companyName = companyName;
this.companyCEO = companyCEO;
this.address = address;
}
@Override
public String toString() {
return "Company [companyName=" + companyName + ", companyCEO="
+ companyCEO + ", address=" + address + "]";
}
String companyName;
String companyCEO;
//@Autowired
Address address;
public Address getCompanyAddress() {
return address;
}
public void setCompanyAddress(Address companyAddress) {
this.address = companyAddress;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getCompanyCEO() {
return companyCEO;
}
public void setCompanyCEO(String companyCEO) {
this.companyCEO = companyCEO;
}
}
Spring Bean Metadata
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="jbt.bean" />
<bean id="address" class="jbt.bean.Address">
<property name="streetNo" value="2nd Street" />
<property name="houseNo" value="345" />
<property name="city" value="Mumbai" />
<property name="state" value="Maharastra" />
</bean>
<bean id="companyConstructorArg" class="jbt.bean.Company">
<constructor-arg>
<ref bean="address"/>
</constructor-arg>
<constructor-arg name="companyCEO" value="VN Gautam"></constructor-arg>
<constructor-arg name="companyName" value="JBT Enterprise"></constructor-arg>
</bean>
</beans>
Spring Autowiring
If you try to get the Company Bean by using below line.
package jbt.spring;
import jbt.bean.Company;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringAutowireExample {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "JBTBean.xml" });
Company company = (Company) context.getBean("companyConstructorArg");
System.out.println(company.toString());
}
}
Output of the above code would be
Company [companyName=JBT Enterprise, companyCEO=VN Gautam, address=Address [streetNo=2nd Street, houseNo=345, city=Mumbai, state=Maharastra]]
Click links below to learn more about Autowiring by different ways
Hi,
I had one doubt in this article.
The above configuration defines the constructor arguments to be ‘address’, ‘companyCEO’ and then ‘companyName’.
But, the constructor is
public Company(String companyName, String companyCEO, Address address)
Isnt the order important here? I am not sure. Please clarify.
Please check your mail