Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Spring Boot] 스프링 부트와 내장 톰캣: 스프링 컨테이너 연결
- Loading branch information
1 parent
2de424b
commit 31c8afb
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
embed/src/main/java/hello/embed/EmbedTomcatSpringMain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |