我从这里尝试了依赖注入示例https://dzone.com/articles/guicing-play-framework
下面是我的代码控制器:
public class TestController extends Controller{
@Inject
private Testing test;
public Result result() {
test.tt();
return ok();
}
}
服务接口代码:
public interface Testing {
public String tt();
}
ServiceImpl code:
public class Testingimpl implements Testing{
@Override
public String tt() {
return "test";
}
}
我收到此错误
创建异常:无法创建注入器
如果我这样做,这就可以了。
public class TestController extends Controller{
@Inject
private TestingImpl test;
public Result result() {
test.tt();
return ok();
}
}
如何解决这个问题?
你忘了将接口绑定到你的实现。如果你有一个实现,请像这样改变你的界面:
import com.google.inject.ImplementedBy;
@ImplementedBy(Testingimpl.class)
public interface Testing {
public String tt();
}
对于更复杂的解决方案,可以使用编程绑定:https://www.playframework.com/documentation/2.7.x/JavaDependencyInjection#Programmatic-bindings