Introduction to Spring Framework
By: Neeraj Takola
Tags: Spring, Dependency Injection, Inversion Of Control

Library Vs Framework?

Do you know the difference between a library and a framework? Are they just lots of reusable code for helping the other developers? Is there really any difference between the two? Let’s look at the difference between the two.

A library is a collection of classes, objects, abstractions, and helper functions that are imported into our application so that a particular functionality can be achieved by it. It is basically a pre-written code that can be reused later by other developers. Suppose, there is already a function written that parses the input files and returns the no of vowels present in it. We just have to include the library it is part of, and call the function with input being provided.

A framework provides a platform for developers to create applications. Basically, it is an abstraction that provides generic functionality. It means that all the developers get the same platform when they develop their applications, but when they include their additional code, we get our application specific software. Some popular frameworks in use today are Angular, React, Spring, Laravel, and many more.

What makes frameworks distinguish from libraries? The flow of control. When a particular function from a library is called, the flow of control is in the developer’s hands. A framework, on the other hand, inverts this control which means the framework is the one making calls to our code.

What makes the Spring framework so popular?

Spring is one of the most popular J2EE frameworks. It was designed by Rod Johnson. The key feature of the Spring framework is the IoC( Inversion of Control) container.

Now, the question arises, what is “inversion of control”? We create applications to achieve some goals. Modularity lies in the heart of a project. We separate our project into different modules. Separation of concern is a design principle in which each section is required to address a particular concern. Basically, we create directories, packages, classes, interfaces to achieve this. Now, all these modules, though separated are still connected. If the implementation is bad, the relation between the modules results in the tight coupling, which makes modifications in the application too difficult. Loose coupling, on the other hand, makes it an easier task to make modifications, testing the independent parts easy, and is used for application development.

Inversion of Control.

Inversion of Control is a principle. It helps in achieving loose coupling in the application. An IoC container, when implemented in an application, takes the control of the flow of the application, objects’ creation, and the binding of objects to their dependencies. IoC takes the responsibility of creating the objects at run-time, checks if any object relies on any dependency, and then injects that dependency into the dependent object. This is dependency injection, where one object is injected into the dependent object.

Spring Framework Architecture.

The spring framework provides several modules that we can use to build our application.

Image from journaldev.com/spring-framework

The core components of the spring framework are :

Spring Container: It manages the life-cycle of objects from creation till destruction in the application, checks for the dependencies, and injects the dependencies into dependent objects. The container does all these tasks by reading configuration metadata. This configuration metadata can be in the form of an XML document, annotations, or java code configuration.

Following are the two types of containers provided by spring:

a. BeanFactory: BeanFactory is the interface provided by spring to instantiate, configure, and assemble the objects in the application. The most common implementation of BeanFacoty is XmlBeanFactory.

b. ApplicationContext: It extends the BeanFactory interface and provides several features suitable for enterprise applications. Most common used implementations are ClassPathXmlApplicationContext and FileSystemXmlApplicationContext.

BeanFactory only instantiates an object when the demand comes using a lazy loading strategy. ApplicationContext, on the other hand, uses eager loading and instantiates objects at startup.

2. Beans: Any object initialized through the spring container is a bean. The container instantiates bean by reading configuration either in an XML document, by annotation, or by java code configuration. We can provide @Component or configure the XML document to provide the container with the bean details.

Following are the scopes defined for a spring bean:

a. Singleton: Only one instance of the bean is created. Even if there are multiple references, the same bean is provided by the container.

b. Prototype: For every request, a new instance is created.

c. Web Aware Scopes: These are meant for web applications.

i. request: It is created for every request by the user.

ii. session: It is created only once for the entire session of a user.

iii. global-session: It is created for the whole application.

To learn more about spring click here. Keep learning!