Customizing the Bootstrap Property Sources
被添加到bootstrap process的外部配置的属性资源默认是在Config Server中,但是你可以通过添加PropertySourceLocator类型的bean到bootstrap上下文中来添加额外的资源。你可以使用这种方式从不同的服务器,数据库添加附加属性。
参考下面的例子
@Configuration
public class CustomPropertySourceLocator implements PropertySourceLocator {
@Override
public PropertySource<?> locate(Environment environment) {
return new MapPropertySource("customProperty",
Collections.<String, Object>singletonMap("property.from.sample.custom.source", "worked as intended"));
}
}
被传递的环境用来创建ApplicationContext,即用它来提供附加的资源属性,它已经拥有了普通的资源属性,你可以使用这些定位资源属性到环境中。(例如,spring.application.name做为默认的配置服务)
如果使用这些class文件生成jar文件,并且在META-INF/spring.factories中添加以下属性,“customProperty”属性资源将会出现在任何一个引用这个jar的应用上。
org.springframework.cloud.bootstrap.BootstrapConfiguration=sample.custom.CustomPropertySourceLocator