Profiles
2024. 1. 15. 14:49
728x90
Spring Profiles
: 다양한 설정을 Profile이라는 단위로 나누어 사용할 설정을 어플리케이션 실행시 결정할 수 있게 해주는 스프링의 기능.
> 개발 단계에서 사용할 데이터베이스와 테스트 코드용 데이터베이스를 나누거나 서비스할 때 로그를 줄이고 싶은 경우
** application-{profile}.yaml
1) 사용하고 싶은 profile 이름을 정한다. (dev, test, prod...)
2) 그 이름이 포함된 yaml을 만든다.
- application.yaml 설정이 기본으로 실행된다.
>> 기본 profile 설정 : spring.profiles.default
// application.yaml
// 특별한 인자 없이 프로젝트 실행시 application-dev.yaml을 사용해서 프로젝트 실행
spring:
profiles:
default: dev
- 실행시에 사용할 프로필을 선택하기 : spring.profiles.active
spring:
profiles:
active: dev
✅ Spring Boot 2.4이상의 버전에서는 spring.config.active.on-profile로 설정해주어야한다.
spring:
config:
active:
on-profile: test
내가 실제로 사용했던 예시
더보기
// application.yaml
#profile 설정
spring:
datasource:
url: jdbc:sqlite:db.sqlite
driver-class-name: org.sqlite.JDBC
jpa:
hibernate:
ddl-auto: update
show-sql: true
defer-datasource-initialization: true
# sql:
# init:
# mode: always
config:
activate:
on-profile: test
#테스트 프로파일
---
spring:
config:
active:
on-profile: test
sql:
init:
mode: never
datasource:
url: jdbc:h2:tcp://localhost/~/test
driver-class-name: org.h2.Driver
username: sa
password:
h2:
console:
enabled: true
path: /h2-console
jpa:
hibernate:
ddl-auto: create
show-sql: true
database-platform: org.hibernate.dialect.H2Dialect
defer-datasource-initialization: true
#개발 프로파일
---
spring:
config:
activate:
on-profile: dev
datasource:
url: jdbc:sqlite:db.sqlite
driver-class-name: org.sqlite.JDBC
sql:
init:
mode: always
>> application.yaml파일에 포함할 수도 있지만, java 명령어로 jar 파일을 실행할 때 첨부할 수도 있다!
java -Dspring.profiles.active=test -jar build/libs/article-0.0.1-SNAPSHOT.jar
- @Profile로 Bean 객체도 특정 Profile에서만 만들어지게 설정 가능.
@Controller
// 개발 환경(profile-dev)에서만 사용하고 싶은 Controller
@Profile("dev")
public class MonitorController {
@GetMapping("/monitor")
@ResponseBody
public String test() {
return "monitor";
}
}
728x90
'Programming > Spring, SpringBoot' 카테고리의 다른 글
Spring Boot & REST (0) | 2024.01.18 |
---|---|
연습문제(로깅) (0) | 2024.01.15 |
Logging (0) | 2024.01.15 |
Spring Beans (0) | 2024.01.15 |
Spring Data JPA (Java Persistence API) (2) | 2024.01.07 |