Setter-based Dependency Injection

Step 1 – Create new maven-based java project

You can create normal java project and add jar file to your project build path. But it’s faster using maven as maven is integrated with Eclipse IDE; so you don’t need to manually install it. You only need spring-context dependency.

Project Object Model (POM.XML)

Step 2 Create POJO class

Select your src folder of your project, copy the below code and press CTRL + V to paste the code. Eclipse IDE will automatically create new class with correct package.

Step 3 Create XML Bean Configuration file

In this example, I use Spring Tool Suite (STS) wizard to generate the file. You can just create an empty xml file named spring-bean.xml under package and paste the content below.

Step 4 Create test class

Compile & Run the application

Go to Project -> Clean to clean & build the project. Eclipse will download & add maven dependencies to your project build path; then compile your classes.

Right click to the test class; select Run As -> Java Application