Spring boot how to read properties file outside jar

后端 未结 6 1724
悲&欢浪女
悲&欢浪女 2020-11-30 04:42

in my target folder, there are 2 folders, lib and conf. all the properties files are placed in conf folder, and jars are placed in lib foulder.

previous to spring

6条回答
  •  执笔经年
    2020-11-30 05:47

    As mentioned in the Spring Boot docs,

    SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:

    1. A /config subdirectory of the current directory.
    2. The current directory
    3. A classpath /config package
    4. The classpath root

    The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).

    One way is to simply rename your 'conf' directory to 'config' and it will work without a problem. So there is no need to do extra configuration until and unless you want your properties file at some location other than the 4 mentioned above.

    In that case you can define the property source explicitly.

    @PropertySource("classpath:config.properties")
    

    and for multiple properties file

    @PropertySources({
        @PropertySource("classpath:config.properties"),
        @PropertySource("classpath:logging.properties"),
        @PropertySource(value="classpath:missing.properties", ignoreResourceNotFound=true)
    })
    

提交回复
热议问题