Play framework dependency injection is not working

user3334226 :

I have tried dependency injection example from here 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

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?

Yevgen :

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python Dependency Injection Framework

Dependency injection into ResourceFilter not working?

Play framework java dependency injection - when to use singletons

Constructor Dependency Injection in Spring Framework

(Play 2.4) Dependency injection in a trait?

Play Framework 2.4 - Dependency injection to replace GlobalSettings.onStart()

Play Framework dependency injection Object vs @Singleton Class

Dependency Injection Does not work in Models or Tests in Play Framework 2.4.x

Dependency injection with abstract class and object in Play Framework 2.5

Play Framework PathBindable with Dependency Injection

Dependency Injection Slim Framework 3

Laravel dependency injection not working

Spring dependency injection not working

Dependency Injection using ninject not working

Play Framework: Dependency Injection in Sub-Templates

Entity Framework Dependency Injection

dependency injection on model is not working

Play Framework without dependency injection?

Dependency injection with Play Framework and Guice leads to NullPointerException on Application start

Dependency Injection framework

Dependency injection and Entity Framework

Simple Dependency injection not working

ScalaWS in play framework 2.5 and dependency injection of WSClient in custom class

Play Framework 2.5 dependency injection in form object bean (Java)

Dependency Injection (DI) in Scala with Play

Working with dependency injection in mvc

Scala Dependency Injection in Play Framework

Play framework compile-time dependency injection and singleton

MAUI: Dependency injection is not working