Pan*_*ciz 6 java spring yaml spring-boot
我使用Spring Boot。我想使用 YAML 而不是属性来编写配置。
由于我使用的spring-boot-starterSnakeYAML 库已经在类路径中,并且 SpringApplication 应该自动使用 YAML 版本。
只要您的类路径上有 SnakeYAML 库,SpringApplication 类就会自动支持 YAML 作为属性的替代方案。
问题是应用程序继续使用 application.properties 文件,如果我删除它,则根本不会加载任何配置。
有人能帮我吗?这是我的主文件
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的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>
Run Code Online (Sandbox Code Playgroud)
application.yml 文件只是
tasks: 231232
Run Code Online (Sandbox Code Playgroud)
我尝试使用注入的环境读取属性
@Autowired
private Environment environment;
....
log.info(environment.getProperty("tasks"));
Run Code Online (Sandbox Code Playgroud)
我的错误在哪里?
我解决了我的问题添加
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.16</version>
Run Code Online (Sandbox Code Playgroud)
到我的 pom.xml 文件。注意1.16, spring-boot-starter-parent 导入1.17。
我打开一个问题https://github.com/spring-projects/spring-boot/issues/6878
您是否在后添加了空格tasks:请确保在冒号 (:) 后添加了空格。在 yml 中缩进也起着至关重要的作用
tasks: 123
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23962 次 |
| 最近记录: |