Skip to content

Commit

Permalink
[Spring Boot] 스프링 부트와 내장 톰캣: 스프링 컨테이너 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
jihunparkme committed Apr 25, 2023
1 parent 2de424b commit 31c8afb
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions 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();
}
}

0 comments on commit 31c8afb

Please sign in to comment.