简体   繁体   中英

Binding a 2-D array in Guice

I have to create an object of ModelWeights using Guice Dependency injection. How can I bind a double[][] array using Guice dependency injection during runtime?

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

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

And in your Guice setup code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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