Spring Boot profiles

Haechan Kim·2023년 7월 10일
0

Spring

목록 보기
46/68
post-thumbnail

Spring Boot profiles

Spring Boot profiles은 애플리케이션을 실행할 때 환경 설정을 구분하여 관리할 수 있는 기능.
프로파일은 애플리케이션을 특정 환경에 맞게 구성하고 설정을 선택적으로 활성화하는 데 사용.
이를 통해 개발, 테스트, 운영 등 다양한 환경에서 동일한 애플리케이션을 사용할 수 있다.

개발(dev), 테스트(stage), 알파(alpha), 상용(prod) 등 운영환경에 따라 서버나 설정 값들이 여러개로 나뉠 때, 스프링 부트에서는 이러한 설정을 간단히 할 수 있다.

프로파일을 사용하려면 application.properties 또는 application.yml 파일에서 spring.profiles.active 속성을 설정하여 현재 활성화할 프로파일을 지정해야 함.

해당 포스팅에서는 yml 파일을 기준으로 설정.

Spring Boot 2.4 버전부터 설정 파일의 방법이 변경됨!

# before
spring:
  profiles: "prod"
secret: "production-password"


# after
spring:
  config:
    activate:
      on-profile: "prod"
secret: "production-password"
  • applicaion.yml
spring:
  config:
    import: optional:file:.env[.properties]
  jpa:
    database: mysql
    hibernate:
      ddl-auto: update
    show-sql: 'true'
    database-platform: org.hibernate.dialect.MySQL8Dialect
    properties:
      hibernate:
        format_sql: 'true'
  freemarker:
    cache: 'false'
  devtools:
    restart:
      enabled: 'false'
    livereload:
      enabled: 'true'
  datasource:
    password: ${LOCAL_DB_PASSWORD}
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: ${LOCAL_DB_USERNAME}
    url: ${LOCAL_DB_URL}
    hikari:
      connection-test-query: select 1 from dual
  redis:
    host: localhost
    port: 6379
  jwt:
    secret: govlepel@$&
  • applicaion-local.yml
spring:
  config:
    import: optional:file:.env[.properties]
    activate:
      on-profile: "local"
  jpa:
    database: mysql
    hibernate:
      ddl-auto: update
    show-sql: 'true'
    database-platform: org.hibernate.dialect.MySQL8Dialect
    properties:
      hibernate:
        format_sql: 'true'
  freemarker:
    cache: 'false'
  devtools:
    restart:
      enabled: 'false'
    livereload:
      enabled: 'true'
  datasource:
    password: ${LOCAL_DB_PASSWORD}
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: ${LOCAL_DB_USERNAME}
    url: ${LOCAL_DB_URL}
    hikari:
      connection-test-query: select 1 from dual
  redis:
    host: localhost
    port: 6379
  jwt:
    secret: govlepel@$&

0개의 댓글