是否有可能使Spring@Import或@Configuration参数化?
我已经创建了很多常见的小bean定义容器(),我用它们来快速开发带有Spring Boot的应用程序,例如:@Configuration
@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 ...
例如,其中一个可以使用Web控制器为区域设置cookie的会话存储,以切换到所选语言等。@Configuration
使用它们和重用非常有趣,但是将其参数化真的很棒,这可以允许更多的重用。我的意思是这样的:
伪代码:
@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}");
}