Search code examples
javagwtguicegwt2guice-3

GWT - Inject value using Guice @Named


I have Utility class as below.

class Utility{
  @Inject
  @Named("endpoint")
  private String endpoint; //generated only getter
}

I am configuring value for endpoint in the as below.

public class ConfigModule extends AbstractModule {
  @Override
  protected void configure() {
    bindConstant().annotatedWith(Names.named("endpoint"))
    .to("Endpoint URL");
  }
}

In my GWT class I am trying to get this value.

public class MyUIPanel extends Composite {
  @Override
  protected void onLoad() {
    Window.Location.assign(new Utility().getEndpoint());
  }
}

UI loaded properly with error coming up in browser console saying You are executing Names.named() in GWT code. GWT does not emulate enough of Java that will work.

Also tried to inject Utility class in MyUIPanel class using guice but getting below error at the time of building war Binding requested for constant key Key[type=java.lang.String, [email protected](value=CognitoEndpoint)] but no explicit binding was found.


Solution

  • Found out Guice cannot be used on client side.

    To fetch static values inside our GWT Client Code, we can bind those values inside class extending AbstractGinModule class.

    To fetch dynamic values from properties files, write logic on server side and get values on client side using GWT RPC.