Dropwizard HK2 injection

后端 未结 1 2048
说谎
说谎 2021-01-19 02:46

im pretty new in working with dropwizard. Currently I\'m trying to implement the HK2 dependency injection. That works pretty fine inside a resource but it doesn\'t work outs

相关标签:
1条回答
  • 2021-01-19 03:15

    If you are going to instantiate the service yourself then it is not going to go through the DI lifecycle and will never be injected. You can let the container create the service if you just register the services as a class

    bind(ContentModuleManager.class)
        .to(ContentModuleManager.class)
        .in(Singleton.class);
    

    On the other hand, if you are creating all the services yourself and you have all of them available, why don't you just not use the DI container at all? Just pass all the services through the constructor. Whether you are using constructor injection1 or manually passing through the constructor, getting the service through the constructor is good practice anyway, as it allows for easier testing of the service.


    1 - Constructor injection

    private Service service;
    
    @Inject
    public OtherService(Service service) {
       this.service = service;
    }
    
    0 讨论(0)
提交回复
热议问题