Spring Fundamentals
Santhosh Duraiswamy
Aspiring Engineering Manager | Experienced Squad Lead | Expertise in Java & Micro-services |Team Development | Strategic Planning | Leadership
What to expect from this article
This article is a high-level overview of the Spring framework, its advantages and disadvantages. It's the first in the series of articles to come on Spring Fundamentals. It is a good idea to know the reason for the birth of any technology or framework to appreciate the design decision and its architecture. This article targets the newbies to the framework.
Prerequisite
The readers should have a basic understanding of Java
Foundation
Let's refresh some basic concepts before getting into Spring Framework
Any application small or big there will be multiple objects which interact with each other to get the job done. Object dependency talks about the relationships and the way each object fulfils its dependencies.
The term POJO was coined by?Martin Fowler, Rebecca Parsons and Josh MacKenzie in September 2000. POJO is a Java object which is not bound to any restrictions other than those forced by Java Specification. POJO focuses on business logic and has no dependency on frameworks. Spring adopted this idea very early and was one of the main reasons for its popularity.
But Why Spring
The framework started as a container that manages the object dependencies using POJO, but now it is much more than just a container. It came as a response to the complexity of the J2EE specification. One of the major complexity in enterprise development was to define and manage Enterprise Java Bean (EJB). This was replaced by POJOs which led to an interesting outcome of removing the necessity of an application server in enterprise application development. Yes, enterprise applications can be developed using Servlet Container/Web server like Tomcat, thanks to the Spring framework.
What is Spring
When someone says Spring, they might refer to the framework itself or other Spring projects. Mostly they refer to the complete family of projects. In simple words, Spring is an open-source software framework with a collection of projects which helps the developer to build applications with ease by removing a lot of boilerplate code, managing object dependency?and much more. It all started with the Spring framework mainly to remove the complexity of the enterprise application development. Spring Core is the foundational module on which all other projects are built. This module provides validation, internationalization, data binding support and much more. But the heart of the core module is Dependency Injection.?In this article, we are going to cover only the part of the Core module namely,
On top of the Core module, there are a lot of other projects developed to solve specific problems. Those projects provided an abstraction to complex problems like security, data and much more. Some of the projects are?
IoC is a design principle in software engineering that tells that the control flow is inverted compared to traditional programming. Let see, what it means by 'inverting the control flow'. Traditionally in most of the programming languages, the application we develop will call some framework to get some work done. But according to the IoC principle, the control or portion of control is taken by the framework i.e. the framework will call the application code. The framework implements the IoC principle which is, Spring IoC container or Application Context.?
DI is the mechanism/design pattern using which IoC is achieved in Spring. Dependency Injection inverted the control flow of managing the object dependency. This is the core concept that needs to be understood correctly to use Spring.
Let's assume there is a Car object like below in an application
领英推荐
public class Car {
private Engine engine;
private Gear gear;
}
?In this example, Car object is dependent on Engine and Gear objects. There are multiple ways to fulfil these dependencies.?
Approach 1 :
The car object itself will create the object of Engine and Gear, which means Car object will fulfil its dependencies by itself which is straight forward and easy way to do it. Like below
public class Car {
private Engine engine = new Engine();
private Gear gear = new Gear();
}
This approach creates a tight coupling because Car object is aware of Engine and Gear implementation. This makes it difficult to change the implementation of Engine or Gear objects in future. This can be reduced to a level using Factory pattern, but developers need to implement the factory pattern.?
Approach 2:
The car object will just declare its dependencies and let someone else fulfil the dependencies. This gives a big advantage which is loose coupling. In this approach, even when the implementation of Engine changes Car need not worry. Spring adopted this approach and achieved it using Dependency Injection.
Advantages of Spring
1. The framework is there for a very long time, the initial version came in 2003, still stable and active.
2. It is designed by considering a lot of best practices and design principles that have been there for many years. For example abstraction, loose coupling and much more.
3. It provides abstraction which makes the development easy for Web, Data, Security etc
4. Very large community means very good support for any of the Spring projects.
5. It can be used for the development of different kinds of applications standalone, web, distributed.?
6. Almost all the modern IDE has support for spring framework which makes life easier for the developer.
Disadvantages of Spring
1. Too many things go in the background, it is a bit magical.
2. Steep learning curve, but a lot of resources are available to help with this
3. Complexity has increased over time, which the Spring team was aware of it and this is one of the reasons for the Spring boot project.
References
Assistant Consultant at Tata Consultancy Services
3 年Good read!
System test
3 年A good article to start Spring fundamentals. ??