Introduction to Spring Framework

Spring framework is an open-source Java framework to build an enterprise-grade application. It provides a solid infrastructure foundation to support robust application development.

Starting way back in 2003 by Rod Johnson, it was initially used as a Dependency Injection container. It emerged as a full-fledged, feature-rich, and mature ecosystem over a period of time.

Spring Framework modules

With the latest release, 5.x, the Spring framework has been equipped with many rapid application development features. As a Java developer, you must know in-depth knowledge of Spring Framework.

In this article, we will explore a few fundamentals. Though it’s pure theory, it’s imperatively important for you to just walk through it. Even If you have prior Spring experience, it would be advisable to browse what it is briefly. You will get more in-depth information about the framework from its official site link.

1. What is the Spring Framework?

The spring framework is loved by millions of developers and became the de-facto standard for developing high-performance, reusable, and easy-to-test enterprise applications. As a framework, it is lightweight in nature and allows a POJO-based programming model.

Moreover, Spring is non-invasive because it doesn’t require you to extend or implement your custom classes from any out of the box classes or interface provided by Spring forcefully.

However, Spring provides API in the form of a set of classes and interface to work with other frameworks like ORMs, Web frameworks, REST API, logging framework, No-SQL frameworks, schedulers, etc. That is the reason it’s called a framework of frameworks, as it supports various other frameworks.

On top of this, Spring is flexible enough to change the underlying implementation by simply changing the configuration.

You can leverage the Spring framework to build various applications, be it an independent Java application or web application. Moreover, the Spring framework can be used to build a specific layer of the application. It integrates seamlessly with other layers developed with different Java APIs or frameworks.

2. Spring Framework features

After attending huge popularity as a robust application framework and gaining a high degree of maturity, the Spring framework has been experienced as a feature-rich framework by a large developer community. we will see a few of them as follows:

Lightweight: The spring framework doesn’t need a full-fledged application server. The Servlet container is just sufficient to run the Spring application. So Spring is truly a lightweight framework in the sense of size and functionality.

Inversion Of Control (IOC) Controller: As a part of the Spring framework core module, the IOC container is responsible for providing the necessary infrastructure for managing the beans (object) lifecycle.

Modularity: The core of the Spring framework is modularity. You can use only the spring modules you need and completely neglect others.

Program to Interface: Spring promotes a good coding practice of programming to an interface rather than classes, which helps produce maintainable code.

Framework Supports: Being a good framework, Spring doesn’t solve the problems which are solved already. It provides consistent supports for various frameworks for data access, transactions, web integration frameworks, etc.

Abstraction: Spring furnishes good support for other Java frameworks abstractly. This really simplifies the process of changing the implementation on the fly.

Maintainability: Spring promotes POJO based programming model and provides easy management of inter-dependencies between objects, makes the code more maintainable and testable.

Aspect-Oriented Programming (AOP) Support: Spring provides an all-new way of managing common functionality by providing AOP implementation, which would otherwise never possible with the traditional Object Oriented Programming (OOP) model.

3. Spring framework modules

Spring is a modular framework and comprises various independent modules that play around the core framework capabilities.

The Spring framework’s modular characteristic allows to pick up the selected modules that are appropriate to a particular use case instead of pulling all modules in the code. This brings great flexibility and ease in development to adapt what you want to be part of the system.

Spring provides around 20 modules, which can be grouped logically in the following categories.

3.1 Core container

The core of the Spring framework comprises to following modules.

3.1.1 Spring core

It’s the heart of the Spring framework, mainly provides IoC container implementation and dependency management capabilities. The IoC container isolates the configuration of beans and their dependency information from the application code.

3.1.2 Spring beans

In the Spring world, the application objects are called beans. This module provides factory pattern implementation called a bean factory, which is used to create and maintain application objects’ lifecycle (beans).

3.1.3 Spring context

This module is built on top of the solid foundation provided by Spring core and Spring beans modules. It is responsible for loading configuration (XML or Annotation) and then use Spring core and Spring beans modules to provide access to the application objects (beans). This module top-ups some additional features like internationalization, event propagation, provides various implementations of context on the fly, etc.

3.1.4 Spring Expression Language (SpEL)

As an extension of Expression Language (EL) of JSP 2.1 specification, Spring Expression Language (shortly Spring EL or just SpEL) is used to access and manipulates application objects (beans) directly in JSP on the fly. It has various benefits, as follows:

  • Cleaner code: Spring EL allows writing cleaner code by avoiding scriptlets in the JSP and increasing code readability.
  • Property configuration: Allows setting of getters and setters of the bean with ease.
  • Access application object (bean): It allows access to any bean defined in spring’s application context. Also, Spring EL can be used to access any method of the controller directly from JSP.
  • Misc: Spring EL allows various interacting operations like selection, projection aggregation, etc. It also allows a various operator to performs arithmetical operations.

