Spring boot how to read properties file outside jar

后端 未结 6 1768
悲&欢浪女
悲&欢浪女 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:35

    Found an solution:

    first create a class and add @ConfigurationProperties

    @ConfigurationProperties(prefix = "asdf", locations = "file:conf/aaa.properties")
    public class ASDF {
        private String name;   
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    

    Please noted in locations, i use file, not classpath.

    then in your application class, add @EnableConfigurationProperties

    @SpringBootApplication
    @EnableConfigurationProperties({ASDF.class, BBB.class})
    public class InitialBeanTestApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(InitialBeanTestApplication.class, args);
        }
    }
    

    then you can read config file in the conf folder

提交回复
热议问题