【问题标题】:Guice and general application configurationGuice 和一般应用程序配置
【发布时间】:2011-06-15 21:46:05
【问题描述】:

对于用 Java 编写的监控软件,我考虑使用 Google Guice 作为 DI 提供程序。项目需要从外部资源(文件或数据库)加载其配置。该应用程序设计为在独立模式或 servlet 容器中运行。

目前配置不包含依赖注入的绑定或参数,仅包含一些全局应用程序设置(JDBC 连接定义和关联的数据库管理/监控对象)。

我看到两个选项:

  • 为 Guice 使用基于文件的插件,例如 guice-xml-config 来存储应用程序选项(这将允许稍后在必要时配置 DI 部分)。

您会建议将 Guice 用于这两个任务,还是将通用应用程序配置与依赖注入分开?您认为哪些优点和缺点最重要?

【问题讨论】:

    标签: java configuration guice apache-commons-config


    【解决方案1】:

    在 Guice 模块中 slurp 属性文件很简单:

    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) {
            //...
        }
      }
    } 
    

    以后很容易从属性切换到其他配置源。

    [编辑]

    顺便说一句,您可以通过使用@Named("myKey") 注释来获取注入的属性。

    【讨论】:

    • +1 我认为您还应该提及如何使用@Named 注入属性。
    【解决方案2】:

    我在自己的项目中遇到了同样的问题。我们已经选择 Guice 作为 DI 框架,为了简单起见,我们还想在配置中使用它。

    我们最终使用 Apache Commons Configuration 从属性文件中读取配置并将它们绑定到 Guice 注入器,如 Guice FAQ How do I inject configuration parameters? 中所建议的那样。

    @Override public void configure() {
        bindConstant().annotatedWith(ConfigurationAnnotation.class)
            .to(configuration.getString("configurationValue"));    
    }
    

    Commons Configuration 支持的重新加载配置在 Guice 注入中也很容易实现。

    @Override public void configure() {
        bind(String.class).annotatedWith(ConfigurationAnnotation.class)
            .toProvider(new Provider<String>() {
                public String get() {
                    return configuration.getString("configurationValue");
                }
        });    
    }
    

    【讨论】:

      【解决方案3】:

      检查州长库:

      https://github.com/Netflix/governator/wiki/Configuration-Mapping

      您将获得一个@Configuration 注解和几个配置提供程序。在代码中,它有助于查看您使用的配置参数在哪里:

      @Configuration("configs.qty.things")
      private int   numberOfThings = 10;
      

      此外,您将在启动时获得一份不错的配置报告:

      https://github.com/Netflix/governator/wiki/Configuration-Mapping#configuration-documentation

      【讨论】:

        【解决方案4】:

        试试 maven central 上的Guice configuration,它支持属性、HOCON 和 JSON 格式。

        您可以将文件 application.conf 中的属性注入到您的服务中:

        @BindConfig(value = "application")
        public class Service {
        
            @InjectConfig
            private int port;
        
            @InjectConfig
            private String url;
        
            @InjectConfig
            private Optional<Integer> timeout;
        
            @InjectConfig("services")
            private ServiceConfiguration services;
        }
        

        您必须将模块 ConfigurationModule 安装为

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

        【讨论】:

          猜你喜欢
          • 2011-09-15
          • 2011-06-23
          • 2016-01-01
          • 2017-10-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多