[Spring Boot] 1. 스프링부트 시작

Intro

레퍼런스 intro 요약

  • toy 수준이 아닌 제품수준(production grade)의 독립 어플리케이션을 빠르고 쉽게.
  • Spring Boot는 일반적으로 나름 널리 쓰인다는 설정을 기본적으로 제공
  • Spring Framework 및 써드 파티까지의 기본 설정도 기본 제공(예로 톰캣)-컨벤션

Goal

  • 모든 스프링 개발시 더 빠르고 폭넓은 사용성 제공
  • 일일히 설정하지 않아도 컨벤션으로 설정된 설정을 제공

하지만 언제나 사용자가 원하는대로 빠르고 쉽게 설정 변경 가능

  • non-functional feature 제공
    • 임베디드 서버 설정 보안, 헬스 체크등 비지니스 로직과 먼 것들의 설정
    • 이런 non-functional feature들을 제공하여 신경안쓰이게

Requirement

Spring Boot 2.0.3 기준

  • Java 8 이상
  • Spring Framework 5.0.7 이상
  • Servlet 3.1 이상
    • 지원 컨테이너:
      • Tomcat 8.5 이상
      • Jetty 9.4 이상
      • Undertow 1.4

Start

스프링 사이트에 보면 자바 개발자를 위한 설치 레퍼런스가 있다.
https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#getting-started-installation-instructions-for-java
maven과 gradle방식을 제공하며 기본 파일 내용도 제공한다.

1) Maven

maven의 경우 3.2 이상 버전과 호환된다.
스프링 부트의 의존성은 groupId로 org.springframework.boot를 사용한다.
일반적으로 ide에서 스프링 부트 스타터등으로 기본 프로젝트를 만드는 경우에는 spring-boot-starter-parent라는 걸 쓰며 여기에 부가적인 스타터들이 추가되어있다.
스프링 부트는 실행 가능한 jar를 만들수 있는 선택가능한(optional) maven 플러그인도 제공한다.

pom.xml
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
33
34
35
<?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.3.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>

spring-boot-starter-parent는 스프링부트를 사용하는 훌륭한 방법이지만,
항상 옳지만은 않다.
때때로 사용자는 다른 부모 pom.xml을 상속받아야하고
때때로 기본 설정이 아니라 모든 설정을 일일히 정의하고 싶을 수도 있다.

이 때 import 스코프를 사용하여 spring-boot-starter-parent를 사용하지 않는 방법이 있다.(이 경우 의존성 관리는 되나 플러그인 관리의 이점을 잃는다.) 필요하면 다음 링크를 이용하자.- 추후 연습할때 링크부분 공부하고 써넣던지
https://docs.spring.io/spring-boot/docs/2.0.3.RELEASE/reference/htmlsingle/#using-boot-maven-without-a-parent

2) Gradle

그래들은 불행하게도 4버전 이상부터 지원한다.
3버전부터 많이 달라져서.. 계속 2.14버전을 고수 했던 나에게는
청천 병력같은 날벼락… ㅜㅜ
이 기회에 늦장 부리던 공부를 다시 하면 되지…

나는 새로 배우는건 좋은데
원래 있던 게 api 바뀌거나 구조가 바뀌에서
별 이점을 얻지 못하는데 새로 공부하는 것이 싫다.

build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
plugins {
id 'org.springframework.boot' version '2.0.4.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")
}

그래들 공식 사이트에서 나온 설정은 위와 같지만
실제로 이렇게 하면 @SpringBootApplication 이 안먹는다.
그래들 구버전의 문법을 쓰면 먹는데
왜 공식적인 신 문법이 안먹는지 연구가 필요하다.
(구글에서 검색해서 나온 것들은 전부 구 문법;)

위의 방법이 어렵다면 https://start.spring.io를 가면 쉽게 해결할 수 있다.

이 site에서 메타 데이터를 넣어주면 프로젝트 형태로 뽑아준다.
나의 경우에는 그래들 프로젝트에 JPA,WEB dependency를 선택했다.
이 경우 build.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
31
buildscript {
ext {
springBootVersion = '2.0.4.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'kr.rkaehdaos'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
mavenCentral()
}


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

이렇게 하면 이제 @SpringBootApplication도 작동되는 정상적인 스프링 부트 프로젝트가 만들어진다.

이제 테스트를 해보자.

Application1.java
1
2
3
4
5
6
7
8
9
10
11
package kr.rkaehdaos;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application1 {
public static void main(String[] args) {
SpringApplication.run( Application1.class, args);
}
}

이렇게만 하고 run하면 톰캣 플러그인 까지 자동 구동되어서
8080port 로 ready 상태가 된다.
실제로 browser로 8080에 접속해보면
매핑된 컨트롤러가 아직 없어서 에러가 나지만
console message를 보면 분명히 톰캣까지 서비스 구동이 되고..
에러메세지도 보면 컨트롤러가 작동하여 request를 처리하고
거기서 에러로 예외처리하여 에러메세지를 response로 띄웠다고 할 수 있다.

스프링 부트 프로젝트의 구조

사실 구조랄 것까지도 없고.. 기존 메이븐 , 그래들 프로젝트와 동일하다.
즉 기존 프로젝트나 기존 스프링 프로젝트와 동일한 디렉터리를 가진다.

다만 중요한 점은 저 위에 있던 @SpringBootApplication으로 만든 어플리케이션 스타트 클래스(Main Application Class)는 최상위 패키지에 있어야한다.

그냥 이렇게만 알고 사용해도 무방하지만 이유를 알면 더 좋겠지.
저 어노테이션 정의를 IDE를 사용해서 소스를 살펴보면..

SpringBootApplication.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

/**
* Indicates a {@link Configuration configuration} class that declares one or more
* {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration
* auto-configuration} and {@link ComponentScan component scanning}. This is a convenience
* annotation that is equivalent to declaring {@code @Configuration},
* {@code @EnableAutoConfiguration} and {@code @ComponentScan}.
*
* @author Phillip Webb
* @author Stephane Nicoll
* @since 1.2.0
*/

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
...
}

여기서 @ComponentScan을 발결한 수 있다.
메인 어플리케이션 클래스가 위치한 패키지부터 하위 패키지를 스캔하고
빈으로 등록할수 있는 부분을 전부 빈으로 등록하게 되는데..
만약 패키지없이 그냥 main/java에 들어있다면?
그러면 말그대로 “모든 패키지”를 전부 빈으로 등록되어 난리

스프링 사이트의 https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-structuring-your-code에 들어가면 이처럼 권장 프로젝트 구조가 나온다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
com
+- example
+- myapplication
+- Application.java
|
+- customer
| +- Customer.java
| +- CustomerController.java
| +- CustomerService.java
| +- CustomerRepository.java
|
+- order
+- Order.java
+- OrderController.java
+- OrderService.java
+- OrderRepository.java

위처럼 작성하면 customer와 order등에 있는 컴포넌트 전부 스캔된다.

Related POST

공유하기