Environment (Profile)
ApplicationContext extends EnvironmentCapable
- getEnvironment()
프로파일
- 빈들의 묶음 (그룹)
- Environment의 역할은 활성화할 프로파일 확인 및 설정
프로파일 정의
- 클래스에 정의
@Configuration @Profile("test")
@Component @Profile("test")
@Component @Profile("!test") : test 프로파일이 아닌 경우
!(not), &(and), |(or) 등의 표현식도 추가가 가능. - 메소드에 정의
@Bean @Profile("test")
프로파일 설정하기
- VMOption : -Dspring.profile.active="test, A, B,..."
- @ActiveProfiles
Environment (Property)
프로퍼티
- 다양한 방법으로 정의할 수 있는 설정값
- Environment의 역할은 프로퍼티 소스 설정 및 프로퍼티 값 가져오기
프로퍼티에는 우선 순위가 있다.
@PropertySource
- Environment를 통해 프로퍼티 추가하는 방법 (app.properties의 내용을 사용할 수 있음.)
@PropertySource("classpath:/app.properties")
MessageSource
국제화 (i18n) 기능을 제공하는 인터페이스
ApplicationContext extends MessageSource
- getMessage(String code, Object[] args, String, default, Locale, loc)
스프링 부트를 사용한다면 별다른 설정 필요없이 message.properties를 사용할 수 있음.
- message.properties
- message_ko_kr.properties
수업에서는 아래의 결과가 en으로 자동 설정되었음.
Locale.getDefault()
실제 테스트 (Windows 10, IntelliJ IDEA Ultimate)에서는 KR이 기본으로 설정되어 있었고,
System.out.println(messageSource.getMessage("greeting", new String[]{"jimin"}, Locale.ENGLISH));
Locale.ENGLISH를 사용하기 위해서는 message_en.properties의 형태로 파일을 생성해주어야 했다.
릴로딩 기능이 있는 메시지 소스 사용하기 (사용 도중 빌드를 하여 message 변경이 가능)
@Bean
public MessageSource messageSource() {
var messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:/messages");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(3);
return messageSource;
}
'노력 > Spring Boot' 카테고리의 다른 글
스프링 프레임워크 핵심 기술 - (2) (0) | 2021.11.20 |
---|---|
스프링 프레임워크 핵심 기술 - (1) (0) | 2021.11.19 |
IntelliJ에서의 Spring boot 테스트 프로젝트 - (1) (0) | 2021.05.29 |
Spring Boot와 Spring의 디렉터리 구조 차이점 그리고 Entity (0) | 2020.01.25 |