Spring is a Java application framework centered on an inversion-of-control container. The container creates application objects, connects their dependencies, applies lifecycle and infrastructure behavior, and lets business code depend on explicit contracts.
Spring Framework supplies the core container and libraries. Spring Boot builds on it with dependency starters, auto-configuration, executable applications, externalized configuration, and production support. Spring Boot 4.1 requires Java 17 or later.
Inversion of Control means object creation and wiring move from application classes to a container. Dependency Injection is the practical mechanism: a class declares what it needs through its constructor, and the container supplies matching beans.
Constructor injection keeps required dependencies visible, supports immutable fields, and lets an ordinary unit test instantiate the class without starting Spring. Component scanning is convenient, but explicit configuration remains useful when construction needs a third-party class or a deliberate choice.
| 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 Framework | Spring Boot |
|---|---|---|
| Foundation | Core container and application libraries | Opinionated setup built on Spring Framework |
| Configuration | Choose and configure framework modules | Auto-configuration backs off when explicit beans are supplied |
| Dependencies | Select individual modules | Starters provide tested dependency sets |
| Runtime | Can be packaged for a chosen environment | Commonly runs as an executable application with an embedded server |
| Operations | Integrate monitoring as needed | Actuator adds health, metrics, and management endpoints |
| Use together | Defines the programming model | Provides the normal application bootstrap and conventions |
| Escape hatch | Declare beans and infrastructure explicitly | Exclude or replace an unwanted auto-configuration |
// 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
A typical web application separates HTTP concerns, business rules, and persistence. A controller validates transport input and chooses a response, a service coordinates a use case and transaction, and a repository handles storage. The names are less important than keeping dependencies pointed toward stable application rules.
| Layer | Owns | Avoid |
|---|---|---|
| Controller | HTTP mapping, status, request DTO validation | Business transactions and SQL |
| Service | Use-case rules and transaction boundary | Servlet APIs and view formatting |
| Repository | Queries and persistence mapping | Authorization and HTTP responses |
| Configuration | Bean creation and infrastructure choices | Feature behavior |
Spring Boot reads external configuration from ordered property sources. Bind related settings to a validated @ConfigurationProperties type instead of scattering @Value strings. Profiles select groups of beans or configuration, but they should not become a substitute for versioned environment settings.
Never commit production secrets. Supply them through the deployment platform or a secret manager, and fail startup when required configuration is absent or invalid.
Use a plain unit test for one class and mocked collaborators. Use a focused test slice when framework behavior such as MVC binding or data mapping is the subject. Use @SpringBootTest only when the complete application context and integrated wiring are genuinely required.
Explore 500+ free tutorials across 20+ languages and frameworks.