스프링 컨테이너가 어셈플러로서 SimpleHelloService 빈의 객체를 HelloController가 사용할 수 있도록 주입하는 작업 수행 ⇒ 생성자 주입 방식을 이용

HelloService 인터페이스

public interface HelloService {
    String sayHello(String name);
}

HelloController

public class HelloController {
    private final HelloService helloService;

    public HelloController(HelloService helloService) {
        this.helloService = helloService;
    }

    public String hello(String name) {
        return helloService.sayHello(Objects.requireNonNull(name)) ;
    }
}

HellobootApplication

public class HellobootApplication {

	public static void main(String[] args) {
		// 스프링 컨테이너 생성
		GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext();
		applicationContext.registerBean(HelloController.class); // 빈 등록
		applicationContext.registerBean(SimpleHelloService.class); // 빈 등록
		applicationContext.refresh(); // 컨테이너 초기화(빈 오브젝트 생성)

		ServletWebServerFactory serverFactory = new TomcatServletWebServerFactory();
		WebServer webServer = serverFactory.getWebServer(servletContext -> {
            servletContext.addServlet("frontcontroller", new HttpServlet() {
				@Override
				protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
					// 인증, 보안, 다국어, 공통 기능
					if(req.getRequestURI().equals("/hello") && req.getMethod().equals(HttpMethod.GET.name())) {
						String name = req.getParameter("name");

						HelloController helloController = applicationContext.getBean(HelloController.class);
						String ret = helloController.hello(name); // 파라미터 추출

						resp.setContentType(MediaType.TEXT_PLAIN_VALUE);
						resp.getWriter().println(ret); // 결과값을 웹 응답에 담음
					}
					else {
						resp.setStatus(HttpStatus.NOT_FOUND.value());
					}
				}
			}).addMapping("/*");
        }); // 웹서버 생성
		webServer.start(); // Tomcat Servelet Container 동작
	}
}

구조

classDiagram
direction BT
class HelloController {
  + HelloController(HelloService) 
  - HelloService helloService
  + hello(String) String
}
class HelloService {
<<Interface>>
  + sayHello(String) String
}
class HellobootApplication {
  + HellobootApplication() 
  + main(String[]) void
}
class SimpleHelloService {
  + SimpleHelloService() 
  + sayHello(String) String
}

HelloController "1" *--> "helloService 1" HelloService 
SimpleHelloService  ..>  HelloService