From 31c8afb99b0a6ca1e0cd791695ba0c73fa48dc80 Mon Sep 17 00:00:00 2001 From: Jihun Park Date: Tue, 25 Apr 2023 21:17:06 +0900 Subject: [PATCH] =?UTF-8?q?[Spring=20Boot]=20=EC=8A=A4=ED=94=84=EB=A7=81?= =?UTF-8?q?=20=EB=B6=80=ED=8A=B8=EC=99=80=20=EB=82=B4=EC=9E=A5=20=ED=86=B0?= =?UTF-8?q?=EC=BA=A3:=20=EC=8A=A4=ED=94=84=EB=A7=81=20=EC=BB=A8=ED=85=8C?= =?UTF-8?q?=EC=9D=B4=EB=84=88=20=EC=97=B0=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hello/embed/EmbedTomcatSpringMain.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 embed/src/main/java/hello/embed/EmbedTomcatSpringMain.java diff --git a/embed/src/main/java/hello/embed/EmbedTomcatSpringMain.java b/embed/src/main/java/hello/embed/EmbedTomcatSpringMain.java new file mode 100644 index 0000000..773f37b --- /dev/null +++ b/embed/src/main/java/hello/embed/EmbedTomcatSpringMain.java @@ -0,0 +1,37 @@ +package hello.embed; + +import hello.spring.HelloConfig; +import org.apache.catalina.Context; +import org.apache.catalina.LifecycleException; +import org.apache.catalina.connector.Connector; +import org.apache.catalina.startup.Tomcat; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.servlet.DispatcherServlet; + +public class EmbedTomcatSpringMain { + + public static void main(String[] args) throws LifecycleException { + System.out.println("EmbedTomcatSpringMain.main"); + + // 내장 톰캣을 생성해서 8080 포트로 연결하도록 설정 + Tomcat tomcat = new Tomcat(); + Connector connector = new Connector(); + connector.setPort(8080); + tomcat.setConnector(connector); + + // 스프링 컨테이너를 생성하고, 필요한 빈을 등록 + AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext(); + appContext.register(HelloConfig.class); + + // 스프링 MVC 디스패처 서블릿 생성, 스프링 컨테이너 연결 + DispatcherServlet dispatcher = new DispatcherServlet(appContext); + + //디스패처 서블릿 등록 + Context context = tomcat.addContext("", "/"); + tomcat.addServlet("", "dispatcher", dispatcher); + context.addServletMappingDecoded("/", "dispatcher"); + + // 내장 톰캣 실행 + tomcat.start(); + } +}