我使用了spring boot 1.4.2,现在尝试设置属性。
我有四个关于使用.properties和.yml到外部(资源内部)和本地文件系统(项目文件夹外部)的案例。
@Value(${property.name})和@PropertySource最后一个使用值属性可以从文件系统加载,.properties外部(在资源文件夹内)工作良好,如下所示。
@Component
@PropertySource(value="file:C:\\properties\\application-dev.properties")
@PropertySource(value="file:C:\\properties\\application-test.properties")
public class ExternalProperties {
// ...
}
但是当文件名为'application.yml‘时,.yml在资源文件夹下不能很好地工作。
插件可以通过@ConfigurationProperties加载,并且需要“propdeps- .yml”
我的.yml文件
environments:
dev:
url: http://dev.bar.com
name: Developer Setup
prod:
url: http://foo.bar.com
name: My Cool App
这段代码运行良好
@Component
// external(inside resources)
@ConfigurationProperties(prefix="environments")
// outside project folder
@ConfigurationProperties(locations={"file:C:\\properties\\application.yml"}, prefix = "environments")
public class YamlProperties {
private Map<String, String> dev = new HashMap<>();
private Map<String, String> prod = new HashMap<>();
public Map<String, String> getDev() {
return this.dev;
}
public Map<String, String> getProd() {
return this.prod;
}
}
该代码存在已弃用的位置属性的问题(我读了很多文章,但都搞不清楚),所以我需要从这个section中找到API文档和'ConfigFileApplicationListener'。
# SPRING CONFIG - using environment property only (ConfigFileApplicationListener)
spring.config.location= # Config file locations.
spring.config.name=application # Config file name.
因此,在application.properties上编写上述属性如下所示。
spring.config.location=file:C:\\properties\\application.yml
spring.config.name=application
并重新加载.yml属性(我没有尝试执行jar。我通过控制器使用war和测试)
@Component
@ConfigurationProperties(prefix="environments")
public class YamlProperties {
private Map<String, String> dev = new HashMap<>();
private Map<String, String> prod = new HashMap<>();
public Map<String, String> getDev() {
return this.dev;
}
public Map<String, String> getProd() {
return this.prod;
}
}
这段代码没有从本地文件系统C:.yml加载驱动器,但是当在资源文件夹中添加application.yml文件时,它工作得很好。
那么如何为load .yml设置spring.config.location呢?
我想知道为什么location属性仍然有效,尽管从1.4版本开始就不推荐使用它了。
想知道如何使用ConfigFileApplicationListener,我跟不上代码,很难理解,给点小贴士~!
编辑:
当war再次启动并使用本地文件系统属性再次创建上下文时,我错过了对此的理解。这不是集合链接,而是为将来的步骤保留。
tomcat重启比war部署新的文件,所以我将文件放在本地系统上,如果我更改了这个文件的数据,那么它包含属性,当tomcat重启时,可以更新属性上下文。
为什么我一直尝试这项工作,我使用公共github帐户和保护连接数据库信息到其他东西。我得到了这些东西,然后继续下一期,比如git-encrpt,spring-cloude,nginx,docker。谢谢你的帮助,真的很有用。
https://stackoverflow.com/questions/40914239
复制相似问题