jar 실행 시 profile 선택

CodeDiver·2021년 12월 28일
0

java -jar sample.jar --spring.profiles.active=prod

환경설정 파일을 아래와 같이 분리하여 작성한다.

application.yml

spring:
  profiles:
    active: prod

applicaion-prod.yml

server:
  port: 8080

spring:
  datasource:
    url: jdbc:mariadb://xxxx:3306/dbname
    username: xxxx
    password: xxxx
    driver-class-name: org.mariadb.jdbc.Driver

  jpa:
    properties:
      hibernate:
        format_sql: true

logging:
  level:
    org:
      hibernate:
        SQL: DEBUG
        type:
          descriptor:
            sql:
              BasicBinder: TRACE
    com:
      godcoder: DEBUG

applicaion-dev.yml

server:
  port: 80

spring:
  datasource:
    url: jdbc:mariadb://xxxx:3306/dbname
    username: xxxx
    password: xxxx
    driver-class-name: org.mariadb.jdbc.Driver

  jpa:
    properties:
      hibernate:
        format_sql: true

logging:
  level:
    org:
      hibernate:
        SQL: DEBUG
        type:
          descriptor:
            sql:
              BasicBinder: TRACE
    com:
      godcoder: DEBUG

설정값 읽기

@Data
@Component
@ConfigurationProperties("server")
public class ServerProperties {
    private Integer port;
}

설정값을 사용할 곳에서 (ex: controller)

private final ServerProperties serverProperties;
...
System.out.println("port = " + serverProperties.getPort());
profile
Spring Boot, React.JS, Android 개발자

0개의 댓글