gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:47:35 26 4
gpt4 key购买 nike

我正在使用ConfigurationProperties用于将 application.yml 中的某些值映射到 java 类的注释。与本地主机配置文件一起使用时一切正常,但是当我从云配置中获取配置时,无法找到这些值。我想这个问题可能是配置文件名可能会有所不同,具体取决于选择的配置,并且 spring 不知道在哪个文件中查找它们。

@Configuration
@ConfigurationProperties(prefix = "some.prefix")
public class SomeMappedConfigClass {
private String variable1;
private String variable2;
}

和带有配置的 yaml

some.prefix:
variable1: abc
variable2: xyz

我试图通过 PropertySource 映射配置文件注释,但它需要配置文件名,在我的情况下可能有所不同。

@PropertySource("classpath:some-application.yml")

有没有办法将当前加载的配置传递给 PropertySource,而不管配置文件名如何?从云配置成功获取配置后收到的日志,适用于应用程序:网络服务器配置文件:本地

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

最佳答案

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

-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")
}

}

关于java - 如何在java spring中将propertysource设置为从云配置服务器获取的配置文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54578090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com