Guice and general application configuration

前端 未结 4 1766
时光说笑
时光说笑 2021-02-04 01:30

For a monitoring software written in Java I consider using Google Guice as DI provider. The project needs to load its configuration from an external resource (file or database).

4条回答
  •  别那么骄傲
    2021-02-04 02:08

    It's straightforward to slurp a property file in a Guice module:

    public class MyModule extends AbstractModule {
    
      @Override
      protected void configure() {
        try {
            Properties properties = new Properties();
            properties.load(new FileReader("my.properties"));
            Names.bindProperties(binder(), properties);
        } catch (IOException ex) {
            //...
        }
      }
    } 
    

    Later it's easy to switch from Properties to other config sources.

    [Edit]

    BTW, you can get the injected properties by annotating it with @Named("myKey").

提交回复
热议问题