Spring Framework
By: Suyash Thonte
Tags: Spring Framework, Spring, Java Spring

Hey Guys, Of all the seasons in the year, Spring is considered the best season. You guys must be wondering is this a technical blog or a weather blog ??

So basically every normal person likes the Spring. Same is the choice in case of Java Developers. So here I am referring to the best and most preferred Java Framework i.e Java Spring. So let us get started with Spring Core and its concepts.

Why Spring?

In the early, most of the software was created using Java Platform, Enterprise Edition (J2EE) that had many drawbacks i.a. applications were really heavy even though they’re business logic was really small. Also time spent on them was not proportionate to the outcome, because many of configurations had to made by the developer.

Hence Spring was developed to overcome the drawbacks of J2EE. The main goal of Spring framework is to simplify the development of enterprise software so it can be done quicker, because most of boilerplate configuration code are handled by the framework.

Spring makes programming Java quicker, easier, and safer for everybody. Spring’s focus on speed, simplicity, and productivity has made it the world’s most popular Java framework.

What is Spring?

Spring Framework is an open source Java platform that provides comprehensive infrastructure support for developing Java applications. Spring handles the infrastructure so you can focus on your application.Spring enables you to build applications from “plain old Java objects” (POJOs) and to apply enterprise services to POJOs.

For example using Spring a developer can :

Make a Java method execute in a database transaction without having to deal with transaction APIs.

Make a local Java method a remote procedure without having to deal with remote APIs.

Make a local Java method a management operation without having to deal with JMX APIs.

Make a local Java method a message handler without having to deal with JMS APIs.

To achieve all the advantages above, Spring relies on these following concepts:

Modular Architecture Plain Old Java Objects (POJOs) Dependency Injection/Inversion of Control (IoC) Aspect Oriented Programming (AOP)

1. Modular Architecture

The whole idea of Spring is to modularize whole framework into smaller pieces which allows you to decide which ones to use in your application. Also, the functionality of a program is split into independent modules, such that each module contains one specific functionality.

Spring Architecture

A.) Core Container.

The Core container from Spring consists of four modules: SpEL , Context, Core, Beans.

SpEL module provides a powerful expression language for manipulating objects during execution.

module provides a powerful expression language for manipulating objects during execution. Context is built on the basis of Beans and Core and allows you to access any object that is defined in the settings. The key element of the Context module is the ApplicationContext interface.

is built on the basis of Beans and Core and allows you to access any object that is defined in the settings. The key element of the Context module is the ApplicationContext interface. Core module provides key parts of the framework including IoC and DI properties.

module provides key parts of the framework including IoC and DI properties. Bean module is responsible for creating and managing Spring Beans.

B.) Spring Framework Web:

Spring Framework Web layer consists of Web, Web-MVC, Web-Socket, Web-Portlet etc.

WEB module provides functions such as downloading files, creating web application, rest web service etc..

module provides functions such as downloading files, creating web application, rest web service etc.. Web-MVC contains a Spring MVC implementation for web applications.

contains a Spring MVC implementation for web applications. Web-Socket provides support for communication between the client and the server, using Web-Sockets in web applications.

provides support for communication between the client and the server, using Web-Sockets in web applications. Web-Portlet provides MVC implementation with portlet environment.

C.) Spring Framework Data Access:

The Data Access/Integration container consists of JDBC, ORM, OXM, JMS and the Transactions module.

JDBC provides an abstract layer of JDBC and eliminates the need for the developer to manually register the monotonous code associated with connecting to the database.

provides an abstract layer of JDBC and eliminates the need for the developer to manually register the monotonous code associated with connecting to the database. Spring ORM provides integration with such popular ORMs as Hibernate, JDO, JPA, etc.

provides integration with such popular ORMs as Hibernate, JDO, JPA, etc. OXM module is responsible for linking the Object / XML — XMLBeans, JAXB, etc.

module is responsible for linking the Object / XML — XMLBeans, JAXB, etc. JMS (Java Messaging Service) module is responsible for creating, sending and receiving messages.

module is responsible for creating, sending and receiving messages. Transactions supports transaction management for classes that implement certain methods and POJOs.

D.) Test:

Spring supports Test Driven Development by providing mock-up objects for unit testing and also many features that allows to make integration tests.

2. Plain Old Java Objects (POJOs)

POJOs are simple Java Objects that do not contains any dependencies in it. They don’t implement any interface or extend any class as it is required by many frameworks. The benefits of such approach is that a solution to a problem is not directly tied to the framework or it is not directly tied to other solutions, so if wechange some part of the application other part won’t be affected.

