java Spring Boot 使用 Yaml 而不是属性文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39474133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot using Yaml instead of properties file
提问by Panciz
I use Spring Boot. I would like to write my configuration using YAML instead of properties.
我使用 Spring Boot。我想使用 YAML 而不是属性来编写我的配置。
Since I use spring-boot-starter
the SnakeYAML library is already in the classpath, and SpringApplication should use the YAML version automatically.
由于我使用spring-boot-starter
的 SnakeYAML 库已经在类路径中,SpringApplication 应该自动使用 YAML 版本。
The SpringApplication class will automatically support YAML as an alternative to properties whenever you have the SnakeYAML library on your classpath.
只要您的类路径上有 SnakeYAML 库,SpringApplication 类就会自动支持 YAML 作为属性的替代方案。
The problem is that the application keep using the application.properties file and if I remove it no configuration are loaded at all.
问题是应用程序继续使用 application.properties 文件,如果我删除它,则根本不会加载任何配置。
Can someone help me? this is my main file
有人能帮我吗?这是我的主文件
@SpringBootApplication
public class App {
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(App.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
}
this is my pom.xml
这是我的 pom.xml
....
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
the application.yml file is just
application.yml 文件只是
tasks: 231232
and I try reading the property using the injected environment
我尝试使用注入的环境读取属性
@Autowired
private Environment environment;
....
log.info(environment.getProperty("tasks"));
Where is my mistake?
我的错误在哪里?
回答by Panciz
I solve my problem adding
我解决了我的问题添加
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.16</version>
to my pom.xml file. Notice 1.16, the spring-boot-starter-parent imports 1.17.
到我的 pom.xml 文件。注意1.16, spring-boot-starter-parent 导入1.17。
I open an issue https://github.com/spring-projects/spring-boot/issues/6878
我打开一个问题https://github.com/spring-projects/spring-boot/issues/6878
回答by byte mamba
If the specified version has conflicts, you can use this method to remove the version.
如果指定的版本有冲突,可以使用此方法删除版本。
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
回答by Kalyan Chavali
回答by Panciz
The problem was simply that snakeyaml 1.7 jar was corrupted in my local repository. Probably the class was not loaded at run time.
问题很简单,我的本地存储库中的snakeyaml 1.7 jar 已损坏。可能该类没有在运行时加载。
The fact that I do not recieve any error at build not at run time mislead me.
我在构建时没有收到任何错误而不是在运行时这一事实误导了我。
回答by Aman Tuladhar
Did you add space after tasks:
Make sure you add the space after colon ( : ). Also in yml indention plays a vital role
您是否在之后tasks:
添加了空格 确保在冒号 ( : ) 之后添加空格。同样在 yml 缩进中起着至关重要的作用
tasks: 123
回答by jonas.lima
use ConfigurationProperties annotation in main class
在主类中使用 ConfigurationProperties 注释
@SpringBootApplication
@ConfigurationProperties
public class App {
public static void main(String[] args) throws Exception {
SpringApplication app = new SpringApplication(App.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
}
回答by satish chennupati
The issue seems to be with your yml file
问题似乎与您的 yml 文件有关
tasks:231232
should be
应该
tasks: 231232