본문 바로가기

STUDY/Spring

[토비의 스프링부트 - 이해와 원리] 독립 실행형 서블릿 애플리케이션

* Containerless

@SpringBootApplication
public class StudyApplication {

	public static void main(String[] args) {
		SpringApplication.run(StudyApplication.class, args);
	}
}

main 메소드만 실행 시켰더니 웹 컨테이너가 작동 

 

Containerless 목적 :  Spring Controller에 존재하는 Bean 관련해서만 작업하고, Servlet Container에 있는 배포, 구성 등의 작업을 SpringBoot에 위임 

 

빈 Servlet Container 띄우기

package com.yoon.tobyspring.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;

@SpringBootApplication
public class StudyApplication {

	public static void main(String[] args) {
		// SpringApplication.run(StudyApplication.class, args);
		// tomcat을 설치하지 않고 tomcat 서버 띄우기
		// embedded tomcat
		ServletWebServerFactory serverFactory = new TomcatServletWebServerFactory();
		WebServer webServer = serverFactory.getWebServer();
		webServer.start();
	}

}



> Task :StudyApplication.main()
00:11:29.197 [main] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer -- Tomcat initialized with port 8080 (http)
00:11:29.538 [main] INFO org.springframework.boot.web.embedded.tomcat.TomcatWebServer -- Tomcat started on port 8080 (http) with context path ''

 

package com.yoon.tobyspring.study;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;

import java.io.IOException;

@SpringBootApplication
public class StudyApplication {

	public static void main(String[] args) {
		// SpringApplication.run(StudyApplication.class, args);
		// tomcat을 설치하지 않고 tomcat 서버 띄우기
		// embedded tomcat
		ServletWebServerFactory serverFactory = new TomcatServletWebServerFactory();
		WebServer webServer = serverFactory.getWebServer(servletContext -> {
            servletContext.addServlet("hello", new HttpServlet() {
				@Override
            	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
					// HTTP Status
					resp.setStatus(200);
					// Headers
					resp.setHeader("Content-Type", "text/plain");
					// Message Body
					resp.getWriter().println("Hello Servlet");
				}
			}).addMapping("/hello"); // /hello로 들어오는 요청을 HttpServlet()의 service()에서 처리하겠다는 의미
        });
		webServer.start();
	}

}