How do I inject constants with hk2 in jersey 2.0?

删除回忆录丶 提交于 2019-12-22 04:18:14

问题


How do I inject a constant into some class using HK2 in jersey? With Guice I could have some class like

public class DependsOnFoo {

    @Inject
    public DependsOnFoo(@Named("FOO") String foo) {
        ...
    }
    ...
}

and I would configure it in the injector with something like

bind(String.class).named("FOO").toInstance(new String("foo"))

What's the equivalent, if any, in HK2?


回答1:


I'm in the process of learning hk2 coming from Guice. Honestly I am still in the weeds a little with the complexity of hk2 vs the simplicity of guice. I did find this solution to work for me and it is much similar to the Guice builder. This did seem a little more straight forward than having to use the ServiceLocatorUtilitiesclass.

public class IOCMockRestModule extends AbstractBinder
    bind(20000).to(Integer.class).named("MAX_REQUEST_TIMEOUT");
}

And to use the injected value:

@Inject
protected CustomerResource(ICustomerProvider customerProvider, @Named("MAX_REQUEST_TIMEOUT") int maxTimeoutMillis) {


来源:https://stackoverflow.com/questions/26365077/how-do-i-inject-constants-with-hk2-in-jersey-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!