What is the purpose of the Spring framework?

Java Spring Framework

From Spring Boot vs Spring MVC vs Spring – How do they compare?

What is the core problem that Spring Framework solves?

Think long and hard. What’s the problem Spring Framework solves?

Most important feature of Spring Framework is Dependency Injection. At the core of all Spring Modules is Dependency Injection or IOC Inversion of Control.

Why is this important? Because, when DI or IOC is used properly, we can develop loosely coupled applications. And loosely coupled applications can be easily unit tested.

Let’s consider a simple example:

Example without Dependency Injection

Consider the example below: WelcomeController depends on WelcomeService to get the welcome message. What is it doing to get an instance of WelcomeService? WelcomeService service = new WelcomeService();. It’s creating an instance of it. And that means they are tightly coupled. For example : If I create an mock for WelcomeService in a unit test for WelcomeController, How do I make WelcomeController use the mock? Not easy!

  1. @RestController
  2. public class WelcomeController {
  3.  
  4. private WelcomeService service = new WelcomeService();
  5.  
  6. @RequestMapping(“/welcome”)
  7. public String welcome() {
  8. return service.retrieveWelcomeMessage();
  9. }
  10. }
  11.  
  12.  

Same Example with Dependency Injection

World looks much easier with dependency injection. You let the spring framework do the hard work. We just use two simple annotations: @Component and @Autowired.

  • Using @Component, we tell Spring Framework – Hey there, this is a bean that you need to manage.
  • Using @Autowired, we tell Spring Framework – Hey find the correct match for this specific type and autowire it in.

In the example below, Spring framework would create a bean for WelcomeService and autowire it into WelcomeController.

In a unit test, I can ask the Spring framework to auto-wire the mock of WelcomeService into WelcomeController. (Spring Boot makes things easy to do this with @MockBean. But, that’s a different story altogether!)

  1. @Component
  2. public class WelcomeService {
  3. //Bla Bla Bla
  4. }
  5.  
  6. @RestController
  7. public class WelcomeController {
  8.  
  9. @Autowired
  10. private WelcomeService service;
  11.  
  12. @RequestMapping(“/welcome”)
  13. public String welcome() {
  14. return service.retrieveWelcomeMessage();
  15. }
  16. }
  17.  
  18.  

What else does Spring Framework solve?

Problem 1 : Duplication/Plumbing Code

Does Spring Framework stop with Dependency Injection? No. It builds on the core concept of Dependeny Injection with a number of Spring Modules

  • Spring JDBC
  • Spring MVC
  • Spring AOP
  • Spring ORM
  • Spring JMS
  • Spring Test

Consider Spring JMS and Spring JDBC for a moment.

Do these modules bring in any new functionality? No. We can do all this with J2EE or JEE. So, what do these bring in? They bring in simple abstractions. Aim of these abstractions is to

  • Reduce Boilerplate Code/ Reduce Duplication
  • Promote Decoupling/ Increase Unit Testablity

For example, you need much less code to use a JDBCTemplate or a JMSTemplate compared to traditional JDBC or JMS.

Problem 2 : Good Integration with Other Frameworks.

Great thing about Spring Framework is that it does not try to solve problems which are already solved. All that it does is to provide a great integration with frameworks which provide great solutions.

  • Hibernate for ORM
  • iBatis for Object Mapping
  • JUnit & Mockito for Unit Testing

Leave a Reply

Your email address will not be published. Required fields are marked *