본문 바로가기
기술 부채 상환 중....

스프링 부트 2. Getting Started

by 닮은 2018. 5. 2.

원문

시작하기

시작하기.

8. 스프링 부트 소개

스프링 부트는 독립적인 프로덕션 수준의 스프링 기반 어플리케이션을 쉽게 만들수 있게 해준다.

9. 시스템 요구사항

스프링 부트 2.0.1.RELEASE는 자바 8(또는 9)과 스프링 프레임워크 5.0.5.RELEASE 또는 이상을 필요로 한다. 메이븐 3.2 이상 그리고 그레들 4에서 제공된다.

10. 스프링 부트 설치하기

spring-boot-*.jar를 클래스 패스에 포함시켜서 사용하며 된다.

자바 개발자를 위한 설치 지침

maven

pom.xml에서 spring-boot-starter-parent를 상속받고 하나 이상의 stater를 의존성에 추가해준다.

<?xml version="1.0" encoding="UTF-8"?>
<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>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

gradle

스프링 부트가 제공하는 플러그인으로 간단하게 의존성을 추가하고 실행가능한 jar를 만들 수 있다.

plugins {
    id 'org.springframework.boot' version '2.0.1.RELEASE'
    id 'java'
}

jar {
    baseName = 'myproject'
    version =  '0.0.1-SNAPSHOT'
}

repositories {
    jcenter()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

Spring Boot CLI 설치하기

(통과)

Srping Boot 이전 버전에서 업그레이드 하기

마이그레이션 가이드 참고

11. 첫번째 스프링 부트 어플리케이션 개발하기

pom 생성

<?xml version="1.0" encoding="UTF-8"?>
<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>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <!-- Additional lines to be added here... -->

</project>

클래스패스 의존성 추가

spring-boot-stater-parent는 유용한 메이븐 기본값을 제공하는 특별한 스타터이다. dependency-management로 의존성의 버전을 생략할 수도 있다.

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

코드 작성하기

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Example {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Example.class, args);
    }

}

@RestController, @RequestMapping

@RequestMapping 어노테이션은 라우팅 정보를 제공한다. @RestController 어노테이션은 콜러에서 직접 결과 문자열을 돌려주도록 스프링에게 지시한다. 이 둘은 spring MVC 어노테이션으로 MVC 섹션에서 더 자세히 보도록 한다.

@EnableAutoConfiguration

@EnableAutoConfiguration 어노테이션은 추가한 jar를 기반으로 어떻게 스프링을 설정할 것인지 스프링 부트가 추측하도록 지시한다. spring-boot-stater-web은 톰캣, 스프링 MVC를 추가하기 때문에 자동-설정은 웹 어플리케이션을 개발한다고 예상하고, 그에 따라 스프링을 set up 한다.

"main" 메소드

어플리케이션의 entry point 이다. 메인 메소드는 스프링 부트의 SpringApplication 클래스를 run 하여 위임한다. SpringApplication 클래스는 어플리케이션을 부트스트랩하고, 스프링을 시작하고 자동 설정된 톰캣 웹 서버를 시작한다. 인자로 Example.class와 커맨드 라인 인자인 args를 넘긴다.

예제 실행하기

$ mvn spring-boot:run

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v2.0.1.RELEASE)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.222 seconds (JVM running for 6.514)

실행가능한 jar 생성하기

실행가능한 jar란, 코드를 실행하는데 필요한 모든 의존성을 포함하여 컴파일된 아카이브이다. 이것을 만들기 위해서는 spring-boot-maven-pluginpom.xml에 포함해줘야한다. 이렇게 하면 spring-boot-stater-parent의 pom에 있는 <executions> 설정을 포함하고, repackage 골에 바인딩 되어 있다. 만약 spring-boot-stater-parent을 상속받지 않는다면 직접 설정해 주어야 한다. 플러그인 문서

mvn package 패키징을 하면 jar 파일이 target 디렉토리 밑에 생성된다. java -jar target/***.jar로 실행할 수 있다.

12. 다음 읽을 내용 (생략)