개발일지/Spring

Spring boot - Profile 설정

E-room 2022. 11. 15. 17:58
728x90

로컬 개발환경과 서버 환경을 따로 Profile을 설정해보자

 

프로젝트 폴더 내의 src/main/resources 에 보면 application.properties 라는 파일에서 Profile을 설정하는데

 

본인은 .yml 형식을 선호하므로 application.yml 형식으로 바꾸어 주었다. (뭐로 사용하든 관계없음)

 

사진과 같이 복사해서 2개의 파일을 더 만들어 준다.

 

local 이라고 되어있는 파일에는 로컬 개발환경의 profile을 설정해주고

# 예시) 로컬 환경에서 사용하는 정보들은 application-local.yml 파일에 설정
spring:
  h2:
    console:
      enabled: true
      path: /h2
  datasource:
    url: jdbc:h2:mem:test
  jpa:
    hibernate:
      ddl-auto: create
    show-sql: true
    properties:
      hibernate:
        format_sql: true
  sql:
    init:
      data-locations: classpath*:db/h2/data.sql
logging:
  level:
    org:
      springframework:
        orm:
          jpa: DEBUG
server:
  servlet:
    encoding:
      force-response: true

 

server 라고 되어있는 파일에는 서버 환경으로 설정해주었다.

# 예시) 서버 환경에서 사용하는 정보들은 application-server.yml 파일에 설정
spring:
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true
    properties:
      hibernate:
        format_sql: true
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: password

 

 

이제 애플리케이션을 실행해보자

 

IntelliJ 일 경우

IntelliJ의 경우 상단에 구성 편집을 클릭한다

 

 

사진과 같이 입력할 경우 application-local.yml의 profile이 적용되어 실행되고 --spring.profiles.active=local

local부분을 server로 수정할 경우 application-local.yml의 profile이 적용되어 실행된다 --spring.profiles.active=server

설정 예시

 

터미널의 경우

build 된 jar파일을 실행시킬 때 뒤에 intelliJ에서 실행할 때와 동일하게 옵션을 추가해서 실행시켜준다

실행 예시

 

728x90