spring

What is Spring Framework

In this article, we are going to explain what is Java Spring framework. We are going to learn about architecture, core features, different modules, and feature additions from Spring 5.

The Spring Framework is an open-source, lightweight application framework and Inversion of Control container for the 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, the most recent being Spring 5.

1. What is 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.

The 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 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 can be very helpful in handling cross-cutting concerns like logging, transactions, security, auditing, caching…etc.

1.3. Spring Environment

Spring provides the environment information where the application is running by exposing an Environment interface. It provides two very crucial information for any application, namely profiles and properties.

1.3.1 Profile:

A profile is the logical segregation of the beans and their related properties. Moreover, this information will be available only if the profile is active. An application can have several profiles for their different environments or uses, and they can activate the profiles based on the requirement. A profile can be configured either through XML configuration or using java annotations.

1.3.2 Properties:

Properties are configuration which we set or externalise in an application. We pass different values to the properties and maintain different property files based on our requirements. For example, we can have two databases, one for test data and one for actual data, so whenever we want to do some test, we can assign the test properties and vice versa

1.4 Differences with Struts

Spring and struts are among the most popular frameworks for Java. Both are different in the way they work and are their uses. Let us discuss the differences between them.

Spring Struts
Spring is an application framework comprises of several different capabilities and being used in all the tiers of an Java applicationStruts is a MVC framework which is basically used to build front-end web applications based on Java
Spring uses Dependency injection and inversion of controlStruts uses Java servlet API and build upon the MVC model
Spring is very lightweight framework and just need very few dependencies.Struts is very heavyweight and contains a large number of dependencies
Integration with other frameworks are very easy like hibernate and provides wrapper for most of the popular ORM, APIs.Integration is very difficult and need a lot of configurations
It is very loosely coupled because of dependency injection and Inversion of controlIt is tightly coupled
Difference between Spring and Struts

2. 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.

What is Spring - Architecture
Spring Framework Architecture

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.5.1 Difference between Spring AOP and AspectJ

Spring AOP AspectJ
It provides high level abstraction with simple implementation of Aspect Oriented programmingIt has complete implementation Aspect Oriented Programming
Implemented using spring beansCan be implemented using any Java classes/ domain objects
Pointcuts aspect can be applied only at method levelCan be applied at all levels

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 Framework 5.
  • Spring Core revamp – Spring Core has been updated to make use of Java 8’s new features. @Nullable and @NonNull annotations 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 GenericApplicationContext and AnnotationConfigApplicationContext.
  • 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. TestContext the framework 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 beans.factory.acceess, jdbc.support.nativejdbc, mock.staticmocks, web.views.tiles2M, orm.hibernate3 and orm.hibernate4 have 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 Framework.

5. Field Injection vs Constructor Injection

Both the constructor and Field Injections are used alternatively in spring to inject dependencies between the objects.

Field Injections are used to inject the dependency directly while declaring the field and it is the simplest way to inject a dependency

While constructor injection is used to inject the dependencies while defining the constructor

Constructor injection is the considered to be safe and better way to inject dependencies as it will create the object only if the dependencies are available.

6. XML Vs Annotation based configuration

Spring started with XML configurations file, and then came the annotations to make life easier. Now, most of the applications have both XML and annotations-based Java configuration, which works very well for small and large applications.

XML is useful. Sometimes, it becomes huge when we have many beans configurations to define in the XML file. While Java configurations are good with annotation, but we need to compile whenever we make any changes. If our applications need many configuration changes from time to time, go with XML, and there is no need to compile our code again and again whenever there are changes. On the other hand, if we think our configuration is stable or non-changing, opt for Java annotations based configuration.

7. Summary

In this article, we discussed frameworks, their features, and various modules(like core, web, aop, etc). We discussed its architecture, environment, dependency injection, and inversion of control. We also learned the differences of spring with a framework like struts.

If you want to learn more visit our tutorials here.

Last updated on Oct. 29th, 2020

Santosh Balgar

He is a Software Engineer working in an industry-leading organization. He has completed his bachelors from Visweswaraya Technological University. In his career, he has worked in designing and implementing various software systems involving Java/J2EE, Spring/ Spring Boot, React JS, JQuery, Hibernate and related database technologies. He loves to share his knowledge and always look forward to learning and explore new technologies. He loves to spend his free time with his family. He enjoys traveling and loves to play cricket.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button