최근 스프링 부트(Spring Boot)로 사이드 프로젝트를 진행하고자 짧게 예제를 만들어 보았다.
추후 참고용으로 사용하고자 포스팅하기로 했다.
참고로 아래 포스팅 내용은 여기의 내용을 참조하였다.
JDK 및 Gradle 설치 확인
1 2
   | java -version gradle --version
   | 
 
아래 내용은 Oracle JDK 1.8, 그래들(Gradle) 4.5 버전 기준으로 작성했으며, 운영체제는 우분투 16.04를 사용하고 있다.
Gradle init
1 2 3
   | mkdir spring-boot-example cd spring-boot-example gradle init
   | 
 
먼저 예제로 사용할 디렉터리를 생성하고, gradle init 명령을 실행하여 gradle에 관련된 기본 파일들을 생성했다.
build.gradle 내용 수정
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
   | buildscript {     repositories {         mavenCentral()     }     dependencies {         classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.10.RELEASE")     } }
  apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'idea' apply plugin: 'org.springframework.boot'
  jar {     baseName = 'gs-spring-boot'     version =  '0.1.0' }
  repositories {     mavenCentral() }
  sourceCompatibility = 1.8 targetCompatibility = 1.8
  dependencies {     compile("org.springframework.boot:spring-boot-starter-web")     testCompile("junit:junit") }
  | 
 
그래들 빌드 설정으로 사용하는 build.gradle에 위 내용을 추가했다. 타입을 지정하지 않고 gradle init을 실행하여 생성한 build.gradle 파일에는 주석을 제외한 내용이 없는 깨끗한 상태이므로 위 내용을 그대로 추가하기만 하면 된다.
Java 소스 추가
소스 디렉터리 추가
1
   | mkdir -p src/main/java/hello
   | 
 
HelloController.java 생성
1 2 3 4 5 6 7 8 9 10 11 12 13
   | package hello;
  import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping;
  @RestController public class HelloController {
      @RequestMapping("/")     public String index() {         return "Greetings from Spring Boot!";     } }
   | 
 
Application.java 생성
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
   | package hello;
  import java.util.Arrays;
  import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean;
  @SpringBootApplication public class Application {
      public static void main(String[] args) {         SpringApplication.run(Application.class, args);     }
      @Bean     public CommandLineRunner commandLineRunner(ApplicationContext ctx) {         return args -> {
              System.out.println("Let's inspect the beans provided by Spring Boot:");
              String[] beanNames = ctx.getBeanDefinitionNames();             Arrays.sort(beanNames);             for (String beanName : beanNames) {                 System.out.println(beanName);             }
          };     } }
   | 
 
예제로 사용할 소스 디렉터리 및 파일을 생성했다.
빌드 또는 실행
빌드
위의 그래들 빌드 명령을 실행하면 build/libs/ 디렉터리 밑에 jar 파일이 생성된다.
실행
또는 바로 위의 명령을 실행하면 http://localhost:8080로 접근할 수 있다. 바로 확인하고자 할 때에는 위 bootRun 명령을 사용하면 된다.
실행 확인
웹 브라우저로 http://localhost:8080에 접근하면 Greetings from Spring Boot!라는 문자열이 출력된 것을 확인할 수 있다.
이것으로 그래들로 스프링 부트 시작하기 간단 예제는 종료.