0

Spent some time on searching, found few similar links but still couldn't able to solve this.

Finally, posting in here, hoping someone will share information on how to accomplish below

Goal: Inject objects having constructor with arguments.

@Path("/test")
@Produces({MediaType.APPLICATION_JSON})
public class DummyApi {

    // with count = 1
    public DummyObject dummyObj1; 

    // with count = 2
    public DummyObject dummyObj2;

    @Inject
    public DummyApi(@CountVal(1) DummyObject dObj1, @CountVal(2) DummyObject dObj2) {
        this.dummyObj1 = dObj1;
        this.dummyObj2 = dObj2;
    }

    @GET @Path("/invoke")
    public String invoke()
    {
        return "";
    }       
}

public class DummyObject
{
    private int count;

    @Inject
    public DummyObject (int value)
    {
        this.count = value;
    }
}
rawat
  • 165
  • 1
  • 2
  • 15
  • Where is the integer value supposed to be coming from? – Paul Samsotha Jun 05 '18 at 17:40
  • @PaulSamsotha edited the post but not sure if am correct. can you please advice!! – rawat Jun 06 '18 at 00:08
  • 1
    You can get some ideas from [this](https://stackoverflow.com/a/41436316/2587435) – Paul Samsotha Jun 06 '18 at 06:55
  • If you're replying DI to inject and instantiate all your instances; then all the classes and their dependencies must be registered with the DI library you're using (HK2 in this case, ie. bind() them). Later, you can just create constructors with @Inject annotation and the DI tool will handle the instantiation. More information [here](https://stackoverflow.com/a/48663987/2141670) – Dhruvil Vaghela Jul 27 '18 at 13:09
  • Possible duplicate of [Dropwizard HK2 injection](https://stackoverflow.com/questions/48662531/dropwizard-hk2-injection) – Dhruvil Vaghela Jul 27 '18 at 13:12

0 Answers0