Search code examples
javadependency-injectionguice

Google Guice binding not working with Integer and Long


Error when using Google Guice.

Could not find a suitable constructor in java.lang.Long. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private. at java.lang.Long.class

class DIModule extends AbstractModule {
   protected void configure(){
      bind(Long.class).annotatedWith(Names.named("seconds")).toInstance(1574965800000L);
   }
}

Using lombok for Constructor

@AllArgsConstructor (onConstructor_ = { @Inject })
class Cat{
   @Named("seconds")
   private Long time;
}

But below one works without error.

class Cat{
  private Long time;
  @Inject
  public Cat(@Named("seconds") Long time){
     this.time = time;
  }
}

can someone explain me how to make it work with Lombok?


Solution

  • You should be able to have all instances of the @Named annotations on fields copied to the corresponding constructor parameter by adding lombok.copyableAnnotations to your lombok.config file, like this:

    lombok.copyableAnnotations += com.google.inject.name.Named