DI
Dependency Inversion: A design pattern that provides for loose coupling; it uses service components, consumer classes, and injector classes.
Service components
interface/abstract class that define a contract for services
Consumer class
written in terms of service interface
What is Spring?
Spring is an open-source framework that sits on top of Java; series of JAR files (dependencies to add to Maven); provides an abstract layer to some of more complex parts of Java; Spring helps structure whole applications in a consistent, productive manner, encouraging SOLID design
What are the benefits of Spring?
Lightweight - about 2 MB; IoC (Inversion of Control) - loose coupling; Spring Container: Spring contains and manages lifecycle of objects; Boilerplate code - Spring provides templates which allow you to reduce amount of boilerplate code.
Spring Core Container
Spring container is the core of Spring Framework, responsible for creating and configuring the objects needed for an application, managing complete lifecycle for creation/destruction of objects; uses DI to wire objects together; these objects are called Spring Beans
ApplicationContext
Interface used to represent the Spring IoC container and therefore load up the configuration
@Component Annotation
@Component tells Spring: “Create an instance of this class and manage it as a bean in the ApplicationContext.”
@Autowired Annotation
@Autowired tells Spring: “Inject a bean of the required type here.” Spring looks in the ApplicationContext, finds a matching bean, and injects it.
IoC
Inversion of Control: It is responsible for creating, managing, and configuring application objects (beans). Using Dependency Injection:
Automatically injects required dependencies into beans.
Manages bean creation, initialization, and destruction.
Supports XML, annotation-based, and Java-based configuration.
Separates object creation from business logic, making applications more flexible and testable.