A.Dumas
A.Dumas

Reputation: 3277

How to bind a object with Guice in Dropwizard?

I want to use a bind object in my service class with guice in dropwizard.

Consider the object

public class MinioData {
  private String endpoint;
public String getEndpoint() {
    return endpoint;
  }
}

And a service

@Path("/upload")
@Produces(MediaType.APPLICATION_JSON)
public class UploadResource {
private final MinioData minioData;
@Inject
public UploadResource(
    @Named("miniodata") MinioData minioData) {
  this.minioData = minioData;
}

How can I bind this object so that can be used in my service. For a String I could use

bindConstant()
.annotatedWith(Names.named("miniodata"))
.to(configuration.getMiniodata());

but since in this case it is a general object how would I bind it?

Upvotes: 0

Views: 1014

Answers (1)

André Barbosa
André Barbosa

Reputation: 724

If you have an existing Guice module configured in your DW application, you can just bind the MinioData instance from the configuration object to the associated class:

binder.bind(MinioData.class).toInstance(configuration.getMiniodata());

Upvotes: 1

Related Questions