일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 냥스토리
- 고양이
- 안동국시
- 스테이크
- CDJ
- CodeJam 2017 Round 1B
- 카페
- 파버스
- 발산
- 치명적 귀여움
- 냥이
- 발산맛집
- coffee
- codejam
- 고양이는 언제나 귀엽다
- 레스토랑
- 소호정본점
- 발산역 근처 카페
- 소호정
- 스코티쉬 스트레이트
- 먹기좋은곳
- 스파게티
- 데이트
- 냥냥
- A. Steed 2: Cruise Control
- 부모님과
- RED CAT COFFEE X LOUNGE
- 커플
- 파머스테이블
- 양재맛집
- Today
- Total
hubring
Spring boot - properties 분리 본문
Spring에서 Spring boot로 넘어오면서 좋았던 점은 XML에 복잡하게 설정할 필요 없이
자동 구성을 지원하여 appliction.properties(혹은 application.yml)에서 설정 값만 명시해주면 되는 것이었다.
하지만 이것저것 라이브러리들을 추가함에 따라 설정하는 속성 값이 많아져 기존에 Spring xml 파일을 목적에 따라 분리하여 사용했듯 properties 역시 분리하고 싶어 졌다.
물론 Config 클래스를 이용하여 목적에 따라 생성하면 되지만...
자동 구성에 대한 편리함이 있기에 properties를 분리하여 사용하고자 한다.
특히 JDBC와 같은 경우 설정해야 할 항목도 많고 여러 Database를 사용하는 경우도 있기 때문에 이번 포스팅에서 분리하는 방법을 다루기로 했다.
분리 방법은 간단하다.
1. resource 경로 및에 jdbc.properites를 작성한다.
#Database Configuration
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.initialize=false
spring.datasource.continueOnError=true
spring.datasource.max-active=10
spring.datasource.max-idle=10
spring.datasource.min-idle=1
spring.datasource.max-wait=-1
spring.datasource.initial-size=1
spring.datasource.test-on-borrow=true
spring.datasource.test-while-idle=true
spring.datasource.validation-query=
#5 minutes
spring.datasource.time-between-eviction-runs-millis=300000
2. application 클래스에 해당 파일의 경로를 추가한다.
@SpringBootApplication
@PropertySource(value = { "classpath:jdbc.properties" })
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
이렇게 하면 쉽게 분리 완료할 수 있다.
+
추가적으로 여러 개의 properties를 설정하고 싶은 경우
@PropertySource 속성 값을 여러개 설정하거나. ( Java8 이상 가능 )
@PropertySource("classpath:foo.properties")
@PropertySource("classpath:bar.properties")
public class PropertiesWithJavaConfig {
//...
}
@PropertySources를 사용하여 설정할 수 있다. (모든 Java 버전에서 가능)
@PropertySources({
@PropertySource("classpath:foo.properties"),
@PropertySource("classpath:bar.properties")
})
public class PropertiesWithJavaConfig {
//...
}
만약 각 파일에 중복된 설정 값이 있는 경우 마지막 @propertySource 파일을 기준으로 설정 값이 사용되어진다.
Spring의 프로퍼티를 제어하는 보다 자세한 방법은 아래 가이드를 참고하세요~
https://www.baeldung.com/properties-with-spring
Properties with Spring and Spring Boot | Baeldung
Tutorial for how to work with properties files and property values in Spring.
www.baeldung.com
'Spring Boot' 카테고리의 다른 글
직접 빌드하기 (0) | 2020.08.04 |
---|---|
Spring Boot Documentation (0) | 2020.08.04 |
IntelliJ Gradle 대신 자바 직접 실행 (0) | 2020.08.04 |
Spring boot - Security (0) | 2020.07.23 |
Spring Boot - Swagger 적용 (0) | 2020.07.11 |