Introduction to Spring
What is Spring?
Spring Framework is an open-source application framework for Java. It simplifies enterprise development by providing:
- Inversion of Control (IoC) — the container manages object creation and wiring
- Aspect-Oriented Programming (AOP) — cross-cutting concerns (logging, transactions) separated from business logic
- Integration — databases, messaging, security, web APIs through consistent abstractions
Spring is not a single library — it is an ecosystem of projects built around a core container.
Spring Ecosystem
| Project | Purpose |
|---|---|
| Spring Framework | Core IoC, MVC, AOP |
| Spring Boot | Auto-configuration, embedded servers, starters |
| Spring Data | JPA, MongoDB, Redis repositories |
| Spring Security | Authentication, authorization, OAuth2 |
| Spring Cloud | Microservices (config, discovery, gateway) |
| Spring Batch | Large-scale batch processing |
Most new projects start with Spring Boot, which wraps the core framework with sensible defaults.
Why Spring?
- Mature — 20+ years, massive community and documentation
- Productive — Spring Boot reduces boilerplate dramatically
- Testable — built-in testing support with
@SpringBootTest - Portable — deploy to Tomcat, Jetty, Undertow, or serverless
- Enterprise-ready — transactions, security, monitoring out of the box
Alternatives exist (Quarkus, Micronaut, Jakarta EE), but Spring dominates Java job markets and enterprise adoption.
Spring vs Plain Java
Without Spring:
// Manual wiring — tight coupling
public class OrderService {
private final OrderRepository repo = new JdbcOrderRepository(new DataSourceConfig().getDataSource());
private final EmailService email = new SmtpEmailService();
}
With Spring:
@Service
public class OrderService {
private final OrderRepository repo;
private final EmailService email;
public OrderService(OrderRepository repo, EmailService email) {
this.repo = repo;
this.email = email;
}
}
Spring’s container injects dependencies automatically — you declare what you need, not how to construct it.
Your First Spring Boot App
# Using Spring Initializr (https://start.spring.io)
# Select: Java 21, Maven, Spring Boot 3.x, Spring Web
./mvnw spring-boot:run
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@RestController
public class HelloController {
@GetMapping("/hello")
public Map<String, String> hello() {
return Map.of("message", "Hello, Spring!");
}
}
Visit http://localhost:8080/hello. Spring Boot starts an embedded Tomcat server automatically.
Project Structure Convention
src/
├── main/
│ ├── java/com/example/demo/
│ │ ├── DemoApplication.java
│ │ ├── controller/
│ │ ├── service/
│ │ ├── repository/
│ │ └── model/
│ └── resources/
│ ├── application.properties
│ └── static/
└── test/
└── java/com/example/demo/
Follow package-by-feature or package-by-layer — consistency matters more than the specific choice.
Configuration
application.properties (or application.yml):
server.port=8080
spring.application.name=demo
logging.level.com.example=DEBUG
Profile-specific config:
# application-dev.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/devdb
# application-prod.properties
spring.datasource.url=jdbc:postgresql://prod-host:5432/proddb
Activate with --spring.profiles.active=dev or environment variable.
Spring Boot Starters
Starters bundle dependencies for common use cases:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
No version management needed — Spring Boot’s BOM handles compatibility.
Testing
@SpringBootTest
@AutoConfigureMockMvc
class HelloControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
void helloReturnsMessage() throws Exception {
mockMvc.perform(get("/hello"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.message").value("Hello, Spring!"));
}
}
Run with ./mvnw test. Spring Boot provides test slices (@WebMvcTest, @DataJpaTest) for focused testing.
Learning Path
- Spring Core — understand IoC, beans, and dependency injection
- Spring Boot — auto-configuration, REST APIs, actuator
- Spring Data — JPA repositories, database integration
- Spring Security — authentication and authorization
Common Pitfalls for Beginners
- Adding
@Componenteverywhere — use specific stereotypes (@Service,@Repository) - Field injection (
@Autowiredon fields) — prefer constructor injection - Ignoring profiles — hardcoding config breaks across environments
- Skipping tests — Spring’s test support is a major productivity advantage
Spring removes infrastructure friction so you focus on business logic. The next pages dive into each layer of the stack.