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

Address.java

Company.java

Spring Bean Metadata

Spring Autowiring

If you try to get the Company Bean by using below line.

Output of the above code would be

Click links below to learn more about Autowiring by different ways

2 Comments Spring autowiring by constructor

  1. Deiva

    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.

    Reply

Leave A Comment

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