Spring-Boot multi module project load property-file

前端 未结 5 1124
面向向阳花
面向向阳花 2021-02-01 18:21

I have a Spring-Boot-Application as a multimodule-Project in maven. The structure is as follows:

Parent-Project
|--MainApplication
|--Module1
|--ModuleN
<         


        
5条回答
  •  半阙折子戏
    2021-02-01 19:03

    You could autowire and use the Enviornment bean to read the property

    @Configuration
    @PropertySource(value = "classpath:tmdb.properties")
    public class TMDbConfig {
    
      @Autowired
      private Environment env;
    
      public String getApiKey() {
        return env.getRequiredProperty("moviedb.tmdb.api-key");
      }
    
    }
    

    This should guarantee that property is read from the context when you invoke the getApiKey() method regardless of when the @Value expression is resolved by PropertySourcesPlaceholderConfigurer.

提交回复
热议问题