简体   繁体   English

Play 框架依赖注入不起作用

[英]Play framework dependency injection is not working

I have tried dependency injection example from here https://dzone.com/articles/guicing-play-framework我从这里尝试了依赖注入示例https://dzone.com/articles/guicing-play-framework

Below is my code Controller:下面是我的代码控制器:

public class TestController extends Controller{
  @Inject
  private Testing test;

  public Result result() {
    test.tt();
    return ok();
  } 
}

Service Interface code:服务接口代码:

public interface Testing {
  public String tt();
}

ServiceImpl code:服务实现代码:

public class Testingimpl implements Testing{
  @Override
  public String tt() {
    return "test";
  }
}

I am getting this error我收到此错误

CreationException: Unable to create injector CreationException:无法创建注入器

If I do this, this works.如果我这样做,这有效。

public class TestController extends Controller{
  @Inject
  private TestingImpl test;

  public Result result() {
    test.tt();
    return ok();
  } 
}

How to resolve this?如何解决这个问题?

You forgot to bind interface to your implementation.您忘记将接口绑定到您的实现。 If you have one implementation change your interface like:如果您有一个实现,请更改您的界面,例如:

import com.google.inject.ImplementedBy;

@ImplementedBy(Testingimpl.class)
public interface Testing {
    public String tt();
}

For more complex solution you can use programmatic bindings: https://www.playframework.com/documentation/2.7.x/JavaDependencyInjection#Programmatic-bindings对于更复杂的解决方案,您可以使用编程绑定: https : //www.playframework.com/documentation/2.7.x/JavaDependencyInjection#Programmatic-bindings

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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