Spring autowiring by constructor

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


package jbt.bean;

public class Address {

	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;



package jbt.bean;

public class Company {

	 * Below constructor will be used for Autowiring with COnstructor.
	public Company(String companyName, String companyCEO, Address address) {
		this.companyName = companyName;
		this.companyCEO = companyCEO;
		this.address = address;

	public String toString() {
		return "Company [companyName=" + companyName + ", companyCEO="
				+ companyCEO + ", address=" + address + "]";

	String companyName;
	String companyCEO;

	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"

<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 id="companyConstructorArg" class="jbt.bean.Company">
<ref bean="address"/>
<constructor-arg name="companyCEO" value="VN Gautam"></constructor-arg>
<constructor-arg name="companyName" value="JBT Enterprise"></constructor-arg>


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");



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

2 thoughts

  1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to top Search