Mastering Spring Framework: The Backbone of Modern Java Backend
Learn how Spring makes backend development easier by using IoC, DI, and powerful modules to make apps that can grow, are safe, and are ready for production.

What is the Spring Framework?
Spring is a lightweight, open-source framework for building large Java applications. It uses key ideas like Dependency Injection, Inversion of Control, Aspect-Oriented Programming, and Modularity. You can use only the parts you need, and it integrates easily.
Why Choose Spring Instead of Java or Other Frameworks?
Spring promotes loose coupling
Plain Java – Components are closely connected.
Spring – Components are more independent:
Building REST APIs
Plain Java (Servlet): Handle HTTP requests and responses manually.
Spring MVC: Use simple, annotation-based REST controllers.
Security Implementation
Plain Java: You have to manually check authentication for each endpoint, manage sessions, hash passwords, and protect against CSRF.
Spring Security: Offers complete security with less code.
Spring Framework Architecture
Core Concepts Explained
Inversion of Control (IoC) and Dependency Injection (DI) Inversion of Control (IoC): This design principle lets the Spring Container handle object creation instead of your code doing it manually. Dependency Injection (DI): A design pattern using IoC, where Spring gives a class its dependencies instead of the class creating them. This makes the code more flexible and easier to test.
Types of Dependency Injection
Constructor Injection: Why use it? It creates unchangeable objects. Dependencies are always available. It's easy to test because you can pass mock objects through the constructor.
Setter Injection: Why use it? It's useful for optional dependencies or when you need to change a bean after it's created.
Field Injection (Avoid in Production): Why avoid it? It hides dependencies, making the class confusing. You can't mark fields as final. It's hard to unit test without Spring context.
Spring Bean Lifecycle
A Spring bean goes through these main stages:
Instantiation → Dependency Injection → Aware → Pre-Initialization → Initialization → Post-Initialization → Ready → Destruction
Instantiation Phase
In this phase, the Spring container creates the bean object using the constructor. No dependencies are injected yet.
Dependency Injection Phase
Spring injects all needed dependencies into the bean. This can be done using @Autowired, constructor injection, or setters. The bean becomes usable internally.
Aware Phase
If the bean uses special Aware interfaces, Spring provides container-related details. Common interfaces include BeanNameAware, BeanFactoryAware, and ApplicationContextAware.
Pre-Initialization Phase
Spring calls BeanPostProcessor.postProcessBeforeInitialization(). This allows changes to the bean before initialization. Used internally by Spring for things like AOP and proxies.
Initialization Phase
This phase is for custom initialization logic after dependencies are injected. There are three ways: @PostConstruct (recommended), afterPropertiesSet() (InitializingBean), and a custom init-method.
Post-Initialization Phase
Spring calls BeanPostProcessor.postProcessAfterInitialization(). This is for final modifications of the bean, often used for proxy creation like Spring AOP.
Ready Phase
The bean is fully initialized and ready to be used anywhere in the application.
Destruction Phase
This phase happens when the Spring container shuts down. It's used for closing database connections and releasing resources. There are three ways: @PreDestroy (recommended), destroy() (DisposableBean), and a custom destroy-method.
Spring Bean Scopes
In Spring, bean scope determines how many bean instances are created and how long they stay in the container. This affects memory use, performance, thread safety, and application behavior.
Scope Overview
Singleton → Prototype → Request → Session → Application → WebSocket
Singleton Scope
Spring creates just one instance of the bean, kept in the Spring IoC container cache. Each time you ask for the bean, the same object is returned. It's like a shared service for the entire application. When to use it? For stateless services (no user-specific data), business logic classes, and DAO/Repository layers.
Key Insight: Only one object → better memory usage; must be stateless to avoid concurrency issues
Prototype Scope
Spring makes a new instance each time the bean is requested. There's no caching, and the lifecycle is only partly managed. Spring creates it but doesn't destroy it. It's like getting a new object every time you call new.
Key Insight : Use for stateful objects, You must handle cleanup manually
Request Scope
(Web Only) One bean per HTTP request. A new object is made for each request. Imagine: Every API call gets a new bean instance.
Key Insight: Works only in Spring Web / Spring Boot,deal for request-specific data
Session Scope
(Web Only) One bean per user session. The same bean is used for multiple requests from the same user. Think: One user = One bean.
Key Insight: Used for: Login info, shopping cart, lives until session expires
Application Scope
(Web Only) One bean for the whole web application, stored in ServletContext. It's like a global shared object for all users.
Key Insight: Shared across all users ,Similar to Singleton but at ServletContext level
WebSocket Scope
One bean is created for each WebSocket session. Imagine: One connection equals one bean.
Key Insight: Used in: Chat apps, Live notifications
Spring Annotations
Spring Annotations make setup easier by reducing XML and adding details directly in Java code. They guide Spring on what to create, how to manage it, and how components work together.
Stereotype Annotations:
These annotations tell Spring**:**
“This class is a Spring-managed component (bean)”
@Component: Base annotation for all Spring-managed components Marks a class as a bean
Key Insight: Generic annotation. Other stereotypes are built on top of this
@Service Specialized version of @Component Used for business logic layer
Key Insight:Improves readability & architecture clarity
@Repository is used for the data access layer (DAO) and provides automatic exception translation.
Key Insight: Changes database errors into Spring errors
@Controller is used in Spring MVC to handle HTTP requests.
Dependency Injection Annotations
These annotations help Spring automatically add dependencies.
@Autowired automatically adds dependencies based on type.
Key Insight: Can be used on fields, constructors (recommended), and setters.
@Qualifier is used when there are multiple beans of the same type. It helps select a specific bean.
@Value adds values from a properties file.
Configuration Annotations
These annotations show how Spring creates and manages beans.
@Configuration marks a class as a configuration class and is used to define beans manually.
@Bean Manually creates and registers a bean.
Key Insight: Use when you don't control the class (third-party) or need custom setup.
@ComponentScan tells Spring where to find components.
Bean Lifecycle Annotations
These manage the start and end of a bean's life.
@PostConstruct runs after dependencies are added.
@PreDestroy runs before a bean is destroyed.
Scope Annotation
@Scope sets the bean's scope, like singleton or prototype.
Other Helpful Annotations
@Required (Deprecated) Makes sure a property is set, but it's not used anymore.
@Primary selects the default bean when there are multiple options.





