我正在尝试通过界面使用Play / Guice依赖注入:
public interface IService {
Result handleRequest();
}
public Service implements IService {
@Override
public Result handleRequest() {
...
return result;
}
}
public class Controller {
private final IService service;
@Inject
public Controller(IService service) {
this.service = service;
}
}
我得到:
play.api.UnexpectedException: Unexpected exception[CreationException: Unable to create injector, see the following errors:
1.) No implementation for IService was bound.
如果我将控制器类更改为不使用接口,则可以正常工作:
public class Controller {
private final Service service;
@Inject
public Controller(Service service) {
this.service = service;
}
}
我如何使其与接口配合使用,以便它找到具体的Service类?
答案 0 :(得分:0)
您可以像这样使用guice注释@ImplementedBy
:
import com.google.inject.ImplementedBy;
@ImplementedBy(Service.class)
public interface IService {
Result handleRequest();
}
或者您可以使用如下模块:
import com.google.inject.AbstractModule;
public class ServiceModule extends AbstractModule {
protected void configure() {
bind(IService.class).to(Service.class);
}
}
然后在application.conf play.modules.enabled += "modules.ServiceModule"