我使用Spring Boot . 我想使用YAML而不是属性来编写配置 .
由于我使用 spring-boot-starter
,SnakeYAML库已经在类路径中,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);
}
}
这是我的 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>
application.yml文件就是
tasks: 231232
我尝试使用注入的环境阅读属性
@Autowired
private Environment environment;
....
log.info(environment.getProperty("tasks"));
我的错误在哪里?
5 回答
我解决了我的问题
到我的pom.xml文件 . 注意 1.16 ,spring-boot-starter-parent导入 1.17 .
我打开一个问题https://github.com/spring-projects/spring-boot/issues/6878
问题很简单,我的本地存储库中的snakeyaml 1.7 jar被破坏了 . 可能是在运行时没有加载类 .
我没有在运行时收到任何错误的事实误导了我 .
你是否在
tasks:
之后添加空格确保在冒号(:)之后添加空格 . yml indention也起着至关重要的作用Here a syntax page
在主类中使用ConfigurationProperties批注
问题似乎与你的yml文件有关
应该