有多个用 @Configuration 注释的类,我想在顶级配置组件中决定在上下文中注册哪个类。
@Configuration
public class FileSystemDataStoreConfiguration {
public @Bean DataStore getDataStore() {
return new FileSystemDataStore(withSomeConfigProperties);
}
}
@Configuration
public class DatabaseDataStoreConfiguration {
public @Bean DataStore getDataStore() {
return new DatabaseDataStore(withSomeConfigProperties);
}
}
@Configuration
public class DataStoreConfiguration {
// Some arbitrary logic to decide whether I want to load
// FileSystem or Database configuration.
}
我知道我可以使用 @Profile 在多个配置类中进行选择。但是,我已经使用配置文件来区分环境。配置类的选择与环境无关。
如何在运行时选择要加载的配置类?
我可以拥有多个活动配置文件,例如“Production, WithDatabase”吗?
如果是这样,我如何添加基于属性的配置文件?