【发布时间】:2015-09-30 21:48:12
【问题描述】:
我创建了许多常见的小型 bean 定义容器 (@Configuration),我用它们来快速开发带有 Spring Boot 的应用程序,例如:
@Import({
FreemarkerViewResolver.class, // registers freemarker that auto appends <#escape etc.
ConfigurationFromPropertiesFile.class, // loads conf/configuration.properties
UtfContentTypeResponse.class, // sets proper Content-language and Content-type
LocaleResolverWithLanguageSwitchController // Locale resolver + switch controller
);
class MySpringBootApp ...
例如,这样的@Configurations 之一可以使用网络控制器为区域设置 cookie 设置会话存储以切换到选定的语言等。
使用和重用它们非常有趣,但是将其参数化真的很棒,这可以允许更多的重用。我的意思是:
伪代码:
@Imports( imports = {
@FreemarkerViewResolver( escapeHtml = true, autoIncludeSpringMacros = true),
@ConfigurationFromProperties( path = "conf/configuration.properties" ),
@ContentTypeResponse( encoding = "UTF-8" ),
@LocaleResolver( switchLocaleUrl = "/locale/{loc}", defaultLocale = "en"
})
所以,我的意思是“可配置的@Configurations”。 以这种方式进行配置的最佳方法是什么?
也许更像这样(再次,伪代码):
@Configuration
public class MyAppConfiguration {
@Configuration
public FreemarkerConfiguration freemarkerConfiguration() {
return FreemarkerConfigurationBuilder.withEscpeAutoAppend();
}
@Configuration
public ConfigurationFromPropertiesFile conf() {
return ConfigurationFromPropertiesFile.fromPath("...");
}
@Configuration
public LocaleResolverConfigurator loc() {
return LocaleResolverConfigurator.trackedInCookie().withDefaultLocale("en").withSwitchUrl("/switchlocale/{loc}");
}
【问题讨论】:
标签: java spring spring-mvc annotations spring-boot