当前位置: 首页 > 知识库问答 >
问题:

如何在java Spring中将propertysource设置为从云配置服务器获取的配置文件?

公西志文
2023-03-14
@Configuration
@ConfigurationProperties(prefix = "some.prefix")
  public class SomeMappedConfigClass {
    private String variable1;
    private String variable2;
}
some.prefix:
  variable1: abc
  variable2: xyz
@PropertySource("classpath:some-application.yml")

不管配置文件名如何,是否有任何方法可以将当前加载的配置传递给PropertySource?从cloud config成功获取配置后收到的日志,用于应用程序:web-server profile:LOCAL

Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='file:central-config/web-server-LOCAL.yml'}]}

共有1个答案

丁书
2023-03-14

可以在类路径中使用外部配置。使用以下传递配置

-Dspring.config.location=your/config/dir/

-dspring.config.location=classpath:prop1.properties,classpath:prop2.properties

@Configuration
public class AppConfig {

    @Value("${config.properties:<default values>}")
    String propvalue;

    @Autowired
    Environment env;


    public void method(){
     String datapath = env.getProperty("data.path")
    }

}
 类似资料:
  • 我刚刚将我们的Spring Boot项目从引导升级到v2.6.2,从Spring Cloud升级到2021.0.0。 现在,我的远程配置获取没有任何效果,应用程序也无法获取正确的属性文件 [main]INFO o. s. c. c. c. ConfigServiceProperty tySourceLocator-从服务器获取配置:http://localhost:8080 [main]WARN

  • 我有3个正在运行的Spring Boot应用程序: 尤里卡:8761 对于Spring云配置,我使用本地git URI来填充数据。本地回购位于分支机构主机上,其文件结构如下: 根据文档,我可以访问如下文本文件: http://localhost:8080/myMicroService/default/master/foo.txt http://localhost:8080/myMicroServi

  • 我正在使用Spring Cloud Config服务器,能够检测来自git存储库的更改并将其传递给配置客户机。 有两种方法,我已经实现了: null 所以两者都工作得很好,那么使用Spring Cloud Bus有什么好处吗?或者在生产环境中,不使用Spring Cloud Bus会有什么问题吗?因为将需要额外的工作来设置RabbitMQ集群(HA)作为生产中的Spring云总线。 谢谢,大卫

  • 配置代理服务器能干嘛 NEI toolkit 提供了代理服务器的功能,帮助将接口代理到NEI官网或者特定的服务器上 将接口代理到NEI官网 开启该功能只需将server.config.js中的online设为true, 那么对Mock Server的所有请求都将会代理到NEI官网上,该模式也被称为在线模式。 启用在线模式可以减少执行nei update的操作,在频繁更改官网数据的情况下能够大幅加快

  • 我是Spring Cloud的新手,我正在尝试使用存储在github上的属性文件连接服务器和客户端。 我的服务器应用程序。yml文件的配置如下: github回购协议链接在这里,主要属性和替代属性 我的客户端应用程序具有以下设置 Rest控制器是: 所有${变量}van都可以在位于git存储库中的属性文件中找到。 服务器运行正常,但是客户端给了我以下错误 创建名为“rateController”的

  • 问题内容: 我想将配置文件名称设置为整个程序包,但我不知道如何。如果在哪里不是简单的方法,那么我必须用注释标记包和子包中的每个类。 标签不支持这样的属性,所以我不知道。 问题答案: 您可以将个人资料设置为: 文件-用于xml配置 Java配置类 在它们的每一个中,您都可以对特定的软件包使用组件扫描。