作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用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/
我是一名优秀的程序员,十分优秀!