Knowledge Walls
J2EE Technologies Tutorial
Hyderabad, Andhra Pradesh, India
Dependency injection in Spring Framework with example
DI is dependency Injection. Spring supports DI to injecting beans in loosly coupled manner.

In below example Player is a definition class. PlayerDAO is a interface which is used in other Robot class. Player is not coupled anywhere tightly.
Download App as Zip 
Spring dependency injection example

Goto File menu then Click on Download
Step.1 Start a Java Project with Spring Jars 
  1. Open Eclipse
  2. Click on menu New -> Others
  3. In wizards type "Java Project" and Select "Java Project"
  4. Click Next
  5. Enter project name as "SpringDependencyInjectionExample", then click Next
  6. Goto Libraries tab, then click on Add External JARs, then select Spring's 21 Framework Jars and commons-logging-1.1.jar.
  7. Click Finish.
Step.2 Project Explorer Preview
package com.springexamples;

public interface PlayerDAO {
    public void playerDetails();
    public String getPlayerName();
    public int getPlayerAge();
package com.springexamples;

public class Player implements PlayerDAO {
    private String playerName;
    private int playerAge;
    public Player(String playerName,int playerAge){
        this.playerName = playerName;
        this.playerAge = playerAge;

    public String getPlayerName() {
        return playerName;
    public void setPlayerName(String playerName) {
        this.playerName = playerName;

    public int getPlayerAge() {
        return playerAge;
    public void setPlayerAge(int playerAge) {
        this.playerAge = playerAge;
    public void playerDetails(){
        System.out.println("Player Details\n");
        System.out.println("Name: "+this.playerName);
        System.out.println("Age: "+this.playerAge);
package com.springexamples;

public class Robot {
    private PlayerDAO player;
    public Robot(PlayerDAO player){
        this.player = player;
    public void sayHello(){
        System.out.println("Robot Saying, Hi, "+player.getPlayerName()+"("+player.getPlayerAge()+")");
    public void sayBye(){
        System.out.println("Robot Saying, Bye, "+player.getPlayerName()+"("+player.getPlayerAge()+")");
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <bean id="john" class="com.springexamples.Player">
        <constructor-arg value="John Peter" />
        <constructor-arg value="29" />
    <bean id="johnPlayer" class="com.springexamples.Robot">
        <constructor-arg ref="john" />

package com.springexamples;

import org.springframework.context.ApplicationContext;

public class RunMyProgram {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
            Robot player = (Robot) context.getBean("johnPlayer");
Robot Saying, Hi, John Peter(29)
Robot Saying, Bye, John Peter(29)
Best Lessons of "Spring 3.0 Examples"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details