gpt4 book ai didi

java - 在 Guice 中绑定(bind)二维数组

转载 作者:太空宇宙 更新时间:2023-11-04 06:21:44 24 4
gpt4 key购买 nike

我必须使用 Guice 依赖注入(inject)创建一个 ModelWeights 对象。如何在运行时使用 Guice 依赖注入(inject)绑定(bind) 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;
}
}

我在尝试几种方法时得到了这个:

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)

最佳答案

使用Guice常量绑定(bind)

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

在你的 Guice 设置代码中

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

关于java - 在 Guice 中绑定(bind)二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27343686/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com