스프링 부트로 개발된 웹 어플리케이션을 톰캣에 올리는 방법입니다.
이클립스에서 단독 실행 모드와 톰캣 배포 방식 두가지 다 가능하기 때문에
개발할떄는 단독으로 띄워서 개발을 하고 배포할때만 톰캣을 띄워서 쉽게 배포를 하셔도 됩니다.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.donnert</groupId>
<artifactId>spring.boot.web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring.boot.web</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceencoding>UTF-8</project.build.sourceencoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
기존에 웹에서 사용하던 파일과 동일합니다.
추가적으로 spring-boot-starter-tomcat을 넣어줍니다.
Application.java
package net.donnert.spring.boot.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
SpringBootServletInitializer를 상속받으셔야 톰캣 기동 시 정상적으로 스프링 모듈이 동작합니다.
그리고 configure를 오버라이드 해주셔야 서블릿 컨테이너 기동 시 스프링 설정을 적용시켜줍니다.
TestController.java
package net.donnert.spring.boot.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping("test")
public String test() {
return "This is spring";
}
}
그냥 테스트용 컨트롤러이니다.
테스트
Application을 실행시켜줍니다. 스프링 부트 어플리케이션이 실행이 됩니다.
http://localhost:8080/test를 실행해보시면 This is spring이 찍히는 것을 볼 수 있습니다.
톰캣에 올리기
이미 Application에 SpringBootServletInitializer를 상속받아놨기 때문에 별다른 설정은 없습니다.
톰캣에 올리기 위해 Dynamic Web Module 프로젝트로 설정해줍니다.
(프로젝트 우클릭-Properties-Project Facets-Dynamic Web Module 체크)

설정된 톰캣에 우클릭을 해서 Add and Remove를 선택하면 스프링 어플리케이션이 보일겁니다.
(톰캣 설정은 따로 안다루겠습니다.)
추가 후 모듈의 Path를 /spring로 변경합니다.

톰캣 스타트 후 http://localhost:8080/spring/test를 때려보시면 This is spring이 보입니다.