Spring-Boot multi module project load property-file

前端 未结 5 1048
面向向阳花
面向向阳花 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:12

    Autowiring is performed just after the creation of the object(after calling the constructor via reflection). So NullPointerException is expected in your constructor as tmdbConfig field would be null during invocation of constructor

    You may fix this by using the @PostConstruct callback method as shown below:

    @Component
    public class TMDbWarper {
    
        @Autowired
        private TMDbConfig tmdbConfig;
    
        private TmdbApi tmdbApi;
    
        public TMDbWarper() {
    
        }
    
        @PostConstruct
        public void init() {
            tmdbApi = new TmdbApi(tmdbConfig.getApiKey());
        }
    
        public TmdbApi getTmdbApi() {
            return this.tmdbApi;
        }
    }
    

    Rest of your configuration seems correct to me.

    Hope this helps.

提交回复
热议问题