Spring Setter Dependency Injection Example using Eclipse

This example shows how to inject dependencies to Spring beans using Setter Based Dependency Injection method.

This example is using following technologies:

Note: The Spring 3.0 requires at least Java 1.5. 

Step 1: Create a new Project in eclipse.

Create a new project in eclipse by going to File > New > Java Project and name it as SpringDISetter as shown in following figure.

Project structure in eclipse is shown below:

There are four files used in this program:

1)      Car.java
2)      Engine.java
3)      MainApp.java
4)      ApplicationContext.java

Create these files in eclipse. If you want to see in detail how a basic Spring project is created in eclipse, you can refer here.

Step2: Add the source code.

Add the following source code in eclipse.

package examples.spring.di;

public class Car {

    private String make;
    private String model;
    private int year;
    private Engine engine;
    /*Dependency Injection by Setter methods*/
    public void setMake(String make){
    public String getMake(){
        return make;
    public void setModel(String model){
    public String getModel(){
        return model;
    public void setYear(int year){
    public int getYear(){
        return year;
    public void setEngine(Engine engine){
    public Engine getEngine(){
        return engine;

    public void showCarDetails(){
        System.out.println("Car Details:");



package examples.spring.di;

public class Engine {
    private String engine;
    private String maxPower;
    private String engineLocation;
    /*Dependency Injection by Setter methods*/
    public void setEngine(String engine){
    public String getEngine(){
        return engine;
    public void setMaxPower(String maxPower){
    public String getMaxPower(){
        return maxPower;
    public void setEngineLocation(String engineLocation){
    public String getEngineLocation(){
        return engineLocation;
    public void showEngineDetails(){
        System.out.println("****Engine Details****");
        System.out.println("Maximum Power="+getMaxPower());
        System.out.println("Engine Location="+getEngineLocation());



package examples.spring.di;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String args[]){
        ApplicationContext ctx=new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
        Car car=(Car)ctx.getBean("myCar");


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

<bean id="myCar" class="examples.spring.di.Car">

    <!-- Injecting primitive values as Dependencies-->
    <property name="make" value="Maruti Suzuki"/>
    <property name="model" value="Wagon R"/>
    <property name="year" value="2012"/>

    <!-- Injecting reference to other bean as Dependency-->
    <property name="engine" ref="myEngine"/>


<bean id="myEngine" class="examples.spring.di.Engine">
<!-- Injecting primitive values as Dependencies-->
    <property name="engine" value="3664 cc"/>
    <property name="maxPower" value="304HP"/>
    <property name="engineLocation" value="front"/>


Step3: Add jars

You will need to add following jars to your application.

1) org.springframework.beans-3.0.6.RELEASE.jar
2) org.springframework.core-3.0.6.RELEASE.jar
3) org.springframework.context-3.0.6.RELEASE.jar
4) org.springframework.asm-3.0.6.RELEASE.jar
5) org.springframework.expression-3.0.6.RELEASE.jar
6) commons-logging-1.1.1.jar 

You can download commons-logging-1.1.1.jar from here. Spring related jars can be found in dist folder of your downloaded Spring 3.0.6 distribution folder.

To add above jars right click on your project SpringDISetter,  go to Build Path > Configure Build Path > Add External JARs... and add them one by one.

Step 4: Running the program.
 Right Click on the project SpringDISetter and go to Run As > Java Application.

You will see the output  in the Console Pane at bottom.

Hope this would be useful to you!

I would like to know your comments and if you liked the article then please share it on social networking buttons.

No comments:

Post a Comment