Nice programing

Spring Boot 애플리케이션의 환경 별 application.properties 파일

nicepro 2020. 11. 26. 19:52
반응형

Spring Boot 애플리케이션의 환경 별 application.properties 파일


내 Spring Boot 애플리케이션에서 환경 별 속성 파일을 만들고 싶습니다. 전쟁에서 내 응용 프로그램의 패키징 유형이며 임베디드 Tomcat에서 실행하고 있습니다. 나는 sts를 사용하고 sts 자체에서 메인을 실행합니다.

  1. application-$ {env-value} .properties와 같은 환경 특정 속성 파일을 가질 수 있습니까?

위의 경우 env-value는 local / devl / test / prod와 같은 값을 갖습니다.

  1. env-value 파일은 어디에 설정합니까? 로컬의 경우 sts를 통해 jvm 인수로 설정할 수 있습니다.

  2. Spring Boot 애플리케이션에서 application.properties를 읽는 사람.

  3. 환경 별 속성 파일을로드하는 방법은 무엇입니까? 예를 들어-환경 특정 속성 파일에서 데이터베이스 uid, pwd, 스키마 등을 설정하면 데이터 소스가 속성을 이해할 수 있습니까?

  4. application.properties와 application-local.properties 파일을 동시에 사용할 수 있습니까?


봄 부팅 이미 지원이 에 대한 프로필 을 기반으로 속성을.

application-[profile].properties파일을 추가 하고 spring.profiles.active속성 을 사용하여 사용할 프로필을 지정하기 만하면 됩니다.

-Dspring.profiles.active=local

이것은로드 application.properties와를 application-local.properties처음부터 후자의 최우선 속성.


그래 넌 할수있어. Spring을 사용하고 있으므로 @PropertySource 주석을 확인하십시오.

구성에 주석 달기

@PropertySource("application-${spring.profiles.active}.properties")

원하는대로 호출 할 수 있으며 원하는 경우 여러 속성 파일을 추가 할 수 있습니다. 모든 환경에 속하는 더 많은 세트 및 / 또는 기본값이 있으면 좋을 수 있습니다 (@PropertySource {..., ..., ...}로도 작성할 수 있음).

@PropertySources(
  @PropertySource("application-${spring.profiles.active}.properties"),
  @PropertySource("my-special-${spring.profiles.active}.properties"),
  @PropertySource("overridden.properties"))

그런 다음 환경으로 응용 프로그램을 시작할 수 있습니다.

-Dspring.active.profiles=test

이 예에서 이름은 application-test-properties 등으로 대체됩니다.


다음과 같이 할 수 있습니다.

application.yml :

spring:
  profiles:
    active: test //modify here to switch between environments
    include:  application-${spring.profiles.active}.yml

에서 응용 프로그램 test.yml :

server:
  port: 5000

그리고 application-local.yml에서 :

server:
  address: 0.0.0.0
  port: 8080

그러면 스프링 부트가 원하는대로 앱을 시작합니다.

참고 URL : https://stackoverflow.com/questions/32196451/environment-specific-application-properties-file-in-spring-boot-application

반응형