what is spring framework

Today we will talk about Spring framework.

Though you will find numerous websites and blogging available on internet regarding spring, I am going to put some more insight on why spring was needed and how the spring framework works. The basic motive of spring development was dependency injection and simplified programming model using java-beans (simple POJO).

So what is mean by dependency injection.?

    Every class will define there dependency. And the spring container will provide you the same.
    for eg. You went to restaurant , and asked for a coffee, burgers. Now there are two types how the order will be delivered,
        1. Self service. (Service Locator Pattern)
        2. Service by the Restaurant staff. (Dependency injection)    

    1. The Service Locator Pattern: 

        Now in the above example the Restaurant is the Spring application container who provide all types of         services. You ask the Spring application container for one service and look for it yourself.
       The service locator design pattern is used when we want to locate various services using JNDI                      lookup.

    2. The Dependency Injection pattern: 

         In the above example you defined what type of food you want to have? and it will be given by                     restaurant.
         In Dependency Injection the application container will inject the dependency. You just define what              service you want to inject and you are done.The container will do it for you.

Enough talking !!! do some code:
lets say you want to build Fee collection module of for Student.

There are two services:

  1. FeeCollectionService
  2. StudentService
While collecting fees for student you need to have access to StudentService. How will you do that using Spring.

StudentService.java:

package com.mvc.tutorial.service;

import com.mvc.tutorial.bean.StudentBean;

public interface StudentService {

 public StudentBean getStudentById(int studentId);
}

FeeCollectionService.java:

package com.mvc.tutorial.service;

import java.math.BigDecimal;

public interface FeeCollectionService {

    public BigDecimal getFeesByStudentId(int studentId);
}
Now we will implement the FeeService.
package com.mvc.tutorial.service.impl;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;

import com.mvc.tutorial.bean.StudentBean;
import com.mvc.tutorial.repository.FeeRepository;
import com.mvc.tutorial.service.FeeCollectionService;
import com.mvc.tutorial.service.StudentService;

public class FeeCollectionServiceImpl implements FeeCollectionService {

    @Autowired
    private StudentService studentService;  // component is injected by spring

    @Autowired
    private FeeRepository feeRepository;  // component is injected by spring

    @Override
    public BigDecimal getFeesByStudentId(int studentId) {

        StudentBean studentBean = studentService.getStudentById(studentId);

        if (studentBean != null) {
            // throw custom exception
        }

        return feeRepository.findByStudentId(studentBean.getId());

    }

}
The approach mentioned above is one way of injecting the dependency in spring framework. There
are other approach (explicit dependency reference using constructor injection and setter method
based injection) also provided by spring framework. spring simplifies the programming approach
by simple use of POJO (plain old java objects).

There are other features of spring framework which are as below:

Lightweight:

Spring Framework is lightweight with respect to size and transparency.

Inversion Of Control (IOC):

In Spring Framework, loose coupling is achieved using Inversion of Control. The objects give their own dependencies instead of creating or looking for dependent objects.

Aspect Oriented Programming (AOP):

By separating application business logic from system services, Spring Framework supports Aspect Oriented Programming and enables cohesive development.

Container:

Spring Framework creates and manages the life cycle and configuration of application objects.

MVC Framework:

Spring Framework is a MVC web application framework. This framework is configurable via interfaces and accommodates multiple view technologies.

Transaction Management:

For transaction management, Spring framework provides a generic abstraction layer. It is not tied to J2EE environments and it can be used in container-less environments.

Good Exception Handling:

The JDBC abstraction layer of the Spring Framework offers an exception hierarchy, which simplifies the error handling strategy.

Cookies Consent

This website uses cookies to offer you a better Browsing Experience. By using our website, You agree to the use of Cookies

Privacy Policy