Introduction to Spring
By: Bikash Dubey
Tags: Blog, Dependency Injection, Spring

In this article, we will discuss the spring. What is spring and Why we use spring and why it is so popular?

Why Spring framework is so popular?

Long back when java developers want to develop web applications then they were using JavaBeans. Like Java applets, JavaBeans components (or “Beans”) can be used to give World Wide Web pages (or other applications) interactive capabilities such as computing interest rates or varying page content based on user or browser characteristics. Java developers needed to use JavaBeans to create Web applications. Although JavaBeans helped in the development of user interface (UI) components, they were not able to provide services, such as transaction management and security, which were required for developing robust and secure enterprise applications.

After that EJB came into the existence. An EJB web container provides a runtime environment for web-related software components, including computer security, Java servlet lifecycle management, transaction processing, and other web services. The advent of EJB was seen as a solution to this problem EJB extends the Java components, such as Web and enterprise components, and provides services that help in enterprise application development. However, developing an enterprise application with EJB was not easy, as the developer needed to perform various tasks, such as creating Home and Remote interfaces and implementing lifecycle callback methods which lead to the complexity of providing code for EJBs Due to this complication, developers started looking for an easier way to develop enterprise applications.

The Spring framework has emerged as a solution to all these complications. This framework uses various new techniques such as Aspect-Oriented Programming (AOP), Plain Old Java Object (POJO), and dependency injection (DI), to develop enterprise applications, thereby removing the complexities involved while developing enterprise applications using EJB, Spring is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made the development of Web applications much easier as compared to classic Java frameworks and Application Programming Interfaces (APIs), such as Java database connectivity(JDBC), JavaServer Pages(JSP), and Java Servlet.

What is the difference between library and framework?

Library: It is a collection of predefined code written by other developers that we can use in our code to solve specific problems.

Framework: It is a collection of Libraries.

But the major difference is IOC i.e. we call library but the framework calls our code. When we are using any library then we have minimum to no rules and we are in control. But when we use the framework then we have to follow some rules and regulations defined by the framework to use in our application. The framework is nothing but a skeleton where we can add our own code.

what is spring framework?

Spring is a dependency injection framework that helps to build loosely coupled java applications. The Spring Framework is a Java platform that provides comprehensive infrastructure support for developing Java applications. Spring handles the infrastructure so you can focus on your application. Infrastructure support includes transaction management, security, managing connections, creation of objects, and maintaining the life cycle of objects, etc.

What is Coupling?

Coupling refers to the usage of an object by another object. It can also be termed as collaboration. This dependency of one object on another object to get some task done can be classified into the following two types:

Tight coupling — When the creation of one object is dependent on the creation of other objects.

class Subject {

Topic t = new Topic();

public void startReading()

{

t.understand();

}

}

class Topic {

public void understand()

{

System.out.println("Tight coupling concept");

}

}

//Explanation :In the above program the Subject class is dependents on Topic class. In the above program Subject class is tightly coupled with Topic class it means if any change in the Topic class requires Subject class to change.

2. Loose coupling — When the creation of one object is not dependent on the creation of other objects.

public interface Topic

{

void understand();

}

class Topic1 implements Topic {

public void understand()

{

System.out.println("Got it");

}

} class Topic2 implements Topic {

public void understand()

{

System.out.println("understand");

}

} public class Subject {

public static void main(String[] args)

{

Topic t = new Topic1();

t.understand();

}

}

//Explanation : In the above example, Topic1 and Topic2 objects are loosely coupled. It means Topic is an interface and we can inject any of the implemented classes at run time and we can provide service to the end user.

Which is the better tight coupling or loose coupling?

In general, Tight Coupling is bad, because it reduces flexibility and re-usability of code, it makes changes much more difficult, it impedes testability, etc. loose coupling is a better choice because A loosely coupled will help you when your application needs to change or grow. If you design with loosely coupled architecture, only a few parts of the application should be affected when requirements change.

Advantages Of Loose coupling:

A loosely coupled will help you when your application needs to change or grow. If you design with loosely coupled architecture, only a few parts of the application should be affected when requirements change. With too a tight coupled architecture, many parts will need to change and it will be difficult to identify exactly which parts will be affected.

What is Dependency Injection?

Dependency Injection is a fundamental aspect of the Spring framework, through which the Spring container “injects” objects into other objects or “dependencies”.

What is Inversion of control i.e. IOC?

Inversion of control is one of the components of the spring framework which addresses the problem of high coupling by implementing dependency injection design pattern. We can say IOC is a spring container that is responsible to create objects by parsing the configuration metadata provided by the developers. Developers no need to create or manage the object life cycle.

Inversion of Control is a principle in software engineering by which the control of objects or portions of a program is transferred to a container or framework. It’s most often used in the context of object-oriented programming. In the Spring framework, the IoC container is represented by the interface ApplicationContext. The Spring container is responsible for instantiating, configuring, and assembling objects known as beans, as well as managing their lifecycle.

What is Bean in Spring?

A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. It will be instantiated when the getBean method is called for the first time on any interface which represents Spring IOC container.

Dependency Injection:

Dependency Injection is a design pattern by implementing that we can make our java application loosely coupled. Dependency Injection (DI) is a design pattern used to implement IoC.

Dependency Injection is a process where objects define their dependencies. The container then injects those dependencies when it creates the bean.

Ways through which we can implement dependency injection:

1. Constructor based dependency injection

In the case of constructor-based dependency injection, the container will invoke a constructor with arguments each representing a dependency we want to set. Spring resolves each argument primarily by type, followed by name of the attribute and index for disambiguation.

2. Setter based dependency injection

Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean. EX:- We have an Employee which has an Address, here the Employee class is dependent on the Address class. That's why if we are performing setter based dependency injection for Employee class bean then we have to provide a no-arg constructor.

Constructor-based or setter-based DI?

1. Difference between setter vs constructor injection in Spring and one of the drawbacks of setter injection is that it does not ensures dependency Injection. You can not guarantee that a certain dependency is injected or not, which means you may have an object with incomplete dependency. On the other hand, constructor Injection does not allow you to construct an object until your dependencies are ready.

2. One more drawback of setter Injection is Security. By using setter injection, you can override certain dependency which is not possible with constructor injection because every time you call the constructor, a new object is gets created.

3. If Object A and B are dependent on each other i.e A is dependent on B and vice-versa. Spring throws ObjectCurrentlyInCreationException while creating objects of A and B because A object cannot be created until B is created and vice-versa. So spring can resolve circular dependencies through setter-injection. Objects constructed before setter methods invoked.

We usually advise people to use constructor injection for all mandatory collaborators and setter injection for all other properties. Again, constructor injection ensures all mandatory properties have been satisfied, and it is simply not possible to instantiate an object in an invalid state (not having passed its collaborators). In other words, when using constructor injection you do not have to use a dedicated mechanism to ensure required properties are set (other than normal Java mechanisms).

With this, we come to an end to this blog. I hope you got a clear understanding of the Spring Framework Basics from this article.