Spring Core- Dependency Injection using setter method

Spring Core- Dependency Injection using setter method

In Java programming language, it is common that one class contains reference of other class. This is said to be “HAS A relationship” in OOP.

Spring framework provides facility to inject one bean to another bean to achieve “HAS ¬†A” relationship.

The below example demonstrate dependency injection using setter method.

1) School and Library java class

School has three instance variables name, noOfFaculties and reference of other class Library. Library class has one instance variable name. classes are as below



2) Spring Configuration

We have created two beans, library and school.

Dependency injection of Library class into School class has been done into school bean through property tag.

3) Load bean xml and run the program

4) Output

Name : Code2Succeed
No of Faculties : 100
Library Name : c2sLibrary

Leave a Reply

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