[英]springboot how to load properties from custom yml
I have a property bean (example): 我有一个属性bean(示例):
@Data
public class MyProperty {
private String name;
private String address;
}
and I have a custom yml file in classpath named: my_property.yml
我在类路径中有一个自定义的yml文件,名为: my_property.yml
my.property:
name: testName
address: testAddress
How to load this file to my property bean? 如何将此文件加载到我的属性bean? Not using @PropertyResource
, because I want to use the yml file. 不使用@PropertyResource
,因为我要使用yml文件。 Thanks. 谢谢。
There are two properties you can look at - 您可以查看两个属性-
spring.config.name - If you don't like application as your file name for eg- spring.config.name-如果您不喜欢application作为您的文件名,例如-
java -Dspring.config.name = my_property myjar.jar
spring.config.location - To tell where your files lie spring.config.location-告诉您文件的位置
More information is well-documented here 有关详细信息,请参见此处
@PropertySource
is the way to include external/custom properties but unfortunately is doesn't work with yaml files due to this issue . @PropertySource
是包括外部/自定义属性的方法,但是由于这个问题,不幸的是不适用于yaml文件。
You need to load these yaml files on your own by simply writing EnvironmentPostProcessor
and adding it META-INF/spring.factories
. 您只需简单地编写EnvironmentPostProcessor
并将其添加到META-INF/spring.factories
即可自行加载这些yaml文件。
This is described in below Spring documentation (slight difference among versions). 下面的Spring文档(版本之间的细微差异)对此进行了描述。
Spring boot 1.5.x 春季靴1.5.x
Spring boot 2.x Spring Boot 2.x
Follow: http://www.baeldung.com/spring-yaml 追踪: http : //www.baeldung.com/spring-yaml
The relative path of application.yml
file is /myApplication/src/main/resources/application.yml
. application.yml
文件的相对路径是/myApplication/src/main/resources/application.yml
。
and access is: 访问是:
@Autowired
private YAMLConfig myConfig;
I put my application.properties
file at src/main/resources/
End my @SpringBootApplication
read. 我将我的application.properties
文件放在src/main/resources/
结束我的@SpringBootApplication
阅读。
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=***
spring.datasource.username=***
spring.datasource.password=***
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.