The Spring Framework is an open-source, lightweight application framework and Inversion of Control container for Java platform. The first version of the Spring Framework was released in 2003. The increasing popularity of the framework among Java developers kept it evolving, most recent being Spring 5.
In this article, we are going to learn about Spring’s architecture, core features, different modules and feature additions from Spring 5.
1. What is Spring Framework ?
Spring is undoubtedly the most popular Java Enterprise Application Framework. It can even support recent JVM based languages Groovy and Kotin. The spring framework is divided into multiple modules. In this section, we are going to learn about Spring’s core principles Dependency Injection/Inversion of Control and Aspect-Oriented Programming (AOP).
1.1 Dependency Injection/ Inversion of Control
Dependency Injection is a software engineering technique that makes a class independent of its dependencies by abstracting an object’s usage from its creation. Spring’s implementation of Dependency Injection is called Inversion of Control.
In Spring the application objects that are managed by IoC containers are called beans. A bean is an object that is instantiated, assembled and managed by Spring IoC container. The bean dependencies are defined in configuration metadata used by the container.
org.springframework.context.ApplicationContext represents the Spring’s IoC container and is responsible for dependency management. Configuration can be defined either using XML or by one or more Java classes.
1.2 Aspect-Oriented Programming
Aspect-Oriented Programming complements the Object-Oriented Programming by providing another way to structure your programs. AOP is used in spring to provide,
- As a replacement to EJB to provide declarative enterprise services
- Allow users to use custom aspects
AOP is can be very helpful in handling cross-cutting concerns like logging, transactions, security, auditing, caching…etc.
2. Spring Framework Architecture
In this section, we are going to learn about the architectural components of the Spring Framework. The below diagram shows overall Spring architecture and modules that it is composed of.
Various modules mentioned in the above diagram are discussed in the coming sections.
3. Spring Framework Modules
3.1 Spring Core
Spring core provides the fundamental parts of the framework including Dependency Injection and IoC.
BeanFactory is a sophisticated factory pattern implementation that manages the instantiation of various beans.
Context module built on top of Beans and IoC and adds internationalization, event propagation, and resource loading.
Expression Language module provides expression language to querying and manipulating the object graph.
3.2 Spring Web
The web layer consists of Web, web-servlet, web-struts, and web-portlets.
Spring web consists of web-related integration features. Spring MVC based implementations are contained in the web-servlet module. Classic struts support can be realized with the web-struts module. The web-portlet module provides the MVC implementation that can be used in the portlet environment.
3.3 Data Access
Data Access/Integration layer consists of JDBC, ORM, OXM, JMS and Transaction modules.
JDBC module is an abstraction on classic JDBC and it eliminates the need for JDBC boilerplate code.
ORM module provides easy integration with different ORM frameworks like JPA, Hibernate, iBatis, and JDO.
OXM module is an abstraction over Object XML mappings like JAXB, Castor, XMLBeans, and XStream.
JMS module provides facilities to produce and consume messages.
The transaction module provides support for the programmatic and declarative transactions.
3.5 AOP and Portlets
Spring AOP provides aspect-oriented programming implementation to handle cross-cutting concerns. Aspects module provides integration with another popular library AspectJ. Class instrumentation and class loader supports are provided by the instrumentation module.
3.4 Test Module
The test module provides testing support using TestNG and JUnit. It provides full support for ApplicationContext and caching of beans. It can very well support mock objects.
4. Spring 5 Features
Since the inception of Spring Framework, the framework kept improving and kept on adding new features. Currently, Spring has become a de facto choice for Enterprise Java applications. The latest version of Spring 5.2.2 (Spring 5 was released first in September 2017) is released in December 2019. Spring 5 has brought in massive improvements as compared to earlier Spring 4 releases. Below are some of the important feature additions in version 5 or later,
- JDK baseline update – Spring 5 is developed on Java 8. Hence, Java 8 is the minimum requirement to run Spring Framework 5.
- Spring Core revamp – Spring Core has been updated to make use of Java 8’s new features.
@NonNullannotations are introduced to mark nullable and non-nullable arguments and return values. Spring 5 can support default methods as well.
- Updates to Spring Container – Spring 5 supports the candidate component index as an alternative to classpath scanning. An application build task can be created in
META-INF/spring.components. Reading entities from the index are quite performant in large applications. Spring 5 introduced the functional programming style as well as part of
- Kotlin Support – Kotlin is an object-oriented programming language with functional programming support. It runs on JVM but not limited to it. With Kottlin support, developers can write functional programs in Spring.
- Reactive Programming – Spring Framework 5 fully supports reactive programming, this can support event loop style processing. Spring’ reactive programming support is built upon Project Reactor.
- Testing Improvements – It fully supports JUnit 5 Jupiter version. Jupiter sub-project provides an engine to run JUnit 5 tests in Spring.
TestContextframework allows us to have parallel execution of tests.
- Library Support – Many libraries that Spring support, their versions have been upgraded. For example, Hibernate 5.0+, JDBC 4.0+, Jackson 2.6+ and so on.
- Discontinued Support – With Spring 5 the packages like
orm.hibernate4have been discontinued. In addition, libraries like Portlets, velocity, JasperReports, XMLBeans, JDO, and Guava have been discontinued.
With this, we have reached the end of this article. I hope you have got a sneak peek into the Spring Framework.