Using Google Guice to inject java properties

前端 未结 5 1036
灰色年华
灰色年华 2021-02-02 14:24

I want to use google guice to make properties available in all classes of my application. I defined a Module which loads and binds the properties file Test.properties.<

5条回答
  •  清歌不尽
    2021-02-02 14:51

    The library Guice configuration can inject for you values from Properties or JSON files to your services.

    You can inject from the file application.properties to your service as :

    @BindConfig(value = "application", syntax = PROPERTIES)
    public class Service {
    
        @InjectConfig
        private int port;
    
        @InjectConfig
        private String url;
    
        @InjectConfig
        private Optional timeout;
    }
    

    You must simply install the modules ConfigurationModule

    public class GuiceModule extends AbstractModule {
        @Override
        protected void configure() {
            install(ConfigurationModule.create());
            requestInjection(Service.class);
        }
    }
    

提交回复
热议问题