eclipse์์ spring-boot๋ก web ๋ง๋ค๊ธฐ
Spring ํ๊ฒฝ์์ ์น ์ดํ๋ฆฌ์ผ์ด์ ์ ๋ง๋ค์ด์ผ ํ๋ค๋ฉด pom.xml ์ ์ด๋ฐ์ ๋ฐ ์ค์ ๋ค์ ์ ์ด์ค์ผ ํ๋ค. ํ์ง๋ง ์ด๋ฐ ์๊ณ (?)๋ฅผ ๋์ด์ค์ ์๋ ๋ฐฉ๋ฒ์ค์ ํ๊ฐ์ง๊ฐ ๋ฐ๋ก Spring Boot๋ก ๋ง๋๋ ๋ฐฉ๋ฒ์ธ๋ฐ, ์ดํด๋ฆฝ์ค ํ๊ฒฝ์์ ๋ง๋๋ ๋ฒ์ ์ ๋ฆฌํ๊ณ ์ ํ๋ค.
new > Maven Project
๋น Maven Project ๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์์ฃผ ๊ฐ๋จํ๋ ์๋ตํ๊ณ … ๋ง๋ค๊ฒ ๋๋ฉด 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>com.example</groupId>
<artifactId>boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
๊ทธ๋ฌ๋ฉด ์ด ๋น์ด์๋ pom.xml ์ Spring-Boot ์ ํ์ํ ์ค์ ๋ค์ ์ถ๊ฐํด์ฃผ๊ธฐ๋ก ํ๋ค.
<parent> <!--boot์ ์คํํฐ๋ฅผ ์ฌ์ฉํ๊ฒ ๋ค๊ณ ๋ช
์์ ์ผ๋ก ์ค์ -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<dependency> <!--boot์์ ์คํํฐํจํค์ง๋ก ์ ๊ณตํด์ฃผ๋ ๊ฒ๋ค์ค์ web ์ค์ ๋ถ๋ถ -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
๊ทธ๋ค์ ์์์ java ํด๋์ค๋ฅผ ํ๋ ๋ง๋ค๊ณ ๊ฑฐ๊ธฐ์ ์๋์ฒ๋ผ ์ค์ ํ๋ฉด ๋
@SpringBootApplication // @Configuration + @EnableAutoConfiguration + @ComponentScan ๋ค์ ์ข
ํฉ ์ด๋
ธํ
์ด์
public class TestApplication{
public static void main(String[] args) throws Exception {
SpringApplication.run(TestApplication.class, args);
}
}
Spring Boot ์์๋ ๋ด์ฅWAS๋ฅผ ๊ฐ์ง๊ณ ์๊ธฐ ๋๋ฌธ์ main ๋ฉ์๋์์ ์ฐํด๋ฆญํ run AS โ Spring Boot App
์ ์ ํํด์ฃผ๋ฉด 8080ํฌํธ๋ก ๋์์ง๊ฒ ๋๋ค.
new > Spring Starter project
(STS๊ฐ ์ค์น๋์ด์๋ค๋ ๊ฐ์ ํ์)์ด ๋ฉ๋ด๋ฅผ ์ฌ์ฉํ๋ฉด ์์์ ํ๋ ์ผ๋ จ์ ์ค์ ๋ค์ ์๋์ผ๋ก ํด์ฃผ๊ฒ ๋๋ค. ๊ฐ๋จํ ๋ด์ฉ์ด๋ next๋ฅผ ํด์ฃผ๋ค ๋ง์ง๋ง์ Dependencies
์ค์ ํ๋ ๋ถ๋ถ์์ Web ์ ์ฒดํฌํด์ฃผ๊ณ Finish ๋ฒํผ
์ ๋๋ฅด๋ฉด ๋
Spring Initializr (start.spring.io)
http://start.spring.io/ ์ ๋ค์ด๊ฐ๋ณด๋ฉด ๊ตฌ์ง ์ค๋ช
ํ์ง ์์๋ ์น์ ํ๊ฒ Generate ํด์ฃผ๋ ํ์ด์ง๊ฐ ๋ณด์ธ๋ค. ์ฌ๊ธฐ์ web ์ Dependencies
์ ์ถ๊ฐํ๊ณ Generate๋ฅผ ํ๋ฉด ํด๋น ํ๋ก์ ํธ๊ฐ ์์ถ๋ ์ํ๋ก ๋ค์ด์ด ๋ฐ์์ง๊ฒ ๋๊ณ ์ด๋ฅผ IDE ์์ ์ด์ด๋ณด๋ฉด ์์์ ํ๋ ์ผ๋ จ์ ๊ณผ์ ๋ค์ด ์ค์ ๋์ด ์๋๊ฒ์ ํ์ธํด๋ณผ์๊ฐ ์๋ค.
๋ด์ฅํฐ์ผ์ ์ฌ์ฉ์ํ๊ณ ๋ณ๋ ํฐ์ผ์ ์ฌ์ฉํด์ผ ํ๋ ๊ฒฝ์ฐ
Spring boot๋ ์์ฒด์ ์ผ๋ก ๋ด์ฅ WAS๋ฅผ ๊ฐ์ง๊ณ ์๋ค. ํ์ง๋ง ๊ด๋ฆฌํฌ์ธํธ๋ ์ด๋ฐ์ ๋ฐ ์ด์ ๋ก ๋ด์ฅํฐ์ผ์ ์ฌ์ฉํ์ง ๋ชปํ๋ ํ๊ฒฝ์ด๋ผ๋ฉด ๋ค์๊ณผ ๊ฐ์ ์ค์ ์ ํด์ฃผ๋ฉด ๋๋ค.
- ์ผ๋ฐ์ ์ผ๋ก ๋น๋๊ฐ ๋๋ฉด
jar
๋ก ๋ง๋ค์ด ์งํ ๋ฐwar
๋ก ๋น๋ ๋๋๋ก ์์ ์ ํด์ผํ๋ค. (was๊ฐ WAR๋ฅผ ๋ฌผ๊ณ ๋ ์ผํ๊ธฐ ๋๋ฌธ..)
<packaging>war</packaging>
- dependency ์ tomcat์ ์ถ๊ฐํด์ค๋ค.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
- ์๋์ฒ๋ผ main ๋ฉ์๋๊ฐ ์๋ ํด๋์ค์
SpringBootServletInitializer
๋ฅผ ์์๋ฐ๊ฒ ํ ํconfigure
๋ฉ์๋๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉ ํด์ค๋ค.
@SpringBootApplication
public class TestApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(TestApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(TestApplication.class, args);
}
}
- ํฐ์ผ์ ๋์ฐ๊ธฐ ์ํ์ฌ ํ๋ก์ ํธ ์ค์ (Project Facets)์์
Dynamic Web Module
์ ์ฒดํฌํด์ค๋ค.
์ฐธ๊ณ URL