spring boot-summary(part2-part3)
TRANSCRIPT
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/
Spring boot 참조 설명서 요약Part II - Getting Started
Part III - Using Spring Boot
2016-10-dd
( 스프링 ) 프로젝트 셋업 힘들죠 ?
• 문제 : 번잡스러운 사전 설치 작업들• 목표 : Spring 기반의 stand-alone,
production-grade 앱 개발을 쉽게 시작할 수 있게 만들자• 방법 : ( 의도적으로 ) 편향된 관점으로 Spring 및
3rd 파티 모듈들을 구성 — > Spring Boot
Build artifacts ( 산출물 )
• java -jar 로 바로 실행 가능한 jar• Tomcat 에 바로 배포 가능한 war• Spring Boot CLI 를 이용한 실행도 가능
Spring Boot 의 목표• 스프링 기반 개발을 쉽게 시작• 처음에 쉽게 시작할 수 있는 구성 / 이후 쉬운
tailoring• 비기능성 feature 포함
• embedded servers, security, metrics, health checks, externalized configuration
• No No No - Code 생성 , XML 설정
System Requirements• Java 7 and 8 (6 도 가능 — 설정필요 )• Maven (3.2+), Gradle (1.12 or 2.x), Gradle 3 은 미지원• Spring Framework 4.3.3.RELEASE 이상• ‘ 건전지 포함’ (Servlet containers, embedded DBs, …)• (Optional) Spring Boot CLI - groovy DSL
• 명령창에서 javac, java 를 직접 사용하는 대신에• $ spring run app.groovy 처럼 사용할 수 있어• Gradle 과 중복되는 부분이 있지만… rails 같은 모습을 추구하는 듯
설치
buildscript { repositories { mavenCentral() } dependencies { classpath(“org.springframework.boot:spring-boot-gradle-plugin:1.4.1.RELEASE") }}apply plugin: 'java'apply plugin: 'spring-boot'jar { baseName = ‘myproject’; version = ‘0.0.1-SNAPSHOT' }repositories { mavenCentral() }dependencies { compile("org.springframework.boot:spring-boot-starter-web") testCompile("org.springframework.boot:spring-boot-starter-test")}
• Maven 이나 Gradle 이용해서 의존성 관리로 설치할 것 권장 ( 이하 Gradle)• Spring Boot 의존성들은 org.springframework.boot 그룹에 속한다 .• spring-boot-gradle-plugin 은 spring boot 를 쉽게 쓸 수 있게 하는
Gradle plugin• 처음에는 몇 개의 “ starter” 들을 사용하게 됨 (spring-boot-starter-*)
헬로 월드 - build.gradle• $ mkdir spring-boot-hello && cd spring-boot-hello• $ {EDIT} build.gradle
buildscript { repositories { mavenCentral() } dependencies { classpath(“org.springframework.boot:spring-boot-gradle-plugin:1.4.1.RELEASE") }}
apply plugin: 'java'apply plugin: 'spring-boot'
jar { baseName = ‘hello' }
repositories { mavenCentral() }dependencies { compile("org.springframework.boot:spring-boot-starter-web") testCompile("org.springframework.boot:spring-boot-starter-test")}
헬로 월드 - java source• $ mkdir -p src/main/prj/hello && cd
src/main/java/prj/hello• $ {EDIT} Hello.java
package prj.hello;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;
@RestController@EnableAutoConfigurationpublic class Hello {
@RequestMapping("/") String home() { return "Hello World!"; }
public static void main(String[] args) throws Exception { SpringApplication.run(Hello.class, args); }
}
헬로 월드 - build & run & test• ( 빌드 ) $ gradle build• ( 실행 ) $ java -jar build/libs/hello.jar 혹은 $ gradle bootRun ( 내장 톰캣으로 서비스 )• ( 테스트 ) $ curl http://localhost:8080/ ( 응답 : “Hello World”)
Spring Boot Starters• spring-boot-starter• spring-boot-starter-test• spring-boot-starter-logging• spring-boot-starter-web• spring-boot-starter-thymeleaf• spring-boot-starter-data-jpa• spring-boot-starter-security• … and many more
@EnableAutoConfiguration• package
org.springframework.boot.autoconfigure• 사용 예상 bean 들을 Spring 설정에 추가
(dependencies jars)• starter 외의 jar 에 대해서도 충분히 똑똑하게 처리• exclude={…}, excludeName={…} 필드로 명시 가능• app 별로 하나만 지정 가능 ( 보통 Class with main())
@SpringBootApplication
• package org.springframework.boot.autoconfigure• @SpringBootConfiguration
• aliased @Configuration for Spring Boot App• + @EnableAutoConfiguration• + @ComponentScan
main() Class 의 위치• @SpringBootApplication 으로 명시적으로 main() Class 지정할 수 있지만• app 의 root package 에 위치 시키면 편함 ( 아래 예에서는
Application.java)• @EnableAutoConfiguration 이나 @ComponentScan 에
base search package 지정을하지 않아도 main() Class 위치를 기본으로 택하기 때문com +- example +- myproject +- Application.java | +- domain | +- Customer.java | +- CustomerRepository.java | +- service | +- CustomerService.java | +- web +- CustomerController.java
차츰 Auto-configuration 대체 ( 해치지 않아요 )• ( 예 ) 직접 ‘ DataSource’ 빈을 추가하면 기본 제공되던 것은 더 이상 사용되지 않음• 앱 시작시 — debug 옵션 지정하여 auto-configuration 관련 로그에서 확인 가능• @EnableAutoConfiguration(exclude={}) 이용• 혹은 spring.autoconfigure.exclude 프로퍼티 이용
import org.springframework.boot.autoconfigure.*;import org.springframework.boot.autoconfigure.jdbc.*;import org.springframework.context.annotation.*;
@Configuration@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})public class MyConfiguration {}
이제 관심있는 주제별로 하나씩• Part IV. Spring Boot features
• SpringApplication / Externalized Configuration / Profiles / Logging / Test• Spring Web MVC framework• Working with SQL databases (JPA)• Distributed Transactions with JTA• …
• Part V. Spring Boot Actuator: Product-ready features• Part VI. Deploying Spring Boot applications• Part VII. Spring Boot CLI• Part VIII. Build tool plugins• Part IX. ‘How-to’ guides ( 세부 타스크별 handbook 성 정보 )• Part X. Appendix (application property 항목 정리 , 각종 정보 포맷등 테이블성 정보 )