我使用Spring Boot。 我想寫我的配置使用YAML而不是屬性。使用Yaml代替屬性文件的彈簧啓動
由於我使用spring-boot-starter
SnakeYAML庫已經在類路徑中,並且SpringApplication應該自動使用YAML版本。
的SpringApplication類將自動支持YAML作爲 替代的屬性,只要你有對 classpath中SnakeYAML庫。
問題是應用程序繼續使用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"));
我的錯誤在哪裏?
你可以嘗試創建一個'@'類型的屬性,它是用'@Value(「$ {tasks}」)註釋'並檢查它的值嗎? –
所有內容都有詳細記錄[boot-features-external-config](http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html) – Patrick
Could not解決佔位符'任務'的字符串值「$ {tasks}」 – Panciz