Category Archives: Spring Tutorial

Spring- Dependency Injection

Problem without DI

class Restuarant{
Tea tea = new Tea();

class Tea{
public void prepareHotDrink(){ }

Here we have tightly coupled object (Restaurant is dependent on Tea object)

We can make an interface like

interface IHotDrink{
public void prepareHotDrink();

And then we can implement as follows:

class Restuarant{
IHotDrink hotdrink;
Restuarant(IHotDrink hotdrink){
hotdrink = this.hotdrink;


class Tea implements IHotDrink{
public void prepareHotDrink(){ }

We can solve this tightly coupled thing with two ways
1. Using the constructor way
2. Using the setter method way

Spring Introduction

Spring is having five major components.
1. Dependency injection – Do not create or instanciate an object inside another java class . Instead rely on Spring’s IOP module to create the object for you.
2. Aspect Oriented programing – Do not mix non Business logic with actual Business logic in your classes like separate the logging, caching, transaction mang etc and business logic a+b from the same class.
3. Data Access/Integration – contains 5 sub modules JDBC, ORM, OXM, JMS, Transaction Management

  • Spring JDBC – opening and closing of database connections
    – Any exception handling code
    – Prepare and execute statements
    – ResultSet
  • Spring ORM – Object Relation Mapping
    – Provide full API support to integrate ORM tools like – Hibernate, JPA, IBATIS etc
  • Spring OXM – Converting a java object into XML or XML to java
    – Provide full APi support to to OXM frameworks like, JAXB, Caster, JiBX, etc
  • Spring JMS – Java messaging service
    – Provide full API support to produce and consume messages from JMS queues
  • Spring Transaction Management – Provide two ways of transaction management
    – Programmatic way and declarative way

4. Spring MVC – Using this we can make a web application using MVC pattern in a simple and good manner.
5. Test Framework – It provide support for testing spring components with JUnit and TestNG.