Spring  Inversion of control & Dependency Injection

Spring Inversion of control & Dependency Injection

What is Spring?

"Spring is an open-source lightweight framework that allows Java developers to build simple, reliable, and scalable enterprise applications".

At its core spring is an implementation of Invasion of Control , a popular enterprise design pattern. (Inversion of Control answers the question: "How will I connect my application components with each other ?" )

Inversion of Control Inversion of Control is a software design pattern, where you tell a framework what components need to exist, how to create them, and which components are needed by other components to function, then when you run the application the IoC framework automatically initializes the components and connects them, procedurally determining the best order and organization in which to do so. This allows you to focus on your main work, designing robots and powerful components and leave the connections between them to the framework, and changes to your design would be automatically factored in.

In a layman's term, assuming your drive to work every morning, you control the car to work. Here you can invert control by hiring a cab driver that would drive you to work every morning. You must not do the driving yourself.
aid5857281-v4-728px-Hail-a-Cab-in-New-York-Step-4.jpg.jpeg Thus, Instead of driving the car yourself, you gave control to the driver. This is inversion of control. This would allow you to focus on other important stuff you have for the day.

Most applications can be thought of as a collection of code components, some of which are business logic, persistent objects with resources and states that have to be initialized and maintained carefully. In simple application programming, you might write all of the logic initializing these components and connecting them manually But as these components become more complex and numerous it becomes harder and more error-prone to make these connections

Decisions on where to initialize the database before the file cache?; Where should you start that thread pool and the requestListener? And What if something changes? How do you rewrite your code ? Like switching to a different database would have to be considered manually without Inversion of Control. Using Inversion of Control IoC reduces errors and increases code flexibility

Here Spring our framework of choice for inversion of control.

The Spring framework is provided to us in form of dependency injection

Dependency Injection (DI) is a design pattern used to implement IoC. it is a technique in which an object receives other objects that it depends on, called dependencies.

What does Spring do?

When we define a component we also define its dependencies, other components that it needs to perform its function. Then when we run our application Spring ensure that all components are created in a compatible order, and injects dependencies in the components that declared them.

Screenshot from 2021-10-24 18-40-01.png(Source:udacity.com)

Dependencies can be of any value, including everything from strings and numbers to database drivers and request handlers. Spring can recognize dependencies by Java type, component name, and configuration property. With this in place, we can define any number of possible variable components as dependencies to be injected throughout our app, thus making Spring incredibly flexible and powerful.

In addition, we can define constants like service URLs and driver class names as dependencies as well as the services classes that use them. We can define entire layers of business logic methods as components that depend on resources like database drivers that can be changed with a simple config file edit. Sping provides instances of a bean or class to another when they are needed. No need to use the new keyword to create an instance of a dependent class. We only need to create a reference of dependent class and the IoC pattern will set the instance to the reference when needed.

Benefits of Spring and IoC

  • Easy to define application Wide Constants

  • Feature-based Code Organization

  • Flexible Development process

  • Create a loosely coupled application. Greater modularity of a program

The whole essence of Spring is to allow developers to focus on what really matters, features, and business logic. The use of an IoC framework like Spring allows for structured application development where the focus is on dividing the application's requirements into single-purpose components that can be combined to implement features.

References Inversion of Control Inversion of Control. (2021). Retrieved 24 October 2021, from tutorialsteacher.com/ioc/inversion-of-control

Spring Boot Spring Boot. (2021). Retrieved 24 October 2021, from spring.io/projects/spring-boot

Difference between Spring and Spring Boot - GeeksforGeeks. (2020). Retrieved 24 October 2021, from geeksforgeeks.org/difference-between-spring..

Udacityhttps://www.udacity.com/course/java-..