Introduction to the Spring Framework
What is Spring?
The Spring Framework is an open-source, enterprise-level application framework for Java. Created by Rod Johnson in 2003, it was designed as a lightweight alternative to the heavy, complex Java EE (Enterprise Edition) specifications of the time.
Spring provides comprehensive infrastructure support for developing robust Java applications. It handles the "plumbing" (object lifecycle, configuration, database connections, security) so developers can focus on business logic.
Core Concepts: IoC and DI
To understand Spring, you must understand two architectural patterns it heavily employs: Inversion of Control (IoC) and Dependency Injection (DI).
The Problem: Tight Coupling
In traditional Java, if Class A depends on Class B, Class A usually instantiates Class B directly.
public class NotificationService {
private EmailSender emailSender;
public NotificationService() {
// Tight coupling! NotificationService is responsible for creating EmailSender.
this.emailSender = new EmailSender();
}
}
This makes testing difficult (you can't easily mock EmailSender) and makes swapping implementations (e.g., using SmsSender instead) a nightmare.
The Solution: Dependency Injection
With DI, the object does not create its dependencies. Instead, dependencies are "injected" from the outside.
public class NotificationService {
private MessageSender sender; // Depends on an abstraction/interface
// Spring injects the dependency via the constructor
public NotificationService(MessageSender sender) {
this.sender = sender;
}
}
Inversion of Control (IoC) Container
The IoC Container is the core of the Spring Framework. It acts as an orchestrator. It reads configuration (Java annotations or XML), instantiates the objects (called Spring Beans), wires their dependencies together, and manages their entire lifecycle. Control is "inverted" from the programmer to the framework.
Spring Architecture and Modules
Spring is highly modular. You only bring in the parts you need.
- Spring Core Container: The heart of Spring (Beans, Core, Context, SpEL). It provides the IoC container.
- Spring AOP (Aspect-Oriented Programming): Allows separation of cross-cutting concerns (like logging, security, and transactions) from business logic.
- Spring Data Access / Integration: JDBC, ORM (Hibernate integration), JMS, and Transaction Management.
- Spring Web (MVC): A powerful framework for building web applications and RESTful APIs.
- Spring Security: Comprehensive authentication and authorization.
Working with Spring Beans
A Bean is simply an object that is instantiated, assembled, and managed by a Spring IoC container.
1. Defining Beans using Stereotype Annotations
Spring automatically scans your codebase (Component Scanning) for classes annotated with specific "stereotype" annotations and registers them as beans.
@Component: A generic stereotype for any Spring-managed component.@Service: A specialization of@Componentfor business logic classes.@Repository: A specialization for Data Access Objects (DAOs). It also translates database exceptions into Spring'sDataAccessException.@Controller/@RestController: For web layer classes handling HTTP requests.
@Service
public class UserService {
// Business logic here
}
2. Wiring Beans with @Autowired
To inject a bean into another class, you can use @Autowired (though constructor injection without the annotation is preferred in modern Spring).
@RestController
public class UserController {
private final UserService userService;
// Constructor Injection (Recommended)
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
// ...
}
3. Java-based Configuration (@Configuration and @Bean)
If you need to configure external libraries or beans that you didn't write (and therefore can't annotate with @Component), you use configuration classes.
@Configuration
public class AppConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Here, the Spring container calls the restTemplate() method and registers the returned object as a bean.
Spring vs. Spring Boot
It is crucial to distinguish between Spring and Spring Boot.
- Spring Framework is the underlying engine (IoC, DI, MVC). Setting it up traditionally requires a lot of configuration.
- Spring Boot is an extension of the Spring Framework. It provides auto-configuration, embedded servers (Tomcat), and starter POMs to get applications running with zero XML and minimal code.
Summary
The Spring Framework fundamentally changes how Java applications are built by enforcing decoupled, modular design through Dependency Injection. By offloading object lifecycle management to the IoC container, developers achieve highly testable and maintainable code architectures.