Modules of Spring Framework

Modules of Spring Framework

In this article, I am going to discuss the Modules of Spring Framework. Please read our previous article, where we discussed the basics of Spring Framework.

Modules of Spring Framework

Spring framework is a complete and modular framework. It means, we can develop either all modules of the application using the spring framework or we can use the spring framework for some modules of the application. Spring is a lightweight, non-invasive, and a loosely-coupled framework for building java projects. Spring is a very popular framework, for the following reasons.

  1. It is a lightweight framework.
  2. It is a loosely coupled framework.
  3. It reduces boilerplate code.
  4. It is easily testable, because of its own container.

Boiler-plate code is nothing but it is repetitive code. The framework has reduced the repetitive code with help of template classes. If we consider the technology by SERVLET, JSP, or EJB then for testing them a third-party provided container is needed. But in the case of the spring framework, the framework itself provided a container and it will be automatically started and automatically stopped with our application.

The Spring Framework consists of features organized into about 20 modules. These modules are grouped into Core Container, Data Access/Integration, Web, AOP (Aspect Oriented Programming), Instrumentation, and Test, as shown in the following diagram. The Spring framework comprises of core, beans, context, expression language, AOP, Aspects, Instrumentation, JDBC, ORM, OXM, JMS, Transaction, Web, Servlet, Struts, etc. These modules are grouped into Test, Core Container, AOP, Aspects, Instrumentation, Data Access / Integration, Web (MVC / Remoting) as displayed in the following diagram. In spring 3.x, the modules are divided like the following.

Modules of Spring Framework

Spring Core Module:

This core module is the fundamental module of the framework. This core module tells about how to configure spring beans, their dependencies in the XML file. This core module also provides a Spring Inversion of Control (IOC) container (Spring Container).

  1. Spring Core Module: The spring core module of the spring framework is the core module of the spring framework. This module is responsible for the implementation of features like IoC (inversion of control) and dependency injection with a singleton pattern.
  2. Spring Bean Module: This Module is responsible for providing the implementation of the factory design pattern through the Bean factory.
  3. Spring Context: This module is a medium for accessing any object defined and configured. This module has been built on the base provided by the core module and Bean module.
  4. Spring Expression Language (SpEL): The SpEL module is an extension for expressing language support by Java Server pages.
AOP Module (Aspect Oriented Programming):

AOP language is a powerful tool that allows Developers to add enterprise functionality to Applications like Transactions, Security, etc. While implementing business logic in a project, to make business logic as efficient, we attach some services to the business logic. If the same services are added to multiple business methods, then we will get redundancy code in the project. To make cross-cutting concerns (services) re-usable for multiple business methods, we separate the services from the business. AOP is a mechanism, which adds services to business at runtime.

Before applying the AOP:

Before Applying AOP Module (Aspect Oriented Programming)

After applying AOP:

After Applying AOP Module (Aspect Oriented Programming)

Data Access & Integration module:

This module is an abstraction layer on top of JDBC technology and ORM tools. This module makes database access simple by avoiding boiler-plate code. Boiler-plate code is a repetitive code. This module of the Spring framework provides some template classes, for reducing boilerplate code problems and also memory leakage problems.

  1. JDBC: The JDBC module of the spring framework is providing an abstraction layer that eliminates the need for repetitive and unnecessary exception handling overhead.
  2. ORM: ORM stands for object relation aping which provides consistency/portability to our code regardless of the data access technology used.
  3. OXM: OXM stands for Object XML Mappers which is used to convert Objects to XML ad vice versa.
  4. JMS: JMS stands for Java messaging Service which provides the features for producing and consuming messages among various clients.
  5. Transaction: The Transaction module provides support for all the transaction implementation concepts.
Web and remoting Module:

This module is for distributed application development using the spring framework. Spring web allows spring developers to integrate Struts with Spring framework or it allows the developer to write MVC application also with Spring framework. Spring remoting tells about how to invoke the services of other beans which are running at some other JVM across the network.

  1. Web: The web module is using the Servlet listeners and a web-oriented application context.
  2. Web Servlet: This module contains MVC-based implementation for web Apps.
  3. Web Portlet: This module of the spring framework is also called as Spring-MVC-Portlet which is responsible for providing the support for spring-based portlets.
Test Module:

This module is newly introduced in Spring 3.x. This test module prepares mock objects needed for developing test cases. In real-time, this spring test module is not used. Instead, some third-party tools like Easymock, Mockito, TestNG, etc are used for creating mock objects. This module supports the testing of Spring components with JUnit or TestNG. It provides consistent loading of Spring Application Contexts and caching of those contexts. It also provides mock objects that we can use to test our code in isolation.

Spring Core Module:

POJO Class (Plain Old Java Object)

POJO stands for Plain Old Java Object. It is an ordinary Java object, not bound by any special restriction other than those forced by the Java Language Specification and not requiring any classpath. POJOs are used for increasing the readability and re-usability of a program. POJO is an object which encapsulates Business Logic. The following image shows a working example of the POJO class. Controllers interact with your business logic which in turn interacts with POJO to access the database. In this example, a database entity is represented by POJO. This POJO has the same members as the database entity.

Why We Should Learn Spring Framework?

POJOs have gained the most acceptance because they are easy to write and understand. They were introduced in EJB 3.0 by Sun microsystems.

Example1:
class MyClass implements Serilizable {}
Serializable is a marker interface from java.lang package. So, my class is a POJO.

Example2:
class MyClass extends GenericServlet {}
GenericServlet is an abstract class of Servlet API. So MyClass is not a POJO Class.

Example3:
class public class MyClass extends Exception {}
The exception is a class of java API. So MyClass is a POJO

Example4:
class MyClass { void process(ServletRequest request {} }
Here my class is a POJO class because the class is not binded to the servlet technology.

Example5:
public class MyClass extends org.apache.struts.action.ActionForm {}
Here MyClass is not a POJO class, because it is binded with the struts framework.

A POJO should not:

  1. Extend prespecified classes, Ex: public class GFG extends javax.servlet.http.HttpServlet { … } is not a POJO class.
  2. Implement prespecified interfaces, Example: public class Bar implements javax.ejb.EntityBean { … } is not a POJO class.
  3. Contain prespecified annotations, Example: @javax.persistence.Entity public class Baz { … } is not a POJO class.
Can we call a JAVA class with annotations as a POJO class or not?

After avoiding annotations, if a java class is acting as a POJO then that java class with annotation is also a POJO class.

Example1:
@Entity public class Student { @Id private int studentID; … }
This is a POJO class

Example2:
@WebServlet public class MyClass extend HttpServlet {}
My class is not a POJO class.

POJI

POJI stands for Plain Old Java Interface. A POJI is an ordinary interface without any specialties. The interfaces that do not extend from technology/framework-specific interfaces. If a java interface is not coupled with any technology (or) any framework then such a java interface is called “POJI” (plain old java interface). POJI is an acronym for Plain Old Java Interface which corresponds to a Standard Java Interface. In Other Words, we can say that a POJI is an Ordinary Interface without any specialty that is not inherited from any of the Technology or framework interfaces.

JAVA Bean:

The Java Bean is an object that forms the backbone of an application and which are managed by the IoC container. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. a bean is one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.

So, in simple words, we can say that Re-usable classes in java are termed beans. A class is called a java bean if it follows the below rules.

  1. Class must be public.
  2. Class must contain default constructer.
  3. Each private property should contain a setter or a getter or both methods.
  4. The class should implement the Serializable interface, no other interface.

Note: Every java bean is a POJO class, but every POJO class is not a java bean

Modules of Spring Framework

Spring Bean:

Spring Bean is the key concept or backbone of the Spring Framework. Spring Bean is the object whose life-cycle managed by the Spring IoC. It is important to understand it before we work with the Spring Framework. In simple words, Spring Bean is the core building block for any Spring application. In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application.

Every java class which is a part of a spring application is called a Spring Bean. A spring bean does not have any special rules. In the spring framework, we call either a user-defined class or a pre-defined class as spring beans. If a spring bean class is public and has a default constructer then it will become a java bean.

The difference between a spring bean and a java bean is, a java bean must be public but a spring bean may not be public and a java bean must contain a default constructer but a spring bean may not contain a default constructer.

Modules of Spring Framework

Tight Coupling and Loose Coupling between Objects:

While developing applications, it contains many classes and one class calls another class, to provide a service to the clients. In the spring framework, if one class calls another class then we call it bean collaboration. This bean collaboration is also called bean dependency. For example, if class A calls services of class B and class C then we call class B and class C are dependencies of A.

In java, a class can get its dependencies by using one of the following 4 ways.

  1. A class can directly create its dependency object with the new keyword.
  2. A class can get its dependencies by calling a factory method.
  3. A class can collect its dependency object from external memory like a registry.
  4. Some external person (entity) can inject class to object into class1.

When one object depends on another and if modifications done on dependency object is demanding the modifications in a caller object also then it is said to be tight-coupling between objects. For example, consider the following 2 classes.

Tight Coupling and Loose Coupling between Objects

In the above example, if a method in Car class is changed from the move() to go() then, in Travel class also needs to change in startJourney() method. It means if dependencies object i.e. Car is changed then caller i.e. Travel also need changes. It is a tight coupling between objects.

In the above example, suppose if Travel class want another class, says Bike then we need to modify the travel class by replacing Car with Bike. This is also tight coupling.

In order to avoid tight-coupling between objects, we should follow the below principles.

  1. Use the POJI-POJO model for creating dependencies.
  2. Apply dependency-injection mechanism.

For example, create an interface Vehicle and define implementation classes like Car, Bike, and Flight. The importance of this model is, method names in multiple classes will be the same and a class cannot change the method name. In Travel Class, take a reference of type Vehicle interface, so that it can store any implantation class object of that interface.

interface Vehicle
{
    void move();
}
class Car implements Vehicle
{
    public void move()
    {
    }
}
class Bike implements Vehicle
{
    public void move()
    {
    }
}
class Flight implements Vehicle
{
    public void move()
    {
    }
}
class Travel
{
    private Vehicle vehicle;
    //setter method
    public void setVehilce(Vehicle vehicle)
    {
        this.vehicle = vehicle;
    }
    void startJourney()
    {
        vehicle.move();
    }
}

A container injects one vehicle object needed from the Travel class. So, there is no need to make any changes to the Travel class. If the container injects the dependencies required for a class, then it is called dependencies injection. For example, every servlet object needs two dependencies request and response. A servlet container injects the dependencies into the servlet object. So, it is also called dependency injection.

Types of Dependencies Injection:
  1. Setter injection: If the dependencies are injected through setter methods defined in a caller class by container, then it is called setter injection.
  2. Constructor injection: If the dependencies are injected through constructer (s) of caller class by container then it is called constructer injection.
  3. Interface injection: If the dependencies are injected by calling the interface methods by container, then it is called interface injection.

In the spring framework, interface injection is only possible a few cases during the life cycle of a bean. So, we say that the spring framework doesn’t support interface injection. A spring container identifies whether a setter injection is needed or constructer injection is needed through a spring configuration file (XML file). Spring configuration means defining the spring beans and their dependencies into an XML file.

A spring configuration file will act as a mediator between a spring programmer and a spring container. In a spring configuration, a spring configuration file can be named as .xml

Component Class

Spring Component annotation is used to denote a class as a Component. It means that the Spring framework will autodetect these classes for dependency injection when annotation-based configuration and classpath scanning is used. The component is an annotation that allows Spring to automatically detect our custom beans. In other words, without having to write any explicit code, Spring will: Scan our application for classes annotated with @Component. Instantiate them and inject any specified dependencies into them.

In the next article, I am going to discuss the Role of MVC in the Spring Framework. Here, in this article, I try to explain the Modules of Spring Framework and I hope you enjoy this article.

Leave a Reply

Your email address will not be published. Required fields are marked *