Spring Boot: Multiple similar ConfigurationProperties with different Prefixes

后端 未结 4 994
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-29 07:45

I\'m using Spring Boot and have two very similar services which I\'d like to configure in my application.yml.

The configuration looks roughly like this:

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-29 08:09

    I achieved almost same thing that you trying. first, register each properties beans.

    @Bean
    @ConfigurationProperties(prefix = "serviceA")
    public ServiceProperties  serviceAProperties() {
        return new ServiceProperties ();
    }
    
    @Bean
    @ConfigurationProperties(prefix = "serviceB")
    public ServiceProperties  serviceBProperties() {
        return new ServiceProperties ();
    }
    

    and at service(or someplace where will use properties) put a @Qualifier and specified which property would be auto wired .

    public class ServiceA {
        @Autowired
        @Qualifier("serviceAProperties")
        private ServiceProperties serviceAProperties;
    
    }
    

提交回复
热议问题