An overview of the

Spring is the most popular enterprise-class Java application development framework, used by millions of developers around the world to create high-performance, easy-to-test, and reusable code.

The Spring Framework is an open source Java platform originally written by Rod Johnson and first released under the Apache 2.0 license in June 2003.

Spring is a lightweight framework, and the base version is only about 2 MB in size.

The core features of the Spring framework can be used to develop any Java application, but building Web applications on the Java EE platform requires extensions. The goal of the Spring framework is to make J2EE development easier to use, promoting good programming practices by enabling a POJO-based programming model.

Three layer architecture

  • A Presentation layer Web layer MVC is A design model of the presentation layer

  • B Service layer Service layer

  • C Persistence layer DAO layer

Dependency Injection (DI)

Spring’s favorite technology is the dependency injection (DI) pattern of inversion of control. Inversion of control (IoC) is a general concept that can be expressed in many different ways. Dependency injection is just one concrete example of inversion of control.

When writing a complex Java application, the application classes should be as independent of the other Java classes as possible to increase the likelihood that these classes can be reused and tested independently of other classes when unit testing is performed. Dependency injection (or wiring, as it is sometimes called) helps glue these classes together and keep them separate at the same time.

What exactly is dependency injection? Let’s take a look at these two words separately. Here the dependency part is translated into an association between the two classes. For example, class A depends on class B. Now, let’s look at the second part, injection. All of this means that class B will be injected into class A via IoC.

Dependency injection can occur by passing arguments to constructors, or by using setter methods called post-construction. Since dependency injection is a core part of the Spring framework, I’ll explain the concept in a separate section with good examples.

Aspect Oriented Programming (AOP) :

A key component of the Spring framework is the section-oriented programming (AOP) framework. Functions that span multiple points in a program are known as crosscutting concerns, and these crosscutting concerns are conceptually independent of the application’s business logic. There are a variety of common good examples of aspects such as logging, declarative transactions, security, and caching.

The key unit of modularity in OOP is the class, whereas in AOP the key unit of modularity is the aspect. AOP helps you separate crosscutting concerns from the objects they affect, whereas dependency injection helps you separate your application objects from each other.

AOP module of the Spring framework

For aspect-oriented programming implementation, you can define things like method interceptors and pointcuts to completely decouple the code that implements the functionality. Using source-level metadata, you can incorporate behavioral information into your code in a manner similar to.NET attributes. I’ll discuss more about Spring AOP concepts in a separate section.

architecture

Spring has the potential to be a one-stop shop for all enterprise applications; however, Spring is modular, allowing you to pick and choose which modules work for you without having to import the rest. The following sections provide a detailed overview of all the modules available in the Spring framework.

The Spring framework provides about 20 modules that you can use depending on your application’s requirements.

Core container

The core container consists of spring-core, spring-beans, spring-context, spring-context-support, and spring-expression (SpEL, Spring expression language, Spring Expression Language and other modules, their details are as follows:

  • The Spring-core module provides the basic building blocks of the framework, including IoC and dependency injection capabilities.

  • The Spring-Beans module provides a subtle implementation of the BeanFactory pattern, which removes the need for coded singletons and decouples configuration and dependencies from the actual coding logic.

  • The Context module is built on top of the Core and Beans modules and accesses objects in a way similar to JNDI registration. The Context module inherits from the Bean module and adds features such as internationalization (for example, using resource bundles), event propagation, resource loading, and transparently creating contexts (for example, through a server container). The Context module also supports Java EE features such as EJB, JMX, and remote calls. The ApplicationContext interface is the focus of the Context module. Spring-context-support provides support for third-party integration into spring contexts, Examples include caching (EhCache, Guava, JCache), mail (JavaMail), scheduling (CommonJ, Quartz), template engines (FreeMarker, JasperReports, Velocity), and more.

  • The Spring-Expression module provides a powerful expression language for querying and manipulating object diagrams at run time. It is an extension of the unified expression language defined in the JSP2.1 specification and supports set and GET attribute values, attribute assignment, method calls, accessing the contents of array collections and indexes, logical arithmetic operations, naming variables, retrieving objects from the Spring IoC container by name, and list projection, selection, and aggregation.

Their complete dependencies are shown below:

Data access/integration

The data access/integration layer includes JDBC, ORM, OXM, JMS, and transaction modules, which are detailed as follows:

Note: JDBC=Java Data Base Connectivity, ORM=Object Relational Mapping, OXM=Object XML Mapping, JMS=Java Message Service

  • The JDBC module provides a JDBC abstraction layer, which eliminates verbose JDBC coding and parsing of database vendor-specific error code.

  • The ORM module provides the integration of multiple object relational mapping apis, including JPA, JDO, and Hibernate. This module allows you to integrate these ORM frameworks with other Spring features, such as the previously mentioned transaction management.

  • OXM modules provide support for OXM implementations such as JAXB, Castor, XML Beans, JiBX, XStream, and more.

  • The JMS module contains the ability to produce and consume messages. Since Spring 4.1, the Spring-Messaging module has been integrated.

  • The transaction module supports programmatic and declarative transaction management for implementing special interface classes and all POJOs. (Note: Programmatic transactions need to write their own beginTransaction(), COMMIT (), rollback() and other transaction management methods. Declarative transactions are automatically processed by Spring through annotations or configuration. Programmatic transactions are more granular.)

The main benefits of the Spring framework are as follows:

1) Convenient decoupling and simplified development

Spring is a big factory that leaves the creation of all objects and maintenance of dependencies to Spring.

2) Convenient integration of various excellent frameworks

Spring does not exclude excellent open source frameworks, and it provides direct support for excellent frameworks such as Struts2, Hibernate, MyBatis, etc.

3) Make Java EE apis easier to use

Spring provides a wrapper around some of the most difficult apis in Java EE development (JDBC, JavaMail, remote calls, etc.), making them much easier to use.

4) Facilitate the test of the program

Spring supports JUnit4, which makes it easy to test Spring programs with annotations.

5) AOP programming support

Spring provides section-oriented programming, which makes it easy to implement functions such as permission interception and running monitoring for programs.

6) Support for declarative transactions

Transactions can be managed through configuration rather than manual programming.