How to add constants with hk2 in jersey 2.0?

How can I insert a constant into a class using HK2 in a jersey? With Guice, I might have a 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 is the equivalent, if any, in HK2?

+5
source share
1 answer

I am participating in the hk2 training process coming from Guice. Honestly, I'm still weedy with hk2 complexity and simplicity. I found this solution to work for me, and it is very similar to Guice builder. This looked a little more straightforward than using the ServiceLocatorUtilities class.

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

And use the entered value:

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

Source: https://habr.com/ru/post/1204658/


All Articles