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).
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)
Enough talking !!! do some code:
lets say you want to build Fee collection module of for Student.
There are two services:
While collecting fees for student you need to have access to StudentService. How will you do that using Spring.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:
- FeeCollectionService
- StudentService
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: