Spring Framework in JAVA

Introduction

The Spring framework in Java is one of the most commonly used frameworks in JAVA for writing robust enterprise applications. The fundamentals of the framework were developed by Rod Johnson and released by Apache in 2003. Spring is both a lightweight and open-source framework that is used by thousands of developers around the world to create applications that have high-performance output, can be easily edited, and instantly tested for persistent errors.

What is the Spring Framework in Java?

The Spring Framework in Java can be thought of as a universal set of frameworks that provides a framework for various smaller JAVA frameworks like Struts, EJB, JSF, Hibernate, and many more. It is a tried and tested structure that provides solutions to various application related problems.  Some of the powerful techniques that the Spring framework uses to manage data are Aspect-Oriented Programming (AOP), Plain Old JAVA Object (POJO), and dependency injection (DI).

Inversion of Control (IOC), and Dependency Injection (DI) are two design patterns that form an integral part of the Spring framework in Java. Both these mechanisms are used to reduce coupling (interdependency) from codes. Let us look at a snippet of code with and without the implementation of the aforementioned design patterns.

Without IOC and DI With IOC and DI
class Student {
ID = id;
Student () {
id = new ID ();
}
}
Class Student {
ID = id;
Student (ID id) {
this.id = id;
}
}

 

By passing the student id through the constructor, a great deal of dependency is reduced and the code is easier to read and re-use. It also prevents logic breakthrough.

Spring Architecture

The Spring Architecture has been broken down in small modules. Depending upon the code’s requirement, a specific module(s) is put to use. The other modules remain passive in such situations.

Core Container

  • The Core container contains four modules starting with the Core module. This is the central body that controls some parts of IOC and DI.
  • The Bean module produces the BeanFactory that implements the Factory design pattern.
  • The Context Module works on the core principles developed by Core and Bean and plays an integral part during object call.
  • The SpEL module acts as a query generator.

Data Access/Integration

  • JDBC – This module establishes connections with the JAVA Database where users do not have to write long lines of code.
  • ORM – This is a mapping module that provides integration for JPA, JDO, and Hibernate.
  • OXM – Uses XML to provide an additional layer of abstraction to the framework.
  • JMS – This is the Java Messaging Service.
  • Transaction – Accounts for data moved to and from the database to an application.

WEB

  • WEB – Provides much-needed web support for applications like multi-part file upload and servlet listeners.
  • Web-MVC – This module contains the Model-View-Controller (MVC) of the framework that assists in creating web applications.
  • Web-Socket – This is a modern two-way conversation technique between clients and the server. Client requests are of utmost priority to developers.
  • Web-Portlet – MVC for portlets and closely works with the Web-Servlet module.

Advantages of the Spring Framework in Java

With the inclusion of IOC and DI, loose coupling can be achieved when using this framework. Additionally, DI makes it even easier to test code. As it is lightweight and open source, it is very approachable to users of all competencies.

Example with Code

Let us design a real-life web-based application that will print out the following line: “Program Successful. Test Passed.”

public class TestProg {
private String text;

public void settext (String text) {
this.text = text;                                  //IOC implemented.
}
public void getText(){
System.out.println(text);
}

//The following class is the application handler

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Handler {
public static void main (String args[]) {
ApplicationContext contenxt = new ClassPathXmlApplicationContext(“Beans.xml”);
TestProg obj = (TestProg) context.getBean(“Program Successful. Test Passed”);
obj.getText();
}
}

Output

Program Successful. Test Passed

Leave a comment

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