[Spring Boot] spring.profiles.active 인자 전달법 2가지

식빵·2023년 4월 23일
0

Spring Lab

목록 보기
21/33
post-thumbnail

Spring boot app 을 bash(또는 cmd, powershell 등)를 통해서 실행 시에
spring.profiles.active 인자를 전달할 수 있습니다.

그런데 이 전달 방법을 제대로 알지 못하면 원하는 동작이 안될 수 있습니다.
한번 이 기회에 제대로 알고 넘어가 보죠. 참고로 여기서 작성된 글은 아래 링크에
있는 내용을 가져온 것입니다.

Setting active profile and config location from command line in spring boot



Spring Boot Jar Argument

Option 1: Java System Properties (VM Arguments)

java -jar -Dspring.profiles.active=prod application.jar
  • 참고: 코드 내에서 System.getProperty("spring.profiles.active") 로 조회할 수 있습니다.

Option 2: Program arguments

java -jar application.jar --spring.profiles.active=prod --spring.config.location=c:\config

Option1, Option2 차이점

java -jar -D{여기에 VM argument 작성} test.jar {여기에 program argument 작성}



참고: VM Argument vs Program Argument

참조한 글spring boot 와 관련된 내용이다 보니 program argument 앞에 -- 같은 prefix 는 붙였는데, 이건 어디까진 스프링 애플리케이션 내부에서 -- 를 쓰는 방식을 미리 지정해서 그런 겁니다.

Spring Boot 에서는 이런 -- 가 붙는 인자를 Option Argument 라고 합니다.
참고: ApplicationArguments javaDoc

그러므로 단순한 jar 파일 실행 시에는 -- 가 반드시 붙어야 되는게 아닙니다!
만약 vm option 과 program argument 의 명확한 차이를 직접 알고 싶다면
아래 링크를 참조하시기 바랍니다.

참고 링크:
What's the difference between program arguments and VM arguments?

profile
백엔드를 계속 배우고 있는 개발자입니다 😊

0개의 댓글