You are here

Multiple MessageSources for environments

本地开发环境使用 org.springframework.context.support.ReloadableResourceBundleMessageSource, 动态加载资源。

@Configuration
	@Profile("development")
	public class LocalMessageSourceConfiguration {
		
		@Bean
		public ReloadableResourceBundleMessageSource messageSource() {
			ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource();
			source.setBasename("classpath:/i18n/messages");
			source.setCacheSeconds(0); 
			source.setDefaultEncoding("UTF-8");
			return source;
		} 
	}

其它发布环境,使用ResourceBundleMessageSource静态加载.

@Configuration
	@Profile("!development")
	public class MessageSourceConfiguration {
		
		@Bean
		public ResourceBundleMessageSource messageSource() {
			ResourceBundleMessageSource source = new ResourceBundleMessageSource();
			source.setBasename("/i18n/messages");
			source.setDefaultEncoding("UTF-8");
			return source;
		}
	}

 

Ref:
https://blog.andresteingress.com/2017/10/06/spring-messagesource-beans.html