관리 메뉴

JIE0025

개발환경에 따른 application.properties 환경 파일 구성 방법 본문

Application/Spring, SpringBoot, JPA, Webflux

개발환경에 따른 application.properties 환경 파일 구성 방법

sdoaolo 2025. 5. 28. 19:00
728x90

 

✅ application.properties

애플리케이션 구성에서 사용할 수 있는 공통 애플리케이션 속성이다. 

 

✔  DB연결 정보, 로그 파일 경로,  서버 포트, 캐싱관련 속성, 보안 설정 등 

다양한 설정을 이 파일에서 지정한다 .

 

 

✔   키값 쌍의 형태로 구성정보를 저장한다. 

spring.jpa.defer-datasource-initialization=true
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create

 

 

 

 

✅ 프로퍼티 파일의 위치

기본적으로  src/main/resources 폴더에 위치한다. 

https://www.geeksforgeeks.org/spring-boot-application-properties/

 

 

이 경로에 넣으면 설정이 변경될 때마다  .jar 파일 생성을 위해  빌드를 다시 해주어야 한다. > 불편하다!!

 

그래서 .jar가 존재하는 위치나  프로젝트 최상단에   config 디렉터리를 생성해 그 내부에 설정파일을 넣어준다.

 

 


✅ 프로퍼티 파일의 위치 

 

애플리케이션이 시작될때   프로퍼티 파일을 찾는 위치는 다음과 같다. 

 

우선순위 위치 설명
1 --spring.config.location 명령줄에서 직접 지정
2 file:./config/ .jar와 같은 디렉터리의 config/ 폴더
3 file:./ .jar 파일이 있는 디렉터리
4 classpath:/config/ src/main/resources/config/ 등 클래스패스 내
5 classpath:/ 기본 src/main/resources/

 

 

이 위치의 우선순위에 따라 정렬되고, 겹치게 될경우 우선 순위가  높은경로의 설정값이  이전 항목의 것을 덮는다. 

 

classpath:   .jar 안에 있는 설정 파일을 의미

  • 프로젝트 내부자원이다. 
  •   maven이나 gradle로 빌드되면 .jar 안에 같이 포함된다. 
  •   내부에 있기 때문에 설정 변경시 빌드를 계속 해야한다.

file:  .jar 밖에 있는 외부 설정 파일을 의미

  • 운영 환경에선  이 경로에 설정파일을 두어 설정 변경을 유연하게 처리한다. 

 

 

 

 

 

✅ 프로퍼티 파일의 분리

 

일반적으로 어플리케이션 배포 환경은  개발/스테이징/운영으로 나뉜다.

따라서 자연스럽게 서로 다른 속성값을 가지게 되므로  application.properties  파일도 3가지로 분리되면 좋다. 

 

 

application.properties를 가장 먼저 읽고 

설정에 따라 application-dev.properties  / application-stg.properties  /  application-prod.properties   중 하나를 추가로 읽어서 스프링에 사용될 설정을 불러온다. 

 

 

 

 

 

프로필의 정의 부분이다. 

프로필이름을 지정해주면 그 밑의 설정 값들이 key=value 형태로   프로필에 해당하는 설정 값으로 세팅된다. 

spring.config.activate.on-profile="프로필이름"

 

 

 

 

어플리케이션 실행 인수로 어떤 프로퍼티 파일을 사용할지  넘겨주면 된다.

 

JVM 옵션으로 부여 

  • --Dspring.profiles.active=dev 로 실행할 때   dev프로필이 활성화 된다. 
  • --Dspring.profiles.active=stg   > stg프로필이 활성화
  • --Dspring.profiles.active=prod > prod프로필이 활성화

커맨드라인 옵션으로 부여 

java -jar "빌드된 Jar 파일명" --spring.profiles.active=dev

 

 

 

 

프로필 옵션을 지정하지 않으면 스프링은 default 라는 이름의 프로필을 사용한다. 

 



✅ 코드단 사용

스프링은  application.properties를 읽어온 다음 코드단에서 값을 사용할 수 있다. 

 

클래스패스 내부 설정에 있는 프로퍼티인 경우 

@PropertySource 를 통해  읽어올 프로퍼티를 지정한다. 

 

외부 설정파일을 읽을 때엔 

@ConfigurationProperties를 사용한다.

 

@Value : 프로퍼티에 설정한 내용을 변수로 주입시켜주는 역할이다.

 

 

 

 

 

 

 

 


 

references

https://spring.io/blog/2020/04/23/spring-tips-configuration

 

Spring Tips: Configuration

speaker: Josh Long (@starbuxman) Hi, Spring fans! Welcome to another installment of Spring tips! in this installment, we're going to look at something that's rather foundational, and something that I wish I'd addressed earlier: configuration. And no, I don

spring.io

https://www.geeksforgeeks.org/spring-boot-application-properties/

 

Spring Boot - Application Properties - GeeksforGeeks

Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.

www.geeksforgeeks.org

 

 

https://bnzn2426.tistory.com/139

https://hulrud.tistory.com/91 

https://adjh54.tistory.com/200

 

https://lordofkangs.tistory.com/320