Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Guice-默认绑定定义_Java_Binding_Guice - Fatal编程技术网

Java Guice-默认绑定定义

Java Guice-默认绑定定义,java,binding,guice,Java,Binding,Guice,有没有办法在Guice 3.0中声明默认绑定 下面是一个我所期望的例子: //Constructor for Class Impl1 @Inject public Impl1 (@One IMyOwn own) { ... } //Constructor for Class Impl2 @Inject public Impl2 (@Two IMyOwn own) { ... } 实际上,这个示例无法工作,因为我必须为所有注释@One、@Two声明一个绑定 Guice有没有解决方案

有没有办法在Guice 3.0中声明默认绑定

下面是一个我所期望的例子:

//Constructor for Class Impl1
@Inject
public Impl1 (@One IMyOwn own)
{
   ...
}

//Constructor for Class Impl2
@Inject
public Impl2 (@Two IMyOwn own)
{
   ...
}
实际上,这个示例无法工作,因为我必须为所有注释@One、@Two声明一个绑定

Guice有没有解决方案?
谢谢。

Guice会尽可能多地检查您的配置。尽可能地绑定。这也意味着,Guice无法判断@One缺少的绑定是错误还是应该映射到某个默认情况

如果您对细节感兴趣,请在Guice中查找序列。由于第4步和第6步处理绑定注释,而第6步明确禁止使用默认值,所以我认为您运气不好

.6。如果依赖项有绑定注释,请放弃。Guice不会为带注释的依赖项创建默认绑定

因此,您最好向Guice提供一个提示,@One应该像这样映射到默认值:

bind(IMyOwn.class).annotatedWith(One.class).to(IMyOwn.class);

因此,您不需要多次声明具体的默认类DefaultMyOwn。

Guice会尽可能多地检查您的配置。尽可能地绑定。这也意味着,Guice无法判断@One缺少的绑定是错误还是应该映射到某个默认情况

如果您对细节感兴趣,请在Guice中查找序列。由于第4步和第6步处理绑定注释,而第6步明确禁止使用默认值,所以我认为您运气不好

.6。如果依赖项有绑定注释,请放弃。Guice不会为带注释的依赖项创建默认绑定

因此,您最好向Guice提供一个提示,@One应该像这样映射到默认值:

bind(IMyOwn.class).annotatedWith(One.class).to(IMyOwn.class);

因此,您不需要多次声明具体的默认类DefaultMyOwn。

使用@Named绑定

Guice附带一个内置的绑定注释@Named,该注释使用字符串:

public class RealBillingService implements BillingService {
  @Inject
  public RealBillingService(@Named("Checkout") CreditCardProcessor processor) {
    ...
  }
若要绑定特定名称,请使用Names.named创建要传递给annotatedWith的实例:

bind(CreditCardProcessor.class)
    .annotatedWith(Names.named("Checkout"))
    .to(CheckoutCreditCardProcessor.class);
所以在你的情况下

//Constructor for Class Impl1
@Inject
public Impl1 (@Named("One") IMyOwn own)
{
   ...
}

//Constructor for Class Impl2
@Inject
public Impl2 (@Named("Two") IMyOwn own)
{
   ...
}
您的模块将如下所示:

public class MyOwnModule extends AbstractModule {
  @Override 
  protected void configure() {
    bind(IMyOwn.class)
        .annotatedWith(Names.named("One"))
        .to(DefaultMyOwn.class);

    bind(IMyOwn.class)
        .annotatedWith(Names.named("Two"))
        .to(TwoMyOwn.class);
  }
}

使用@Named绑定

Guice附带一个内置的绑定注释@Named,该注释使用字符串:

public class RealBillingService implements BillingService {
  @Inject
  public RealBillingService(@Named("Checkout") CreditCardProcessor processor) {
    ...
  }
若要绑定特定名称,请使用Names.named创建要传递给annotatedWith的实例:

bind(CreditCardProcessor.class)
    .annotatedWith(Names.named("Checkout"))
    .to(CheckoutCreditCardProcessor.class);
所以在你的情况下

//Constructor for Class Impl1
@Inject
public Impl1 (@Named("One") IMyOwn own)
{
   ...
}

//Constructor for Class Impl2
@Inject
public Impl2 (@Named("Two") IMyOwn own)
{
   ...
}
您的模块将如下所示:

public class MyOwnModule extends AbstractModule {
  @Override 
  protected void configure() {
    bind(IMyOwn.class)
        .annotatedWith(Names.named("One"))
        .to(DefaultMyOwn.class);

    bind(IMyOwn.class)
        .annotatedWith(Names.named("Two"))
        .to(TwoMyOwn.class);
  }
}
有了Guice 4.X,就有了

在Guice 3.0中,您可以利用默认构造函数的自动绑定

使用单个@Inject或public无参数构造函数。 但这是有限制的,因为默认构造函数必须是同一个具体类,所以派生可能会变得很麻烦

有了guice4.X就有了

在Guice 3.0中,您可以利用默认构造函数的自动绑定

使用单个@Inject或public无参数构造函数。
但这是有限制的,因为默认构造函数必须是同一个具体类,所以派生可能会变得很麻烦

我必须使用ToConstructor绑定吗?关于toConstructor绑定:这适用于不能用@Inject注释构造函数的情况。既然可以,这对您来说不是问题。我必须使用ToConstructor绑定吗?关于toConstructor绑定:这适用于不能用@Inject注释构造函数的情况。既然你可以,这对你来说不是问题。如果这是唯一的办法,那会有点脏。。谢谢你的回答。顺便说一句,如果这是唯一的办法的话,那会有点脏。。谢谢你的回答。在这种情况下,你不能绑定实现吗?binImpl1.class.toDefaultMyOwn.class?这是实现OP尝试执行的操作的正确方法。在这种情况下,您不能只绑定实现吗?binImpl1.class.toDefaultMyOwn.class?这是实现OP尝试执行的操作的正确方法。