我使用了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。谢谢你的帮助,真的很有用。
发布于 2016-12-02 11:34:26
因为您使用的是spring boot 1.4.2,所以一切都很简单。您只需要为您的应用程序指定额外的参数,比如java -jar name-of-application-version.jar --spring.config.location=file:///C:/properties/application.yml
。一种更常见的方法是为JVM定义额外的选项,比如java -Dspring.config.location=file:///d:/private-bootstrap.yml -jar name-of-application-version.jar
。这两种方法都应该有效,因为它们在我的桌面上工作得很好。
发布于 2018-05-10 03:58:10
您可以在调用Spring Boot应用程序时使用spring.config.location属性。下面是一个示例,其中您在自定义位置中有4个自定义名称的属性文件。
java -Dspring.config.location=file:/gfs/config/post/post.properties,/gfs/config/post/post_pwd.properties,/myDbConfig/config/db-config.properties,/myserver/config/db-passwd.properties -jar myapp-1.0-SNAPSHOT.jar
在您的代码中,您可以通过以下方式访问它们:
@Component
public class AppProperties {
@Value("${post.user.name}")
private String username;
}
https://stackoverflow.com/questions/40914239
复制相似问题