Spring 프레임워크는 Java 플랫폼을 위한 오픈 소스 애플리케이션 프레임워크 -> 간단히 말해 Spring(스프링)이라고 부름

개발자는 스프링을 사용하여 비즈니스 로직에 더 집중할 수 있다
실제 예시
<스프링 사용하기 전>
import java.net.*;
import java.io.*;
public class RawSocketHandler {
public void handleRequest(Socket clientSocket) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
try {
String requestLine = in.readLine();
if (requestLine == null) return;
String[] requestParts = requestLine.split(" ");
String method = requestParts[0];
String path = requestParts[1];
String headerLine;
int contentLength = 0;
while ((headerLine = in.readLine()) != null && !headerLine.isEmpty()) {
if (headerLine.startsWith("Content-Length:")) {
contentLength = Integer.parseInt(headerLine.substring(15).trim());
}
}
char[] bodyChars = new char[contentLength];
in.read(bodyChars, 0, contentLength);
String body = new String(bodyChars);
String responseBody = "";
String statusLine = "HTTP/1.1 200 OK";
if ("/api/add".equals(path) && "POST".equals(method)) {
// JSON 파싱 - {"a": 10, "b": 20}
String json = body.replaceAll("[{}\"\\s]", "");
String[] pairs = json.split(",");
int a = 0, b = 0;
for (String pair : pairs) {
String[] keyValue = pair.split(":");
if (keyValue[0].equals("a")) {
a = Integer.parseInt(keyValue[1]);
} else if (keyValue[0].equals("b")) {
b = Integer.parseInt(keyValue[1]);
}
}
int result = a + b;
responseBody = "{\"result\":" + result + "}";
} else if (!"/api/add".equals(path)) {
statusLine = "HTTP/1.1 404 Not Found";
responseBody = "{\"error\":\"Not found\"}";
} else {
statusLine = "HTTP/1.1 405 Method Not Allowed";
responseBody = "{\"error\":\"Method not allowed\"}";
}
out.println(statusLine);
out.println("Content-Type: application/json");
out.println("Content-Length: " + responseBody.length());
out.println("Connection: close");
out.println();
out.print(responseBody);
out.flush();
} catch (Exception e) {
String errorResponse = "{\"error\":\"Server error\"}";
out.println("HTTP/1.1 500 Internal Server Error");
out.println("Content-Type: application/json");
out.println("Content-Length: " + errorResponse.length());
out.println();
out.print(errorResponse);
out.flush();
} finally {
clientSocket.close();
}
}
}
<스프링 사용한 후>
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
public class SpringCalculator {
@PostMapping("/api/add")
public Map<String, Integer> add(@RequestBody Map<String, Integer> numbers) {
int result = numbers.get("a") + numbers.get("b");
return Map.of("result", result);
}
}
스프링은 복잡한 것을 대신 해주는 편리한 도구!