如何从其他文件加载基于Spring-boot的REST应用程序属性?

时间:2015-01-30 10:55:11

标签: java spring spring-boot

似乎spring-boot启动程序在以下位置查找application.{xml|properties|yml|yaml}文件以加载属性。有没有办法指定不同的文件名或文件来加载属性?

$ java -Ddebug -jar target/app-1.0.jar
16 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./config/application.properties' resource not found
16 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./config/application.yaml' resource not found
16 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./config/application.yml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/application.properties' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/application.xml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/application.yaml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/application.yml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/config/application.properties' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/config/application.xml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/config/application.yaml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'classpath:/config/application.yml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./application.properties' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./application.xml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./application.yaml' resource not found
32 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./application.yml' resource not found
6 [main] DEBUG org.springframework.boot.context.config.ConfigFileApplicationListener  - Skipped config file 'file:./config/application.xml' resource not found

修改

有没有办法指定另一个Bean配置文件(比如application-context.xml)?

3 个答案:

答案 0 :(得分:3)

您需要使用@ConfigurationProperties来加载属性。

@ConfigurationProperties(locations = {"yourProperties.xml"})

答案 1 :(得分:2)

您可以设置弹簧在启动期间搜索配置文件的文件夹。

java -jar target/app-1.0.jar -Dspring.config.location=your/config/dir/

答案 2 :(得分:1)

在命令行中,您可以使用下面的属性来提及其他引导配置文件:

--spring.config.name="file:/path/to/application.properties"

另一种选择是:

-Dspring.config.name="file:/path/to/application.properties"

请注意,字符为小写,单词分隔符为句点

否则,您可以使用已使用的密钥的环境变量:

  • 在* nix系统中:

    export SPRING_CONFIG_NAME=file:/path/to/application.properties
    
  • 在Windows操作系统中:

    set SPRING_CONFIG_NAME=file:/path/to/application.properties
    

有关following Docs resource Spring Boot配置的更多信息。