Introduction to Spring & Spring Boot Part-1



Important Components of Spring Framework

  1. Core Container
  2. AOP (Aspect-Oriented Programming)
  3. JDBC (Java Database Connectivity)
  4. Web
  5. Testing
IoC (Inversion of Control) : 

The IoC container in the Spring Framework is responsible for managing the components of an application. It injects dependencies into these components instead of letting the components create their own dependencies. The IoC container Creates objects (beans) ,Wires them together, Configures them
Manages their complete lifecycle.

Bean in Spring :

A Bean is a managed object that is instantiated, assembled, and managed by the Spring IoC container.
Beans are the backbone of a Spring application and are the core building blocks that are wired together to create the application.

Defining Beans

Beans can be defined in two ways:

1. Using Stereotype AnnotationsAnnotate a class with one of the stereotype annotations:
  • @Component
  • @Service
  • @Repository
  • @Controller
  These annotations inform Spring that the class should be managed as a bean.

2. Explicit Bean Declaration in a Configuration Class 

Define a configuration class and annotate it with @Configuration.
Inside this class, define methods with @Bean to specify and configure beans.

Example:
 @Configuration  
 public class AppConfig {  
  @Bean  
  public Apple getApple() {  
   return new Apple();  
  }  
 }  

Bean Lifecycle :

Spring manages the lifecycle of a bean through several stages:

1. Bean Created : 
    The bean instance is created using static factory method or
     An instance factory method (for annotation-based configuration)

2. Dependency Injection :
    Spring sets the bean's properties and dependencies using Setter Injection ,Constructor Injection or Field Injection

3. Bean Initialized :
    If the bean implements the InitializingBean interface, Spring invokes the initialization method.
    Alternatively, you can use the @PostConstruct annotation to specify an initialization method.

4. Bean is Used : 
The bean is fully initialized and ready to be used by the application.

5. Bean Destroyed :
Spring invokes the destroy method when the bean is no longer needed.


Bean Lifecycle Hooks

@PostConstruct Used to mark a method that should be executed immediately after the bean has been constructed and all dependencies have been injected.

@PreDestroy Used to mark a method that should be invoked just before a bean is destroyed by the IoC container.

This method can perform cleanup tasks, such as releasing resources.

Scope of Beans in Spring Framework :

The scope of a bean determines how and when the bean is created and shared within the Spring IoC container.

1. Singleton (Default) : A single bean instance is created and maintained within the Spring IoC container. Every request for this bean returns the same instance.

2. Prototype : A new bean instance is created every time it is requested. Multiple instances of the bean exist, unlike Singleton.

3. Request : A single bean instance is created for each HTTP request. Each request gets its own instance of the bean.

4. WebSocket : A single bean instance is created for the lifecycle of a WebSocket session. This scope is valid only in the context of a web-aware Spring Application.

Declaring Scope 

@Scope("prototype")

Dependency Injection in Spring :

Dependency Injection (DI) is a design pattern used in the Spring Framework to decouple components in a software application. Instead of creating dependencies manually, Spring injects dependencies into a component from an external source.
The Spring IoC container manages these dependencies.

Benefits of Dependency Injection :

1. Loose Coupling:
Components are decoupled from their dependencies, making them easier to maintain and test.

2. Flexible Configuration:
Dependencies can be configured externally, making customization and swapping of components easier.

3. Improved Testability:
Components can be easily mocked or replaced during testing, making unit testing more efficient.
How to Inject Dependencies in Spring

Spring provides different ways to inject dependencies:

1. Constructor Injection :

Dependencies are provided through a constructor.

2. Field Injection :
Uses the @Autowired annotation to inject dependencies directly into fields.

Spring Boot vs Spring Framework

  1. Key DifferencesSpring Boot includes starter dependencies that simplify project setup.
  2. Spring Boot provides auto-configuration, reducing manual setup.
  3. Spring Boot includes an embedded server (Tomcat/Jetty), whereas Spring Framework requires external configuration.
  4. Built-in monitoring and health checks in Spring Boot provide better observability.