@Profile 이란
스프링 프레임워크에서 제공하는 어떤 특정한 환경에 따른 설정값을 달리 적용하는 것.
각 Profile 은 각각 다른 설정이나 각각 Bean 을 정의한다.
빌드 시 원하는 Profile 에 맞춰 빌드할 수 있다.
@Profile 예시
개발 환경과 배포 환경의 각기 다른 Profile 을 생성한다.
// BaseConfiguraion.java @Profile("prod") @Configuration public class BaseConfiguration { @Bean public String hello() { return "hello"; } } // TestConfiguraion.java @Profile("test") @Configuration public class TestConfiguration { @Bean public String hello() { return "hello test"; } }
그리고 Runner 에서 위에서 정의한 Bean 을 사용한다.
// SampleRunner.java @Component public class SampleRunner implements ApplicationRunner { @Autowired private String hello; @Override public void run(ApplicationArguments args) throws Exception { System.out.println("==========="); System.out.println(hello); System.out.println("==========="); } }
이 상태로 실행하면 오류가 발생한다.
왜냐하면 어떤 Profile 을 사용할지 설정하지 않았기 때문이다.
// application.properties spring.profiles.active=prod
이제 App 을 실행하면 @Profile("prod")
에서 설정한 Bean 이 출력된다.
@Profile Properties
(1) 외부 설정 우선순위
// Command Line Arguments 이용 java -jar target/edu-application-0.0.1-SNAPSHOT.jar --spring.profies.active=test
Command Line Arguments 가 기존 Properties 설정을 오버라이딩 하여 결과는 hello test
가 출력된다.
* VSCode Maven Packaging 시 Test Skip 방법
Custom 클릭 후 packaging -DskipTests
입력
(2) Profile 용 Properties
application-<Profile name>.properties
형식으로 Profile 용 Properties 를 생성할 수 있다.
// application-prod.properties jsk.name=Jinseo prod // application.properties jsk.name=Jinseo
// Command Line Arguments 이용 java -jar target/edu-application-0.0.1-SNAPSHOT.jar --spring.profies.active=prod
// Result Jinseo prod
Profile 용 Properties 가 Application 의 Properties 보다 우선순위가 높기 때문에 오버라이딩 된다.
(3) Inculde
// application-prod.properties jsk.name=Jinseo prod spring.profiles.include=proddb // 해당 properties 가 읽혀질 때 추가할 Profile 지정 // application-proddb.properties jsk.full-name=proddb fullname
// Command Line Arguments 이용 java -jar target/edu-application-0.0.1-SNAPSHOT.jar --spring.profies.active=prod
// Result proddb fullname
참고
https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-logging
“How-to” Guides
Spring Boot has no mandatory logging dependency, except for the Commons Logging API, which is typically provided by Spring Framework’s spring-jcl module. To use Logback, you need to include it and spring-jcl on the classpath. The recommended way to do th
docs.spring.io
'Dev > Spring Boot' 카테고리의 다른 글
[스프링 부트 개념과 활용] 테스트 (0) | 2020.08.24 |
---|---|
[스프링 부트 개념과 활용] 로깅 (0) | 2020.08.24 |
[스프링 부트 개념과 활용] 외부 설정 2부 (0) | 2020.08.24 |
[스프링 부트 개념과 활용] 외부 설정 1부 (0) | 2020.08.21 |
[스프링 부트 개념과 활용] SpringApplication 2부 (0) | 2020.08.20 |
댓글