代码之家  ›  专栏  ›  技术社区  ›  eric2323223

如何将字符串绑定到Guice中的变量?

  •  21
  • eric2323223  · 技术社区  · 17 年前

    我是Guice的新手,这里有一个天真的问题。我了解到,我们可以通过以下方式将字符串绑定到特定值:

    bind(String.class)
            .annotatedWith(Names.named("JDBC URL"))
            .toInstance("jdbc:mysql://localhost/pizza");
    

    但如果我想把字符串绑定到任何可能的字符上呢?

    如何用Guice替换“newsomeclass(stringstrparameter)”呢?

    3 回复  |  直到 10 年前
        1
  •  44
  •   mkobit    10 年前

    您首先需要为 SomeClass :

    class SomeClass {
      @Inject
      SomeClass(@Named("JDBC URL") String jdbcUrl) {
        this.jdbcUrl = jdbcUrl;
      }
    }
    

    我更喜欢使用自定义注释,如下所示:

    class SomeClass {
      @Inject
      SomeClass(@JdbcUrl String jdbcUrl) {
        this.jdbcUrl = jdbcUrl;
      }
    
      @Retention(RetentionPolicy.RUNTIME)
      @Target({ElementType.FIELD, ElementType.PARAMETER})
      @BindingAnnotation
      public @interface JdbcUrl {}
    }
    

    然后,您需要在模块中提供绑定:

    public class SomeModule extends AbstractModule {
      private final String jdbcUrl; // set in constructor
    
      protected void configure() {
        bindConstant().annotatedWith(SomeClass.JdbcUrl.class).to(jdbcUrl);
      }
    }
    

    然后,当Guice创建SomeClass时,它将注入参数。例如,如果SomeOtherClass依赖于SomeClass:

    class SomeOtherClass {
      @Inject
      SomeOtherClass(SomeClass someClass) {
        this.someClass = someClass;
      }
    

    通常,当您认为需要注入字符串时,您需要注入一个对象。例如,如果字符串是URL,我通常会插入 URI 使用绑定注释。

    这一切都假定在模块创建时可以为字符串定义某个常量值。如果该值在模块创建时不可用,则可以使用 AssistedInject .

        2
  •  21
  •   mkobit    10 年前

    这可能与主题无关,但Guice使配置比为所需的每个字符串编写显式绑定容易得多。您可以为它们设置一个配置文件:

    Properties configProps = Properties.load(getClass().getClassLoader().getResourceAsStream("myconfig.properties");
    Names.bindProperties(binder(), configProps);
    

    瞧,你所有的配置都准备好注入了:

    @Provides // use this to have nice creation methods in modules
    public Connection getDBConnection(@Named("dbConnection") String connectionStr,
                                      @Named("dbUser") String user,
                                      @Named("dbPw") String pw,) {
      return DriverManager.getConnection(connectionStr, user, pw);
    }
    

    Java properties file myconfig.properties 在类路径的根目录中,使用

    dbConnection = jdbc:mysql://localhost/test
    dbUser = username
    dbPw = password
    

    或者将来自其他来源的授权信息合并到属性中,就可以设置了。

        3
  •  2
  •   Selvaram G    7 年前

    我能把一根绳子插进去 Named

    @Provides
    @Named("stage")
    String stage() {
        return domain;
    }
    
    class SomeClass {
       @Inject
       @Named("stage")
       String stageName;
    }
    
        4
  •  0
  •   mkobit    10 年前

    我在Guice的FAQ中找到了一个解决方案:

    http://code.google.com/docreader/#p=google-guice&s=google-guice&t=FrequentlyAskedQuestions

    除了在MyModule中定义注释和字符串属性外,我还需要在下面的行中写入以获取SomeClass的实例:

    SomeClass instance = Guice.createInjector(new MyModule("any string i like to use")).getInstance(SomeClass.class);
    

    但是我记得除了根对象之外不应该使用Injector.getInstance(),所以有更好的方法吗?