Dependency Injection with Java Spring IoC, A Complete Example

Here i am going to describe a simple example of Dependency Injection which uses Spring IoC framework.
You may refer to my earlier"Understanding Dependency Injection and Its Importance" for understanding the concepts.
This example uses Implementation of  Operation, Reader and Writer Interfaces.
We simply perform a one of the arithmentic Operation by taking values from Reader and write the values using Writer Class.

Reader Interface:
public interface Reader {
    Operands readValues(String promptMsg);

Its implementations can be:
public class ConsoleReader implements Reader{
    Operands oprnds;
    Scanner sc;
    public ConsoleReader (){
        oprnds=new Operands();
        sc=new Scanner(;
    public Operands readValues(String promptMsg) {
        return oprnds;

The Operation Interface
public interface Operation {
    Result operate(long op1,long op2);
    String getOperationName();

The Operation Interface can have implementations such as
public class Multiply implements Operation {
    Result res;
    public Multiply(){
          res=new Result();
    public Result operate(long op1, long op2) {
        return res;
    public String getOperationName() {
        return "Multiply";

public class Add implements Operation {
    Result res;
    public Add(){
        res=new Result();
    public Result operate(long op1, long op2) {
        return res;
    public String getOperationName() {
        return "Add";

The  writer Interface
public interface Writer {
    void write(Result res);

Its ConsoleWriter Implementation
public class ConsoleWriter implements Writer{
 public void write(Result res){
    System.out.println("The result after operation : "+res.getRes());

Its TXTFileWriter Implementation
public class TXTFileWriter implements Writer{
    File file;
    PrintWriter fwriter;
    public TXTFileWriter(){
        try {
           file = new File("output.txt");
           fwriter = new PrintWriter( new BufferedWriter( new FileWriter(file)));
       } catch (Exception ex) {
    public void write(Result res)

The Model Classes
public class Operands {
    private long op1;
    private long op2;
//setters and getters

The Result Class
public class Result {
    private long res;
    //setters and getters

Now create a configuration file in class path i.e, src folder of project directory - confBean.xml.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
<bean id="operation" class=""/>
<bean id="reader" class=""/>
<bean id="writer" class=""/>

The dependency injection will help you to inject the implementations of an object at runtime.
In the Main Class: read the xml file and inject the dependent beans…
public class Main {
    public static void main(String[] args) {
    ApplicationContext ctx = 
new ClassPathXmlApplicationContext("confBean.xml");
        //get beans from ctx
        Reader rdr = (Reader) ctx.getBean("reader");
        Operation opr = (Operation)ctx.getBean("operation");
        Writer wrt = (Writer) ctx.getBean("writer");//
        //read operands
        Operands opnds = rdr.readValues("Enter the Values :");
        //do operation
        Result res =opr.operate(opnds.getOp1(), opnds.getOp2());
        //write result

No comments :

Post a Comment

Your Comment and Question will help to make this blog better...