[SpringBoot] application.yml 구성 파일 설정

RUNGOAT·2023년 4월 2일
0

SpringBoot

목록 보기
2/3

📝 구성파일

1. application.yml

  • application.yml 하나로 공통 설정 정보들을 관리
  • ---은 yml 파일 내에서 프로파일 영역별 경계를 설정하는 기능
# 예시)
spring:
  profiles:
    active: local
    group:
      dev:
        - db-dev
      local:
        - db-local
      test:
        - db-test
    include:
      - db
      - login
      - site

2. application-{profile}.yml

  • 데이터베이스 접속 정보
  • 내/외부 API 호출 정보
  • application-db.yml, application-site.yml 등으로 분리해서 각 설정 정보들을 관리할 수 있다.
# default 공통 설정
spring:
  jpa:
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    generate-ddl: true
    properties:
      hibernate:
        format_sql: true
        show_sql: true
        use_sql_comments: true
  sql:
    init:
      mode: always
      continue-on-error: true
      
--- # dev 설정
spring:
  config:
    activate:
      on-profile: db-dev
# 관련 설정...

--- # local 설정
spring:
  config:
    activate:
      on-profile: db-local
# 관련 설정...

--- # test 설정
spring:
  config:
    activate:
      on-profile: db-test
# 관련 설정...

⚙ 설정

1. spring.profiles.active

  • default로 사용될 프로파일을 명시하는 기능
  • 해당 값을 명시하지 않는 경우 default 프로파일(application.yml)이 사용됨

2. spring.config.activate.on-profile

  • 해당 프로파일이 선택되었을 때만 활성화
  • 프로파일 활성화시 사용할 속성을 정의할 수 있다.
  • dev, prod, local, test 등으로 나눠서 사용 가능
  • 반대로 spring.config.activate.on-profile 프로퍼티가 없는 영역은 활성화된 프로파일과 무관하게 모든 프로파일들에서 공통적으로 사용된다.

3. spring.profiles.group.{group-name}

  • 그룹에 작성한 프로파일을 활성화할 수 있다.
  • ex) spring.profiles.group.dev: db-dev

4. 주의사항

  • YAML 파일 마지막에 spring.config.activate.on-profile선언할 것
  • 마지막에 선언한 것으로 덮어쓴다.

5. test에서 사용할 때

  • @ActiveProfiles()에 사용할 프로파일을 정의할 수 있다.
// test에 사용할 yaml 파일 적용
@ActiveProfiles("test")
@SpringBootTest
@AutoConfigureMockMvc
class UserControllerTest {
	// ...
}
  • test에서 직접 변수 정의하는 것도 가능
```java
@SpringBootTest(
				// 직접 변수 정의
        properties = {
                "testId=goddaehee",
                "testName=갓대희"
        }
)
@Transactional
@Slf4j
public class TestJpaRestControllerTest {

    @Value("${testId}")
    private String testId;

    @Value("${testName}")
    private String testName;

    @Test
    void getMember() throws Exception {
        log.info("##### Properties 테스트 #####");
        log.info("testId : " + testId);
        log.info("testName : " + testName);
    }
}
INFO  20-02-28 00:06:46[main] [TestJpaRestControllerTest:81] - ##### Properties 테스트 #####
INFO  20-02-28 00:06:46[main] [TestJpaRestControllerTest:82] - testId : goddaehee
INFO  20-02-28 00:06:46[main] [TestJpaRestControllerTest:83] - testName : 갓대희
```

📌 참고

profile
📞피드백 너무나 환영

0개의 댓글