Why Use Spring?
You could write library-service in plain Java: open a socket, parse HTTP by hand, create every object with new, manage database connections yourself. It would take weeks before you wrote a single line about books. Spring exists so that those weeks go into the part only you can write.
The rule
Spring takes the plumbing so your code can be about the problem.
"Plumbing" is everything every server needs but no customer ever asks for:
- Wiring objects together. Without Spring,
mainends up building the whole object graph by hand:new BookController(new LoanService(new BookRepository(dataSource))), and every change ripples through it. Spring does this wiring for you. - HTTP and JSON. Spring MVC turns a request into a method call and a Java object into JSON.
- Data access and transactions. Spring Data writes common queries for you;
@Transactionalcommits or rolls back. - Security, metrics, health checks. Each arrives as a starter instead of a project.
Spring Boot adds the last mile: an embedded server, sensible defaults, and a single runnable jar.
@Service
class LoanService {
private final BookRepository books;
LoanService(BookRepository books) { this.books = books; }
@Transactional
Loan borrow(long bookId, long memberId) { /* only library rules here */ }
}Look at what is missing: no connection handling, no commit or rollback code, no object creation. What remains is the library's logic.
There are costs, and a good engineer names them. Spring apps start more slowly than a tiny hand-written program, use more memory, and do work "behind the scenes" that can confuse a beginner when something goes wrong. For a short script or a small command-line tool, Spring is often too much.