-
application.properties 란?Spring/Spring Boot 2020. 8. 29. 19:21
- resources 디렉터리 밑에 있게 되는 application.properties 나 application.yaml 파일은
- 스프링 부트가 자동으로 로딩하게 되는 규약들이다.
- 이러한 프로퍼티들을 사용하는 방법은 여러 가지이며 각 사용마다 우선순위가 다르므로
- 오버 라이딩될 수도 있다.
다음은 프로퍼티에 우선순위이다.
- 1. 유저 홈 디렉터리에 있는 spring-boot-dev-tools.properties
- 2. 테스트에 있는 @TestPropertySource
- 3. @SpringBootTest 애노테이션의 properties 애트리뷰트
- 4. 커맨드 라인 아규먼트
- 5. SPRING_APPLICATION_JSON (환경 변수 또는 시스템 프로퍼티)에 들어있는 프로퍼티
- 6. ServletConfig 파라미터
- 7. ServletContext 파라미터
- 8. java:comp/env JNDI 애트리뷰트
- 9. System.getProperties() 자바 시스템 프로퍼티
- 10. OS 환경 변수
- 11. RandomValuePropertySource
- 12. JAR 밖에 있는 특정 프로파일용 application properties
- 13. JAR 안에 있는 특정 프로파일용 application properties
- 14. JAR 밖에 있는 application properties
- 15. JAR 안에 있는 application properties
- 16. @PropertySource
- 17. 기본 프로퍼티 (SpringApplication.setDefaultProperties)
- 이 resources 밑에 있는 application properties는 15번째 우선순위를 가지게 된다.
- 그리고 다음과 같이 my.name은 seungyeol이지만
- 4순위의 커맨드 라인 아규먼트를 이용해서 my.name을 none으로 오버 라이딩할 수도 있다.
- 다음의 코드는 3순위 코드이다. 코드를 실행하면 결국 가장 우선순위가 높은 baekseung이 된다.
- 만약 테스트용으로 application.properties를 따로 만들고 싶다면 다음과 같이
- test - resources - application.properties를 만들어 주면 된다.
- 이렇게 하는 것이 가능한 이유는
- 테스트 코드를 실행하기 전에 컴파일이 되는데 src 밑에 있는 파일들이 classpath에 들어가게 된다.
- 그 다음에 테스트 코드를 컴파일해서 classpath에 넣게 된다. 이때 application.properties가
- Test에 있는 것으로 바뀌게 된다.
이때 오버라이딩 외에 src에 properties는 테스트 properties에 없기 때문에 따로 정의해주어야 한다. src properties를
그대로 사용하면서 test properties를 사용하려면 이름을 다르게 해 주면 된다. ex ) test-properties
이런 외부설정들을 사용하는 방법은
Environment 클래스에서 environment.getProperty를 이용하는 방법
@Value를 이용하는 방법 등이 있다.
application.properties는 위치에 따라서도 우선순위가 나뉘는데
1. file:./config/
2. file:./
3. classpath:/config/
4. classpath:/
다음과 같다.
더 자세한 내용은 링크 참조
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config
Spring Boot Reference Documentation
This section goes into more detail about how you should use Spring Boot. It covers topics such as build systems, auto-configuration, and how to run your applications. We also cover some Spring Boot best practices. Although there is nothing particularly spe
docs.spring.io
Spring Boot Reference Documentation
This section goes into more detail about how you should use Spring Boot. It covers topics such as build systems, auto-configuration, and how to run your applications. We also cover some Spring Boot best practices. Although there is nothing particularly spe
docs.spring.io
'Spring > Spring Boot' 카테고리의 다른 글
[springboot, kotlin] 배포시 FileReader FileNotFoundException (0) 2021.09.28 @ConfigurationPropertiesScan 과 생성자 바인딩 (1) 2020.12.15 Spring Boot mockMvc 한글 깨짐 처리 (0) 2020.07.31 RestTemplate, WebClient (0) 2020.07.13 JSR-303 Errors들을 Json으로 Serialization 해보자 (0) 2020.06.25