Home > Development tools and environments > ๐Ÿ› ๏ธ[๊ฐœ๋ฐœ ๋„๊ตฌ ๋ฐ ํ™˜๊ฒฝ] Spring Boot์—์„œ Profile์„ ํ™œ์šฉํ•˜์—ฌ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ๋ถ„๋ฆฌํ•˜๊ธฐ.

๐Ÿ› ๏ธ[๊ฐœ๋ฐœ ๋„๊ตฌ ๋ฐ ํ™˜๊ฒฝ] Spring Boot์—์„œ Profile์„ ํ™œ์šฉํ•˜์—ฌ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ๋ถ„๋ฆฌํ•˜๊ธฐ.
Development tools Enviroments

๐Ÿ› ๏ธ[๊ฐœ๋ฐœ ๋„๊ตฌ ๋ฐ ํ™˜๊ฒฝ] Spring Boot์—์„œ Profile์„ ํ™œ์šฉํ•˜์—ฌ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ๋ถ„๋ฆฌํ•˜๊ธฐ.

1๏ธโƒฃ ํ”„๋กœํŒŒ์ผ ๋ถ„๋ฆฌ ๋ฐฉ๋ฒ•.

๋ฐฉ๋ฒ• 1: application.yml ํŒŒ์ผ์—์„œ ๋ถ„๋ฆฌ.

  • application.yml ํŒŒ์ผ์€ ์—ฌ๋Ÿฌ ํ”„๋กœํŒŒ์ผ์„ ํ•˜๋‚˜์˜ ํŒŒ์ผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.
  • ํ™˜๊ฒฝ๋ณ„๋กœ ์„ค์ •์„ ๋‚˜๋ˆ„๊ธฐ ์œ„ํ•ด --- ๊ตฌ๋ถ„์ž๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์‹œ: application.yml

# ๊ธฐ๋ณธ ์„ค์ • (spring.profiles.active๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด ์ด ์„ค์ •์ด ์‚ฌ์šฉ๋จ)
spring:
    profiles:
        default: local
    datasource:
        url: jdbc:h2:mem:localdb
        username: local_user
        password: local_pass

---
# ๊ฐœ๋ฐœ ํ™˜๊ฒฝ(dev) ์„ค์ •.
spring:
    profiles: dev
    datasource:
        url: jdbc:mysql://dev.example.com:3306/devdb
        username: dev_user
        password: dev_pass
        
---
# ์šด์˜ ํ™˜๊ฒฝ(prod) ์„ค์ •.
spring:
    profiles: prod
    datasource:
        url: jdbc:mysql://prod.example.com:3306/proddb
        username: prod_user
        password: prod_pass

๋ฐฉ๋ฒ• 2: applicatioon-{profile}.yml ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌ.

  • ๊ฐ ํ”„๋กœํŒŒ์ผ์„ ๋ณ„๋„ ํŒŒ์ผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ํŒŒ์ผ ์ด๋ฆ„์— {profile} ๋ถ€๋ถ„์„ ํ”„๋กœํŒŒ์ผ ์ด๋ฆ„์œผ๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

ํŒŒ์ผ ๊ตฌ์กฐ

src/main/resources/
โ”œโ”€โ”€ application.yml        # ๊ธฐ๋ณธ ์„ค์ • ํŒŒ์ผ
โ”œโ”€โ”€ application-local.yml  # local ํ”„๋กœํŒŒ์ผ ์„ค์ •
โ”œโ”€โ”€ application-dev.yml    # dev ํ”„๋กœํŒŒ์ผ ์„ค์ •
โ”œโ”€โ”€ application-prod.yml   # prod ํ”„๋กœํŒŒ์ผ ์„ค์ •

application.yml(๊ณตํ†ต ์„ฑ์ • ๋ฐ ํ™œ์„ฑํ™” ํ”„๋กœํŒŒ์ผ ์ง€์ •)

spring:
    profile:
        active: local # ๊ธฐ๋ณธ ํ™œ์„ฑํ™” ํ”„๋กœํŒŒ์ผ ์„ค์ •

application-local.yml

spring:
    datasource:
        url: jdbc:h2:mem:localdb
        username: local_user
        password: local_pass

application-dev.yml

spring:
    datasource:
        url: jdbc:mysql://dev.example.com:3306/devdb
        username: dev_user
        password: dev_pass

application-prod.yml

spring:
    datasource:
        url: jdbc:mysql://prod.example.com:3306/proddb
        username: prod_user
        password: prod_pass

2๏ธโƒฃ ์‹คํ–‰ ์‹œ ํ”„๋กœํŒŒ์ผ ์ง€์ • ๋ฐฉ๋ฒ•.

๋ฐฉ๋ฒ• 1: ๋ช…๋ น์–ด ์˜ต์…˜์œผ๋กœ ํ”„๋กœํŒŒ์ผ ์ง€์ •.

java -jar library-app-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev

๋ฐฉ๋ฒ• 2: ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ํ”„๋กœํŒŒ์ผ ์ง€์ •.

SPRING_PROFILES_ACTIVE=dev java -jar library-app-0.0.1-SNAPSHOT.jar

๋ฐฉ๋ฒ• 3: IDE๋ฅผ ํ†ตํ•ด ํ”„๋กœํŒŒ์ผ ์ง€์ •

server:
  port: 8080