3.2 Data access

Spring modules fall under the data access category are mainly used for data manipulation. It comprises to following modules.

3.2.1 Java Data-Base Connectivity(JDBC)

This module is responsible for providing a low-level code to deal with JDBC abstraction. It is used to interact with a database with standard JDBC API.

3.2.2 Object Relationship Mapping (ORM)

Instead of providing its own ORM, Spring supports integration with various ORM frameworks like Hibernate, JPA, JDO, etc., with this module. This brings consistency and portability to application code regardless of various data access technologies.

3.2.3 Transaction

Spring manages the database transactions of type programmatic and declarative with this module. For this purpose, this module closely works with ORM and JDBC modules. This module manages all enterprise-level transactions.

3.3 Web

Spring provides a set of modules that are used mainly to build web-based applications as follows:

3.3.1 Web

As its name suggests, this module facilitates basic web-related integration features like a multipart file upload. Under the hood, it uses a custom tag in JSP for this. It also initializes the Spring IoC container and makes it ready in the web application context.

3.3.2 Servlet

This module contains Model-View-Controller implementation for Spring MVC in a web-based application. Obviously, MVC’s benefits are a clear separation of view layer from the model and controller who control the flow between them. This module provides various custom tags and validations, which can be used in JSP.

3.3.3 Portlet

The Servlet module provides MVC implementation for a servlet-based web application. Similarly, this module provides MVC implementation for portlet based application. It mirrors the functionalities of a servlet-based web module in a portlet context.

3.3.4 Web-socket

This module is responsible for providing two-way communication between client and server with WebSocket implementation in a web application.

3.3.5 Reactive

Spring provides reactive-stack web application support with this module. It’s also known as WebFlux, which was added since Spring 5.0 to support reactive programming.

3.4 Testing

This module is responsible for providing backing support of the unit and integration testing of Spring components with well-known testing frameworks like JUnit, TestNg, etc. Additionally, It helps in consistently loading the Spring application context. Moreover, it assists in providing mock objects used to test the application code in an isolated environment.

3.5 Integration

Spring integration supports a lightweight messaging mechanism to interact with an external system with the declarative approach. It provides a high-level abstraction for supporting remote integration. This extensively helps build a loosely coupled system, especially in an external system, by providing a clear separation of concerns, resulting in maintainable and testable code.

Spring framework provides a set of APIs to deal with various technology by implementing remote services with POJO based model. Currently, Spring supports the integration of the following technologies.

JAX-WS: Spring provides out of the box remote support for web service implemented through JAX-WS.

JMS: Spring provides a set of classes with underlying protocol to interact remote system with JMS.

AMQP: Spring provides an implementation of the AMQP protocol and allows it to interact with high-level abstraction.

RMI: Spring allows exposing custom services through RMI support.

Spring’s HTTP invoke: Spring supports remote service invocation through native java serialization, accessible over HTTP protocol.

3.6 Miscellaneous

Spring provides few miscellaneous features which may help to make the application development with ease and make them more stable to get a steady output as follows:

AOP: Spring provides Aspect-Oriented Programming (AOP) programming model, which helps implement various interceptors. This substantially decouples the common functionalities and allows to embed them in a plug and play fashion.

Aspects: This module helps write the code with AspectJ – a feature-rich, powerful, and mature AOP framework.

Instrumentation: Spring provides classloader implementation through this module. Certain application servers require it.

4. Conclusion

In short, the Spring framework is a straightforwardly simple, lightweight, and robust framework to build modern Java-based applications. It provides lots of functionality in a modular fashion, and you can use those you really need instead of pulling all of them.

The popularity of any robust framework depends on how quickly you are getting help on the internet. Spring is so popular that you will get lots of personal blogs and forums to get help from. In this blog, also you will see detailed tutorials about the Spring framework. The official Spring document provides detailed and up to date information to get what you want.

So keep reading more articles on this blog and the internet to get in-depth about the Spring framework.

 

Recommended For You

About the Author: Nilang

Nilang Patel is a technology evangelist who loves to spread knowledge and helping people in all possible ways. He is an author of two technical books - Java 9 Dependency and Spring 5.0 Projects.

2 Comments to “Introduction to Spring Framework”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.