Java is a widely-used, easy-to-use, and portable programming language. It is used to write complex programs in a simple way. Every programming language has some platforms or tools called frameworks to develop software applications. A software framework can be defined as a platform that helps a programmer build software applications. Frameworks have a set of programs in which coders can add or modify codes as per application needs to develop their desired applications. There are some frameworks used in Java which include the spring framework. It is a platform for Java developers to build applications in a less complex and easy-to-execute way.
What is Spring?
Spring was initially introduced by an Australian IT specialist Rod Johnson in 2003. It became the most popular Java development framework as it provided developers with an easy approach to develop software applications. Spring framework attracts Java developers because it is lightweight (its initial version was approximately 2MB), transparent, and easy to use. Any Java application can be developed by using the Spring framework and applications are easily executed and can be modified as per changing needs. By introducing the Spring framework, Johnson made J2EE development easier for programmers to use. It uses a POJO-based programming model that encourages good programming practices for developers so it is helpful for all Java programmers even for beginners.
Spring Architecture & its modules
Spring is mainly a modular framework and it has around 20 modules that can be used as per developers’ needs. Developers can choose whichever modules they need to use to build software applications. Each module has its own functions so all the modules may not be required for a developer to make an application
There are 4 basic spring modules if we classify them broadly
- Core Container
- Data Integration
- Web Container
a) Core Container:
There are 4 sub-modules in the Core Container module.
- Beans: BeanFactory, a sophisticated & unique implementation of the factory pattern is provided by this module. Beans are considered as the backbone of any Java application and they are controlled by the IoC container. Beans are created with the configuration metadata that is supplied to the Spring IoC container.
- Core: The core module has an important function of providing fundamental parts of this framework that include the Dependency Injection and IoC features.
- Context: The context module is built on the base provided by the Beans and Core modules. It serves as a medium to access defined and configured objects.
- SpEL: This module provides a powerful expression language used for querying and manipulating object graphs.
b) Data Integration
There are 5 modules present in the Data Integration layer.
- JDBC module: The JDBC module removes the need for tedious JDBC-related coding by providing a JDBC-abstraction layer.
- lORM module: Integration layers for JPA, iBatis, Hibernate, and JDO which are popular object-relational mapping APIs are provided by the ORM module.
- OXM module: An abstraction layer supporting XML mapping implementations for Castor, JAXB, XStream, JiBX, and XML Beans.
- JMS module: It is an acronym for Java Messaging Service. This module helps users produce and consume messages.
- Transaction module: Declarative and programmatic transaction management for classes (that implement special interfaces) is supported by the transaction module.
c) Web Container
There are 4 modules in this layer.
- Web module: The web module supports basic web-oriented integration features like the initialization of the IoC container using Web-Servlet listeners and a web-oriented application context.
- Servlet module: It is also known as the Web-MVC module because it supports MVC implementation for web applications.
- WebSocket module: This module gives support for two-way communication & WebSocket-based communication between the server and the client in web applications.
- Web-Portlet module: This module mirrors the functionality of the Web-Servlet module and it also provides the MVC implementation to be used in a portlet environment.
This module or layer has other essential modules such as AOP, Web and Test modules, Aspects & Instrumentation.
- AOP module: This module helps us decouple codes by supporting aspect-oriented programming implementation in which we can use Pointcuts, Advices, etc.
- Messaging module: It gives support for STOMP as a WebSocket sub-protocol to use in software applications. It does support annotation programming for routing as well.
- Test module: This module supports the testing of components in the Spring framework with TestNG or Junit frameworks.
- Instrumentation module: Certain application servers need class instrumentation support and classloader implementations that are provided by this module.
- Aspects module: It helps and supports integration with the AspectJ framework
Related Article: What is Bean in Java Spring
Spring MVC flow
Spring MVC flow is used to design web applications and is a Java framework that follows the Model-View-Controller design pattern. It has all features of a core spring framework such as IoC and Dependency Injection.
A Spring MVC runs with the help of DispatcherServlet as it receives incoming requests and it maps them to the right destination such as models, view, and controllers.
- All incoming requests are intercepted by the DispatcherServlet (working as the front controller). It manages the flow of the Spring MVC application.
- The XML file gives an entry of handler mapping to the DispatcherServlet, and that transmits the request to the controller.
- After getting the request, the controller gives an object of ModelandView.
- This entry of the ViewResolver within the XML file is checked by the DispatcherServlet, and after that, which invokes the defined view component.
Benefits of Spring framework
Although there are many advantages of using the Spring framework, some are listed below.
- Lightweight: The spring framework is lightweight and requires less space.
- It comes with existing technologies like J2EE, ORM framework, etc which reduces the effort to explicitly integrate those technologies.
- The creation of the singleton and factory classes can be eliminated by using Spring.
- Developers can develop enterprise applications by using Plain Old Java Object (POJOs) because developers can use robust servlet containers instead of enterprise containers like application servers.
- As it has a lightweight container, it can be activated without using a web server or application server.
- It gives support for managing business objects and it also supports XML and annotation configurations.
- It can be used to develop different kinds of applications like applets, standalone applications, web applications, etc.
- It provides essential middleware services like transaction management, connecting pools, etc.
- To simplify the development process, it provides an abstraction layer on technologies like RMI, JMS, Servlets, etc.
- The Spring web framework can be used as an alternative to the legacy web framework as it has a well-designed web MVC framework.