spring:
  config:
    activate:
      on-profile: local
  datasource:
    url: "jdbc:h2:mem:library;MODE=MYSQL;NON_KEYWORDS=USER"
    username: "sa"
    password: ""
    driver-class-name: org.h2.Driver
  jpa:
    hibernate:
      ddl-auto: create
    properties:
      hibernate:
        show_sql: true
        format_sql: true
        dialect: org.hibernate.dialect.H2Dialect
  h2:
    console:
      enabled: true
      path: /h2-console
---
server:
  port: 8080
spring:
  config:
    activate:
      on-profile: dev
  datasource:
    url: "jdbc:mysql://localhost/library"
    username: "root"
    password: ""
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: none
    properties:
      hibernate:
        show_sql: true
        format_sql: true
        dialect: org.hibernate.dialect.MySQL8Dialect
    1. Run/Debug Configuration ์ฐฝ ์—ด๊ธฐ
    1. Active profiles์— ์ง€์ •ํ•  ํ™˜๊ฒฝ ์ถ”๊ฐ€

3๏ธโƒฃ ์ฝ”๋“œ๋กœ ํ”„๋กœํŒŒ์ผ ํ™•์ธ ๋ฐ ์ ์šฉ.

  • Spring Boot ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ์—์„œ ํ˜„์žฌ ํ™œ์„ฑํ™”๋œ ํ”„๋กœํŒŒ์ผ์„ ํ™•์ธํ•˜๊ฑฐ๋‚˜ ํŠน์ • ๋กœ์ง์„ ํ™˜๊ฒฝ์— ๋”ฐ๋ผ ๋ถ„๊ธฐํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ™œ์„ฑ ํ”„๋กœํŒŒ์ผ ํ™•์ธ.

import org.springframework.core.env.Enviroment;
import org.springframework.stereotype.Component;

@Component
public class ProfileChecker {
    
    private final Environment environment;
    
    public ProfileChecker(Environment environment) {
        this.environment = this.environment;
    }
    
    public void printActiveProfiles() {
        String[] activeProfiles = environment.getActiveProfiles();
        System.out.println("Active Profiles: " + String.join(", ", activeProfiles));
    }
}

ํŠน์ • ํ”„๋กœํŒŒ์ผ์—์„œ๋งŒ ๋™์ž‘ํ•˜๋Š” ์ฝ”๋“œ.

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

@Service
@Profile("dev") // dev ํ™˜๊ฒฝ์—์„œ๋งŒ ํ™œ์„ฑํ™”
public class DevOnlyService {
    public void run() {
        System.out.println("This service runs only in the dev environment!");
    }
}

4๏ธโƒฃ ์ฃผ์˜์‚ฌํ•ญ.

1๏ธโƒฃ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์šฐ์„  ์ˆœ์œ„.

  • Spring Boot๋Š” ํ”„๋กœํŒŒ์ผ ์„ค์ •์—์„œ ์•„๋ž˜ ์ˆœ์„œ๋กœ ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.
    • ๋ช…๋ น์ค„ ์ธ์ˆ˜
    • SPRING_PROFILES_ACTIVE ํ™˜๊ฒฝ ๋ณ€์ˆ˜
    • appilcation.yml ๋˜๋Š” applicatioon.properties์˜ spring.profiles.active ๊ฐ’

2๏ธโƒฃ ์šด์˜ ํ™˜๊ฒฝ์— ๋Œ€ํ•œ ๋ณด์•ˆ

  • ์šด์˜ ํ™˜๊ฒฝ์—์„œ๋Š” application-prod.yml ๊ฐ™์€ ํŒŒ์ผ์— ๋ฏผ๊ฐํ•œ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๊ณ  ์™ธ๋ถ€ ์„ค์ • ํŒŒ์ผ์ด๋‚˜ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

3๏ธโƒฃ ํ”„๋กœํŒŒ์ผ์— ๋”ฐ๋ฅธ ๋นˆ(Bean) ์ƒ์„ฑ ๊ด€๋ฆฌ.

  • ํŠน์ • ํ”„๋กœํŒŒ์ผ์—์„œ๋งŒ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๋นˆ์€ @Profile ์• ๋„ˆํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

5๏ธโƒฃ ๊ฒฐ๋ก .

  • Spring Boot์˜ ํ”„๋กœํŒŒ์ผ์„ ํ†ตํ•ด ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์— ๋”ฐ๋ฅธ ์„ค์ •์„ ํšจ๊ณผ์ ์œผ๋กœ ๋ถ„๋ฆฌํ•˜๊ณ  ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • application.yml ๋‚ด์—์„œ ๋ถ„๋ฆฌํ•˜๊ฑฐ๋‚˜ ๋ณ„๋„ ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์„ค์ •ํ•˜๋ฉฐ, ์‹คํ–‰ ์‹œ โ€“spring.profiles.active๋กœ ํ™œ์„ฑํ™”ํ•  ํ”„๋กœํŒŒ์ผ์„ ์ง€์ •ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  • ์šด์˜ ํ™˜๊ฒฝ์—์„œ๋Š” ๋ณด์•ˆ์„ ๊ณ ๋ คํ•ด ๋ฏผ๊ฐ ์ •๋ณด๋ฅผ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋กœ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๊ถŒ์žฅ๋ฉ๋‹ˆ๋‹ค.