如何在JavaSpring中将propertysource设置为从云配置服务器获取的配置文件?
我正在使用注释将一些值从application.yml映射到java类。当将它与localhost配置文件一起使用时,一切正常,但当我从cloud config获取配置时,找不到这些值。我猜问题可能是配置文件名可能会因选择的配置而异,而spring不知道在哪个文件中查找它们如何在JavaSpring中将propertysource设置为从云配置服务器获取的配置文件?,java,spring,spring-cloud-config,configurationproperties,Java,Spring,Spring Cloud Config,Configurationproperties,我正在使用注释将一些值从application.yml映射到java类。当将它与localhost配置文件一起使用时,一切正常,但当我从cloud config获取配置时,找不到这些值。我猜问题可能是配置文件名可能会因选择的配置而异,而spring不知道在哪个文件中查找它们 @Configuration @ConfigurationProperties(prefix = "some.prefix") public class SomeMappedConfigClass { priva
@Configuration
@ConfigurationProperties(prefix = "some.prefix")
public class SomeMappedConfigClass {
private String variable1;
private String variable2;
}
和带有配置的yaml
some.prefix:
variable1: abc
variable2: xyz
我试图通过注释映射配置文件,但它需要配置文件名,在我的情况下可能会有所不同
@PropertySource("classpath:some-application.yml")
无论配置文件名如何,是否有方法传递到PropertySource当前加载的配置?
成功从云配置获取配置后收到的日志,用于应用程序:web服务器配置文件:本地
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")
}
}
@配置
公共类AppConfig{
@值(“${config.properties:}”)
字符串属性值;
@自动连线
环境环境;
公开作废法(){
字符串datapath=env.getProperty(“data.path”)
}
}
这里没有问题。添加spring boot actuator以查看绑定到应用程序的属性。或者检查配置服务器上检索到的属性http://localhost:8888/{application}/{profile}
我验证了它,您是对的,spring加载的配置正确,我在配置方面有一些问题,感谢您,实际上我不想使用@Value,但我阅读了更多关于spring环境及其工作原理的信息,我发现SpringLoad配置正确,而且我在加载配置时遇到了一些问题,谢谢