播放框架依赖注入不起作用
Play framework dependency injection is not working
我试过这里的依赖注入示例
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 代码:
public class Testingimpl implements Testing{
@Override
public String tt() {
return "test";
}
}
我遇到了这个错误
CreationException: Unable to create injector
如果我这样做,就可以了。
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
我试过这里的依赖注入示例 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 代码:
public class Testingimpl implements Testing{
@Override
public String tt() {
return "test";
}
}
我遇到了这个错误
CreationException: Unable to create injector
如果我这样做,就可以了。
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