흐름도에 첫 박스를 그린다
@Autowired 로 주입받아 사용한다@Service@Autowired
package com.example.demo;
import org.springframework.stereotype.Service;
@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";
}
}
<!-- /WEB-INF/views/home.jsp -->
<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<body>
<h1>${msg}</h1>
</body>
</html>
컨트롤러에 new HelloService() 가 없습니다. 컨테이너가 자동 주입한 결과입니다.
Hello Servlet 한 개로 응답을 만들었다.
Spring 프로젝트 안에서 Bean 두 개(Service · Controller)가 자동 연결되어 응답을 만든다.
@Service + @Autowired 로 Bean 주입을 첫 코드로 본다다음 차시(Part 3): MVC — 왜 분업하나.