Spring Bean DI- Setter based injection with XML Metadata configuration

In this tutorial we will discuss about dependency injection done via setter injection.To perform setter based dependency injection container first calls no-argument constructor or no-argument static factory method to instantiate bean and after instantiating bean it will calls setter method on beans.

Note**: In case there is an argument based constructor is defined in class but no no-arg constructor is there then setter injection can not be performed. Hence no-arg constructor
would be required to perform setter injection.

Example : Address class has 5 properties(flatNo, bldgNo, streetNo, city, pincode). Setter injection can be done by providing below configuration.

 

2032CBhagat Singh StreetMumbai

 

If Contact class two property one of name and other of reference to Address then setter injection can be done through reference too.

 

 

Above configuration can also be written in below format.

 

 

Leave A Comment

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.