public class EmployeePojo {

public String firstName;

public String lastName;

private LocalDate startDate;



public EmployeePojo(String firstName, String lastName, LocalDate startDate) {

this.firstName = firstName;

this.lastName = lastName;

this.startDate = startDate;

}

public String name() {

return this.firstName + " " + this.lastName;

}

public LocalDate getStart() {

return this.startDate;

}

}

This class can be used by any Java program as it’s not tied to any framework.

3. Dependency Injection/Inversion of Control (IoC)

Dependency Injection : It is a design pattern, in which for each function of the application there is one, a conditionally independent object (service) that can have the need to use other objects (dependencies) known to it by interfaces. Dependencies are transferred (implemented) to the service at the time of its creation. In practice, DI is implemented by passing parameters to the constructor or using setters. Libraries that implement this approach are also called IoC containers.

So, what exactly is inversion of control?

IoC is also known as dependency injection (DI). It is a process whereby objects define their dependencies (that is, the other objects they work with) only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse (hence the name, Inversion of Control) of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes or a mechanism such as the Service Locator pattern.

Spring IOC Container : Spring IoC container classes are part of org.springframework.beans and org.springframework.context packages. Spring IoC container provides us different ways to decouple the object dependencies.

BeanFactory is the root interface of Spring IoC container. ApplicationContext is the child interface of BeanFactory interface that provide Spring AOP features, i18n etc.

Spring Bean : Any object in the Spring framework that we initialize through Spring container is called Spring Bean. Any normal Java POJO class can be a Spring Bean if it’s configured to be initialized via container by providing configuration metadata information.

Bean has following scopes :

singleton — Single bean object instance per spring IoC container prototype — A new instance will be created every time the bean is requested. request — This is same as prototype scope, however it’s meant to be used for web applications. A new instance of the bean will be created for each HTTP request. session — A new bean will be created for each HTTP session by the container. global-session — This is used to create global session beans for Portlet applications. websocket — A single instance will be created and available during complete lifecycle of WebSocket.

Also there are three ways to configure Spring Beans :

XML Based Configuration — It is considered as an old-fashioned. It was introduced in first releases of Spring, so you can find it in the old projects, but it is still used. The idea is to have single or multiple XML files that contains a list of Beans (i.e. Java objects) that can be registered into Spring container. If you are using Spring MVC framework, the xml based configuration can be loaded automatically by writing some boiler plate code in web.xml file. Annotation Based Configuration — Using this, Spring will scan for Java classes with special annotations. So in this we only add annotation above class name and the framework will do all the work for us. Following are the annotations :

@Component — it is a standard annotation and can be added to any type of Java class.

— it is a standard annotation and can be added to any type of Java class. @Repository — it is used for Data Access Objects (DAO) that are responsible for retrieving information from the database,

— it is used for Data Access Objects (DAO) that are responsible for retrieving information from the database, @Service — this one is used for service layer classes, they working as a middleman between UI and DAO and responsible for processing data and whole business logic,

— this one is used for service layer classes, they working as a middleman between UI and DAO and responsible for processing data and whole business logic, @Controller — classes with these annotations are taking care of user requests in UI.

Scope details can be provided with @Scope annotation.

Java Based Configuration — Starting from Spring 3.0, we can configure Spring beans using java programs. Some important annotations used for java based configuration are :

@Configuration : This tells Spring that it is a config file.

@ComponentScan :This tells Spring that the User bean will be created after the Address, not before.

@Bean.

4. Aspect Oriented Programming (AOP)

Aspect-oriented Programming (AOP) complements Object-oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns (such as transaction management) that cut across multiple types and objects. (Such concerns are often termed “crosscutting” concerns in AOP literature). So in short, It allows the separation of cross-cutting concerns from the business logic. By “cross-cutting concerns” we mean features that span across different parts of the application and tend to affect the entire application like logging, security, caching etc.

📣 But, why exactly would be face such requirements?

Let’s say you want to make a specific security consideration in the application. If the security consideration is used across many methods, then this changed is gonna be a huge one. That’s when AOP could save us.

AOP can solve this problem by allowing us to express cross-cutting concerns in stand-alone modules i.e the aspects.

So in short, AOP is used in the Spring Framework to:

Provide declarative enterprise services. The most important such service is declarative transaction management.

Let users implement custom aspects, complementing their use of OOP with AOP.

Some Extra Modules of Spring:

Spring also includes a number of other important modules, such as Aspects, Instrumentation, Messaging, and Test.

The Aspects module provides integration with AspectJ, which is also a powerful AOP framework.

Instrumentation is responsible for supporting class instrumentation and class loader, which are used in server applications.

The Messaging module provides STOMP support.

Finally, the Test module provides testing using TestNG or the JUnit Framework.

So that was all about Spring Framework and its concepts!

See you Guys in the next Blog!!!