| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1672 人关注过本帖
标题:Spring Cloud自定义引导属性源
只看楼主 加入收藏
ITXX
Rank: 1
等 级:禁止访问
帖 子:14
专家分:0
注 册:2018-6-28
收藏
 问题点数:0 回复次数:0 
Spring Cloud自定义引导属性源
引导过程添加的外部配置的默认属性源是Config Server,但您可以通过将PropertySourceLocator类型的bean添加到引导上下文(通过spring.factories)添加其他源。您可以使用此方法从其他服务器或数据库中插入其他属性。

作为一个例子,请考虑以下微不足道的自定义定位器:
程序代码:
@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"));
    }

 
}

传入的Environment是要创建的ApplicationContext的Environment,即为我们提供额外的属性来源的。它将已经具有正常的Spring Boot提供的资源来源,因此您可以使用它们来定位特定于此Environment的属性源(例如通过将其绑定在spring.application.name上,如在默认情况下所做的那样Config Server属性源定位器)。

如果你在这个类中创建一个jar,然后添加一个META-INF/spring.factories包含:

org.springframework.cloud.bootstrap.BootstrapConfiguration=sample.custom.CustomPropertySourceLocator
那么“customProperty”PropertySource将显示在其类路径中包含该jar的任何应用程序中。
搜索更多相关主题的帖子: Spring 自定义 属性 添加 Environment 
2018-06-29 10:59
快速回复:Spring Cloud自定义引导属性源
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017072 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved