Spring is the most popular open-source Java application development framework. Initially written by Rod Johnson and first released in June 2003, Spring provides comprehensive infrastructure support for developing Java applications. It lets you focus on your application's business logic while Spring handles the infrastructure.
Spring's core philosophy is Inversion of Control (IoC) - instead of your code creating and managing objects, the Spring tl-container creates and manages them for you. This is achieved through Dependency Injection (DI), where dependencies are "injected" into objects rather than objects creating their own dependencies.
| Module | Description |
|---|---|
| Spring Core | IoC container, DI, Bean lifecycle management |
| Spring MVC | Web MVC framework with DispatcherServlet |
| Spring Boot | Auto-configuration, embedded server, production-ready features |
| Spring Data | Simplified data access (JPA, MongoDB, Redis, etc.) |
| Spring Security | Authentication, authorization, OAuth2, JWT |
| Spring Cloud | Microservices, service discovery, config server |
| Spring Batch | Batch processing for large datasets |
| Spring Integration | Enterprise integration patterns |
| Spring AOP | Aspect-Oriented Programming support |
| Spring WebFlux | Reactive, non-blocking web framework |
| Feature | Spring | EJB (Enterprise JavaBeans) |
|---|---|---|
| Container | Lightweight IoC container | Heavy EJB tl-container (full Java EE server) |
| Deployment | Any servlet tl-container (Tomcat) | Requires full Java EE server (JBoss, GlassFish) |
| Testing | Easy unit testing (POJO-based) | Difficult (requires container) |
| Configuration | Annotations or XML | Complex XML descriptors |
| Learning Curve | Moderate | Steep |
| Performance | Excellent | Good (but heavier) |
| Popularity | Very high | Declining |
// WITHOUT Spring: tight coupling, hard to test
public class OrderService {
// Creates its own dependency - tightly coupled!
private EmailService emailService = new EmailService();
private PaymentService paymentService = new PaymentService();
public void placeOrder(Order order) {
paymentService.processPayment(order);
emailService.sendConfirmation(order);
}
}
// Problem: Can't easily swap EmailService with MockEmailService for testing
// Problem: OrderService is responsible for creating its dependencies
// WITH Spring: loose coupling, easy to test
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Service
public class OrderService {
// Spring injects these - loosely coupled!
private final EmailService emailService;
private final PaymentService paymentService;
// Constructor injection (recommended)
@Autowired
public OrderService(EmailService emailService, PaymentService paymentService) {
this.emailService = emailService;
this.paymentService = paymentService;
}
public void placeOrder(Order order) {
paymentService.processPayment(order);
emailService.sendConfirmation(order);
}
}
// Spring creates and injects EmailService and PaymentService automatically
// Easy to test: inject mock implementations in unit tests
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// @SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
// Starts the embedded Tomcat server and Spring context
SpringApplication.run(MyApplication.class, args);
}
}
// Run: mvn spring-boot:run
// Or: java -jar target/myapp.jar
// Access: http://localhost:8080
Explore 500+ free tutorials across 20+ languages and frameworks.