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
114
votes
8 answers

Dependency injection with Jersey 2.0

Starting from scratch without any previous Jersey 1.x knowledge, I'm having a hard time understanding how to setup dependency injection in my Jersey 2.0 project. I also understand that HK2 is available in Jersey 2.0, but I cannot seem to find docs…
donnie_armstrong
  • 1,353
  • 2
  • 10
  • 8
19
votes
4 answers

UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl

There are errors when using DI in Jersey Rest application: org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at…
J.Olufsen
  • 13,415
  • 44
  • 120
  • 185
17
votes
1 answer

Why does HK2 repackage everything?

I've recently switched from Jersey 1 to Jersey 2 for some projects I work on. The biggest annoyance I've run across with Jersey 2 is that it uses HK2, which for some reason repackages standard Maven artifacts. To avoid potential annoying-to-debug…
M. Justin
  • 14,487
  • 7
  • 91
  • 130
16
votes
1 answer

What is the relationship between Weld and HK2

This might be a silly question, but I am puzzled by it and I could not find any clear explanation anywhere. HK2 is a dependency injection framework implementing JS330, the foundation of Glassfish V3 and V4. As far as I can see it is used by…
user2465039
  • 894
  • 1
  • 11
  • 28
15
votes
3 answers

HK2 failure has been detected in a code that does not run in an active Jersey Error scope

Currently I am developing a jersey based RESTful application and would like to use DPI in my resources. (Note: Version of jersey is 2.23.1 and the servlet container is tomcat 8.5.3.) Therefore I followed the tutorial Chapter 23. Custom Injection…
Sebastian Frey
  • 488
  • 1
  • 4
  • 13
13
votes
2 answers

Trouble creating a simple singleton class in Jersey 2 using built-in Jersey dependency injection

I am having trouble getting a very basic implementation of a singleton class off the ground with Jersey 2 (2.7) and only Jersey's built-in HK2 dependency injection. I am running this on Tomcat. My goal is to create a singleton instance of a support…
ricb
  • 1,197
  • 2
  • 12
  • 23
11
votes
2 answers

Jersey 2.6 REST Service upgraded to Java 8 tomcat 8 exception:java.lang.NoSuchMethodError: javax.validation.Configuration.getBootstrapConfiguration

We have a war that expose rest service over jersey 2.6 with validation based on hibernate-4.2.7 validation (implements validation-api-1.0.0.GA). Everything worked fine before we upgraded to java 8 and tc-server 3.1.X. After upgrading to java 8 it…
Haim Raman
  • 11,508
  • 6
  • 44
  • 70
11
votes
1 answer

HK2 Factory invoked prior to Jersey filter when @Context is used for setter/field/constructor injection

I've been able to inject into my jersey resource from a filter as per How to inject an object into jersey request context?. This allows me to successfully inject into a method parameter: @GET public Response getTest(@Context MyObject myObject) { //…
rndgstn
  • 321
  • 2
  • 7
11
votes
1 answer

Jersey 2.x Custom Injection Annotation With Attributes

I am in the process of migrating from DropWizard 0.7.1 to 0.8.1. This includes migrating from Jersey 1.x to 2.x. In my implementation that uses Jersey 1.18.1, I had a MyProvider (changed all class names for simplicity's sake) that implements…
Eric Bernier
  • 459
  • 1
  • 7
  • 17
10
votes
2 answers

How do I get a reference to the Jackson Object Mapper in a jersey2 / hk2 application

I have a jersey2 application configured for JSON support via Jackson, adding org.glassfish.jersey.media jersey-media-json-jackson
agnul
  • 12,608
  • 14
  • 63
  • 85
9
votes
0 answers

HK2: Bind to implementation of interface using Immediate scope

I am building an application which will run on Jetty Server. My main class is extended from ResourceConfig to start the "javax.ws.rs.Application" init parameter is set to this class. I have also used ServiceLocator to return the service instances on…
Bhavik Patel
  • 119
  • 4
9
votes
3 answers

Swagger HK2 service reification failed

This application use Swagger (swagger-jersey2-jaxrs, 1.5.18), Spring-boot (spring-boot-starter-jersey, 2.0.1.RELEASE) and Java 9. When application ran Swagger UI loads without an issue. But I can see following warning in the logs. Appreciate any…
era
  • 391
  • 4
  • 24
9
votes
2 answers

jersey + grizzly + hk2: Dependency injection, but not into resource

Following up on Jersey + HK2 + Grizzly: Proper way to inject EntityManager?, I would like to understand how it is possible use dependency injection in classes which are not jersey resources. As an example, I might have background tasks running in…
Hank
  • 4,597
  • 5
  • 42
  • 84
9
votes
1 answer

How do I inject constants with hk2 in jersey 2.0?

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…
agnul
  • 12,608
  • 14
  • 63
  • 85
9
votes
1 answer

How to get HK2 ServiceLocator in Jersey 2.12?

I would like to create a singleton instance of a class that is not involved in Jersey as a Resource or Service and yet would like its dependencies injected from the Jersey ServiceLocator. I can register this class manually in my ResourceConfig…
jsr___
  • 141
  • 1
  • 10
1
2 3
18 19