* 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();
}
}
'STUDY > Spring' 카테고리의 다른 글
[토비의 스프링부트 - 이해와 원리] 스프링 부트 시작하기 (0) | 2024.02.17 |
---|---|
[토비의 스프링부트 - 이해와 원리] 스프링 부트 살펴보기 (0) | 2023.12.11 |
[Spring boot] 간단한 API 개발 (0) | 2023.12.09 |
[Spring] Spring, MariaDB, MyBatis 연동 (0) | 2023.12.09 |
[Spring] MariaDB, MySQL Workbench 설치하기 (0) | 2023.12.09 |