Rajinder Menu

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.

Car.java
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){
        this.make=make;
    }
  
    public String getMake(){
        return make;
    }
  
    public void setModel(String model){
        this.model=model;
    }
  
    public String getModel(){
        return model;
    }
  
    public void setYear(int year){
        this.year=year;
    }
  
    public int getYear(){
        return year;
    }
  
    public void setEngine(Engine engine){
        this.engine=engine;
    }
  
    public Engine getEngine(){
        return engine;
    }

    public void showCarDetails(){
        System.out.println("Car Details:");
        System.out.println("Make="+getMake());
        System.out.println("Model="+getModel());
        System.out.println("Year="+getYear());
        engine.showEngineDetails();
      
    }

}

Engine.java 

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){
        this.engine=engine;
    }
   
    public String getEngine(){
        return engine;
    }
   
    public void setMaxPower(String maxPower){
        this.maxPower=maxPower;
    }
   
    public String getMaxPower(){
        return maxPower;
    }
    public void setEngineLocation(String engineLocation){
        this.engineLocation=engineLocation;
    }
   
    public String getEngineLocation(){
        return engineLocation;
    }
   
    public void showEngineDetails(){
        System.out.println("****Engine Details****");
        System.out.println("Engine="+getEngine());
        System.out.println("Maximum Power="+getMaxPower());
        System.out.println("Engine Location="+getEngineLocation());
    }

}

MainApp.java

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");
        car.showCarDetails();
      
        }

}

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

<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<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>

<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"/>
</bean>

</beans>


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