Now, we are going to explore some concepts that lie as the ❤️ of Spring.

Dependency Injection (DI)

The Inversion of Control (IoC) is a design principle in which the flow of control of a system is inverted. It’s a general concept, where the dependency injection is one concrete example of it.

💡 The Spring IoC Container creates, manages, and injects object’s dependencies.

The dependency injection separates an object from its dependencies, so we can decide which dependencies to be injected during the run time.

💡 This decision is done in Spring in the beans configuration file (or using annotations or java code) instead of modifying the code, as we’ll see later.

And the benefit is, we are able to replace and extend the functionality of the dependencies (services) without changing the dependent (App) class at all.

Aspect Oriented Programming (AOP)

It allows the separation of cross-cutting concerns from the business logic.

💡The cross-cutting concerns are features that span across different parts of the application and affects the entire application. Examples → logging, declarative transactions, security, caching, etc.

📣 But, why we would use it?

What happens if we need to change, for example, a specific security consideration across all the application. Such a change would require a major effort, especially, if this security consideration is used across many methods.

So, AOP attempts to solve this problem by allowing us to express cross-cutting concerns in stand-alone modules called “aspects”.

For example, a security module “aspect” can include “advice” that performs a security check (actual piece of code that will be executed), and a “join point” which defines when (i.e. when the code is executed) in the corresponding program the aspect code should be executed.

This can be done either using XML syntax, or syntax based on “AspectJ”, which uses Java code.

🔦 DI vs AOP → DI helps you decouple your application objects from it’s dependencies, while AOP helps you decouple cross-cutting concerns from the objects.

Spring Architecture 🏠

Spring is organized in a modular fashion which allows you to worry only about the modules you need and ignore the rest.

💡Modular programming is a software design technique that separates the functionality of a program into independent modules, such that each contains one specific functionality. 🔦 Modularity vs AOP → The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect.

It provides about 20 modules. All modules are built on the top of its core container. These modules provide everything that a developer may need for use in the enterprise application development.

Spring Architecture — docs.spring.io

Core Container

It’s used for creating beans and manage beans dependencies.

The Core and Bean modules provide the fundamental parts of the framework, including the IoC and Dependency Injection features.

and modules provide the fundamental parts of the framework, including the IoC and Dependency Injection features. The Bean module provides BeanFactory, which is a sophisticated implementation of the factory pattern. It decouples the configuration and specification of dependencies from the actual program logic.

module provides BeanFactory, which is a sophisticated implementation of the factory pattern. It decouples the configuration and specification of dependencies from the actual program logic. The Context module builds on the Core and Beans modules. It is a means to access any objects defined and configured.

💡The context module extends the BeanFactory and adds support for application lifecycle events, and validation. It enables many enterprise services such as JNDI access, EJB integration, remoting and scheduling, and also facilitates easy integration with the other frameworks.

The SpEL module provides a powerful expression language for querying and manipulating objects during the runtime.

Data Access/Integration Modules

It’s used to communicate with the database.

JDBC : Provides a JDBC-abstraction layer that removes the need for JDBC related coding.

: Provides a JDBC-abstraction layer that removes the need for JDBC related coding. ORM : Provides an integration layers for popular object-relational mapping APIs, including JPA, JDO, Hibernate, and iBatis.

: Provides an integration layers for popular object-relational mapping APIs, including JPA, JDO, Hibernate, and iBatis. OXM : Provides an abstraction layer that supports Object/XML mapping implementations.

: Provides an abstraction layer that supports Object/XML mapping implementations. The Java Messaging Service (JMS) : Contains features for producing and consuming messages (emails).

: Contains features for producing and consuming messages (emails). Transaction: It supports transaction management for classes.

Web Modules

It’s the home of Sring MVC framework. We can integrate other technologies like JSF.

Web : Provides basic web-oriented integration features such as multipart file upload functionality and the initialization of the IoC container using Servlet listeners and a web-oriented application context.

: Provides basic web-oriented integration features such as multipart file upload functionality and the initialization of the IoC container using Servlet listeners and a web-oriented application context. Web-MVC : Contains Spring’s Model-View-Controller (MVC) and REST Web Services implementation for web applications.

: Contains Spring’s Model-View-Controller (MVC) and REST Web Services implementation for web applications. Web-Sockets : Provides support for WebSocket-based, two-way communication between the client and the server in web applications.

: Provides support for WebSocket-based, two-way communication between the client and the server in web applications. Web-Portlet: Provides the MVC implementation to be used in a portlet environment and mirrors the functionality of Web-MVC module.

AOP and Instrumentation

AOP : Provides an aspect-oriented programming implementation allowing you to separate the cross-cutting concerns from the business logic (as explained before).

: Provides an aspect-oriented programming implementation allowing you to separate the cross-cutting concerns from the business logic (as explained before). Aspects : It’s a separate module provides integration with AspectJ.

: It’s a separate module provides integration with AspectJ. Instrumentation: Provides support to class instrumentation support and classloader implementations.

Messaging

It serves as a foundation for messaging-based applications.

It brings the features of asynchronous messaging system and provides support for STOMP to be used over WebScokets for users to exchange messages.

Test

It supports the unit testing and integration testing of Spring components with JUnit or TestNG. It also provides mock objects that you can use to test your code in isolation.