简体   繁体   English

在Guice中绑定二维数组

[英]Binding a 2-D array in Guice

I have to create an object of ModelWeights using Guice Dependency injection. 我必须使用Guice Dependency Injection创建一个ModelWeights对象。 How can I bind a double[][] array using Guice dependency injection during runtime? 如何在运行时使用Guice依赖注入绑定double[][]数组?

public class MW {

    private double[][] weights;
    private LogConditionalObjectiveFunction objectiveFunction;

    @Inject
    public MW(double[][] weights, LogConditionalObjectiveFunction func)
    {
        this.weights = weights;
        this.objectiveFunction = func;
    }

    public double[][] getWeights()
    {
        return this.weights;
    }

    public LogConditionalObjectiveFunction getObjectiveFunction()
    {
        return this.objectiveFunction;
    }
}

I got this while trying several approaches: 我在尝试几种方法时得到了这个:

1) No implementation for double[][] was bound.
  while locating double[][]
    for parameter 0 at com.data.MW.<init>(MW.java:13)
  while locating com.data.MW
    for parameter 0 at com.predictor.impl.MEP.<init>(MEP.java:50)
  at     com.ServletDependencyInjector$1.configureServlets(ServletDependencyInjector.java:72)

Use Guice constants binding 使用Guice常数绑定

@Inject
public ModelWeights(@Named("MyMatrix") double[][] weights, LogConditionalObjectiveFunction func) {
        this.weights = weights;
        this.objectiveFunction = func;
}

And in your Guice setup code 在您的Guice设置代码中

@Override
protected void configure() {
   bind(double[][].class).annotatedWith(Names.named("MyMatrix")).toInstance(MY_MATRIX); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM