▣ PART · SPRING

첫 Spring 프로젝트 + Bean 주입

Spring 기초
SMHRD

학습 목표

1. 프로젝트 생성 5 단계

  1. File → New → Spring Legacy Project
  2. Spring MVC Project 템플릿
  3. 패키지명 점 3 개 이상 (예: com.example.demo)
  4. Maven Update Project
  5. 의존성 다운로드 대기

2. 핵심 코드 한눈에

@Service public class HelloService { public String getMessage() { return "Spring Bean 으로 인사드립니다!"; } } @Controller public class HomeController { @Autowired private HelloService helloService; ← 자동 주입 @RequestMapping("/") public String home(Model model) { model.addAttribute("msg", helloService.getMessage()); return "home"; } }

3. 일어난 일

브라우저 GET / → DispatcherServlet → HomeController.home() → HelloService.getMessage() (Bean 자동 주입 결과) → model 에 담기 → "home" → home.jsp → ${msg} 채워서 응답

4. ⚠️ 실험으로 확인

@Service 를 빼보면: "No qualifying bean of type 'HelloService'" 에러. Bean 등록 없이는 자동 주입이 불가능함을 직접 체험.

5. Before / After

Part 1 끝

Servlet 한 개로 응답

Part 2 끝

Spring 컨테이너가 두 Bean 을 자동 연결해 응답을 만든다

학습 확인 체크리스트