Questions tagged [hk2]

HK2 is a light-weight and dynamic dependency injection framework for Java.

  • HK2 is JSR-330 compliant and has useful utilities for marking classes as services and interfaces as contracts. JSR-330 defines services and injection points that can be dynamically discovered at runtime and which allow for Inversion of Control (IoC) and dependency injection (DI).

  • HK2 provides an API for fine control over its operation and has the ability to automatically load services into the application. It is the foundation for the GlassFish V3 and V4 application servers as well as other products.

  • HK2 also has a powerful API that can be used to perform several tasks such as binding service descriptions into the system and looking up services, as well as several extensibility features allowing the users to customize or change the behavior of HK2.

The following list gives an overview of some of the things that can be customized or extended with HK2:

  • Custom scopes
  • The use of proxies
  • Custom injection resolution
  • Assisted injection
  • Just In Time injection resolution
  • Custom validation and security
  • Run Level Services
285 questions
4
votes
0 answers

Jersey 2 - Injecting request scoped service in Filters

I'm writing a RESTful web service where I'm trying to inject a request scoped service class into a filter. I have gone through Paul Samsotha's blog on how to inject request scoped services using proxies. Here's my implementation: The supplier for…
Termin4t0r
  • 199
  • 3
  • 10
4
votes
1 answer

Difference between bindAsContract and bind in HK2

I am working to implement a dependency injection of a constructor in a J2EE jersey project. I am using HK2. I create a class class MyServiceImpl implements MyService{ @Inject public MyServiceImpl(String test){ // do something } } Now, my…
4
votes
2 answers

How do I make HK2 act like Guice for injecting classes that weren't configured explicitly?

I have a project using Jersey 2.25 (with HK2 2.5-b30). Originally, I was using the HK2-Guice Bridge. However, this seems to fail unexpectedly for some cases (in particular, cases where Strings are annotated with a custom annotation configured in…
Robert Fraser
  • 10,649
  • 8
  • 69
  • 93
4
votes
1 answer

Configure EntityManager in a Jersey / hk2 application

I'm trying to inject EntityManagerFactory (as a Singleton) and EntityManager (in a request scope) in my Jersey / Hk2 application. I'm using this question (How to i properly configure an entitymanager in a jersey-hk2 application) as a guide I spent…
4
votes
1 answer

Jersey HK2 Dependency Injection

I'm writing a simple microservices that exposes REST API. So I started working with Jersey and of course I need to Inject my object into jersey resources. Basically I have 2 classes that defines a set of resources and some of them need to use…
Raffaele
  • 461
  • 1
  • 7
  • 20
4
votes
1 answer

How to Disable Dependency Injection in Jersey 2?

I'm new to creating REST web services with Jersey 2. I was able to get a few simple cases going now I'm moving on to "real" work. I create my own instances of ResourceConfig and populate the ResourceConfig instances with instances of the REST…
user3303372
  • 761
  • 1
  • 10
  • 21
4
votes
1 answer

Injecting hibernate session to Jersey using HK2

I'm developing little app and have some issues with DI. I have a repository class for persisting my entities which I inject to my service. And I'd like to inject Session object to it using H2K. For this purpose I try do something similar described…
alexdzot
  • 175
  • 14
4
votes
2 answers

HK2 using @Named to inject implementations not working

I am trying to learn HK2 to use the service locator pattern. The following is some code i have written: package org.swx.nursing.ccquerytool.file; import org.jvnet.hk2.annotations.Contract; @Contract public interface FileReader { public void…
Rookie
  • 5,179
  • 13
  • 41
  • 65
4
votes
1 answer

How can I bind a factory to a annotation-qualified injection point?

I asked and got an answer how to bind a named injection point. And I don't know how to bind a factory to a qualified injection point. class SomeResource { @Inject @Some // is a @Qualifier, of course. private MyType qualified; } I…
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
4
votes
1 answer

HK2 is not injecting the HttpServletRequest with jersey

I am trying to follow the example located here to create a factory in order to inject my HttpSession. Unfortunately no matter what I try it is not working. Not sure what could be the issue. I have tried injecting just the HttpServletRequest and a…
Chris Hinshaw
  • 6,967
  • 2
  • 39
  • 65
4
votes
1 answer

How do I get my Jersey 2 Endpoints to eagerly initialize on startup?

I am porting some code from Jersey 1.x and my implementation of various Health Check endpoints relies on all the @Singleton endpoint resources being initialized at start up so as to be able to detect which checks to do. In Jersey 2.0 - whatever I do…
TiGz
  • 894
  • 2
  • 13
  • 21
4
votes
2 answers

HK2 equivalent Assisted Injection with FactoryModuleBuilder

Thanks to migration to jersey 2 I need to migrate from guice to HK2. I have an Assisted injection approach for some of my dependencies which I couldn't get my head around to implement in HK2. It looks like it's supposed to be solved via Custom…
Natan
  • 2,816
  • 20
  • 37
4
votes
1 answer

Injection into Jersey resource, HK2 doing injection instead of Weld when running in Java SE -> Unsatisfied dependency

I have a simple test-application which I deploy to GlassFish (just one resource having one simple class injected). On GlassFish everything is working fine (except for the exceptions when logging FINE, see here). I can successfully call the resource…
user3836334
  • 173
  • 2
  • 7
4
votes
2 answers

Jersey and HK2 ServiceLocator

I'm trying to initialize some components in my Jersey application in the Application constructor (the thing that inherits from ResourceConfig) . It looks like this public Application(@Context ServletContext context, @Context…
Bill
  • 1,563
  • 1
  • 15
  • 28
4
votes
3 answers

Jersey 2.3 not invoking HK2 service locator shutdown when servlet container is shut down

Question: Is there a proper way to ensure that HK2 shuts down with Jersey 2; if not, would this be considered a bug? Using jersey-server 2.3.1, I've noticed that HK2 @PreDestroy methods on managed singletons and dispose() methods on Factory