Spring-Boot multi module project load property-file

前端 未结 5 1057
面向向阳花
面向向阳花 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 19:17

    For field injection:

    Fields are injected right after construction of a bean, before any config methods are invoked. Such a config field does not have to be public. Refer Autowired annotation for complete usage. Use constructor injection in this case like below:

    @Component
    public class TMDbWarper {
    
        private TMDbConfig tmdbConfig;
    
        private TmdbApi tmdbApi;
    
        @Autowired
        public TMDbWarper(final TMDbConfig tmdbConfig){
                this.tmdbConfig = tmdbConfig;
                tmdbApi = new TmdbApi(tmdbConfig.getApiKey());
        }
    

    (or)

    Use @PostConstruct to initialise like below:

    @Component
    public class TMDbWarper {
    
        @Autowired
        private TMDbConfig tmdbConfig;
    
        private TmdbApi tmdbApi;
    
        @PostConstruct
        public void init() {
            // any initialisation method
            tmdbConfig.getConfig();
        }
    

提交回复
热议问题