how to get my configuration values in yml - using dropwizard (microservice) Jersey D.I @Injection?

前端 未结 1 1196
独厮守ぢ
独厮守ぢ 2021-01-05 08:38

here\'s my code snippets.

here\'s my yml file:

productionServer:
  host: production-server.amazonaws.com
  publicIp: xx.xx.xx.xx
  privateIp: xx.xx.x         


        
1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-05 09:05

    The problem is, with this configuration

    bind(MyConfiguration.class).to(MyConfiguration.class);
    

    HK2 will create a new instance of the MyConfiguration. It will not be the same instance populated by DW. What you can do though, is use the instance created by DW, by simply binding that same instance in your HK2 configuration

    public class MyApplication extends Application {
    
        @Override
        public void run(final MyConfiguration config, Environment env) {
            env.jersey().register(new AbstractBinder() {
                @Override
                protected void configure() {
                    bind(config).to(MyConfiguration.class);
                }
            });
        }
    }
    

    0 讨论(0)
提交回复
热议问题