2016-09-13 58 views
0

我使用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")); 

我的錯誤在哪裏?

+0

你可以嘗試創建一個'@'類型的屬性,它是用'@Value(「$ {tasks}」)註釋'並檢查它的值嗎? –

+0

所有內容都有詳細記錄[boot-features-external-config](http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html) – Patrick

+0

Could not解決佔位符'任務'的字符串值「$ {tasks}」 – Panciz

回答

0

您是否在tasks:之後添加空格請確保在冒號(:)後面添加空格。此外,在陽明縮進起着至關重要的作用

tasks: 123 

Here a syntax page

+0

好的,謝謝我加了,但沒有結果 – Panciz

+0

無論如何,我糾正了我原來的帖子 – Panciz

+0

你在哪裏放置你的application.yml文件 –

-3

這個問題似乎是與您的YML文件

tasks:231232 

應該在主

tasks: 231232 
0

使用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); 
    } 
} 
0

的問題只是在於snakeyaml 1.7罐子是在我的本地庫損壞。可能這個班在運行時沒有加載。

事實上,我沒有收到任何錯誤在構建不在運行時誤導我。