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
3
votes
1 answer

How do I utilize supportsNullCreation() in Jersey?

I have an injectable provider that may or may return null. I am getting an exception when it is null. I registered the provider as a Singleton, can I possibly register it as a type of SingletonContext that I customize to return true for…
Coder1224
  • 1,785
  • 2
  • 17
  • 21
3
votes
1 answer

Guice nullpointer exception on injected instance

I'm using Guice in Jersey2 for DI (i want to use it so i can use Google App Engine -> not working with HK2). My ApplicationResource: public class ApplicationResource extends ResourceConfig { private static final Logger LOGGER = null; …
Fabian Lurz
  • 2,029
  • 6
  • 26
  • 52
3
votes
1 answer

No object available for injection error when using bindFactory

I have the following implementation for a Jersey (2.18) application: public class RootApplication extends ResourceConfig { public RootApplication() { packages("com.foo.bar"); register(new AbstractBinder() { …
Jim O'Neil
  • 23,344
  • 7
  • 42
  • 67
3
votes
1 answer

What's the right way to bootstrap Jersey 2 (with Jetty)?

I've managed to hack my way to a working Jersey/Jetty setup with HK2 injection, but given the vast number of somewhat confusing (and at times inconsistent) docs I'm finding, I'm not sure if I've missed some salient detail of getting it right.. As it…
XeroxDucati
  • 5,130
  • 2
  • 37
  • 66
3
votes
1 answer

Using Guice with embedded Tomcat?

I have a jersey 2 project with Guice used for DI (via hk2 bridge). Spring JDBC is used for DB calls and is configured via Guice. I'm running it locally with embedded tomcat. This setup works fine for the application i.e. i'm able to access database…
dusk7
  • 129
  • 1
  • 1
  • 9
3
votes
1 answer

@inject doesn't work when the bean is not created via hk2

I have been trying to inject a service to a contract: @org.jvnet.hk2.annotations.Contract public interface CategoryDAO{ } @org.jvnet.hk2.annotations.Service public class HibernateCategoryDAO implements…
Dejell
  • 13,947
  • 40
  • 146
  • 229
3
votes
4 answers

Unit testing JSR-330 injected objects

I have previously used Spring DI, and one of the benefits I perceive is that I can test my Spring bean classes without involving Spring (imports omitted for brevity): public class Foo { private String field; public void setField(String…
slim
  • 40,215
  • 13
  • 94
  • 127
2
votes
1 answer

HK2 Generic Subclass to NonGeneric Interface Binding

I've 2 interfaces and 2 implementations like below. KafkaConsumerExecutor (Interface) DefaultExecutor (Implementation of 1) ConsumerRecordsProcessor (Interface) LastOnlyRecordsProcessor (Impl of 3.) -- public interface KafkaConsumerExecutor { …
Bleach
  • 309
  • 3
  • 18
2
votes
2 answers

Jersey. EOF on upoad operation : java.lang.IllegalStateException: Unable to perform operation: resolve on xxx.UploadFileData

In general, not so long ago we encountered a problem, for some reason, all attempts to upload a file for a client fail. On the back we get an exception: Jersey 2.33 Dropwizard 2.0.28 MultiException stack 1 of…
Valor
  • 23
  • 4
2
votes
0 answers

OpenWebBeans CDI scanning Jersey and HK2 dependencies and throwing error

I am trying to upgrade my JSF 1.1 project to JSF 2.3.9 and have implemented CDI on tomcat 9 following instructions from Balus C Install CDI in Tomcat So I have valid dependencies, context.xml, beans.xml and listener added in web.xml as per…
Vicky
  • 16,679
  • 54
  • 139
  • 232
2
votes
1 answer

Jersey 3 with CDI Weld 4 instead of HK2

I have a small JAX-RS (Jersey 3.0.1) API running in a standalone Jetty 11 but want to use Weld 4.0.0 instead of HK2. The Jetty bootstraps the Weld context properly (Jetty module ci-decorate was installed as described in the Weld…
kladderradatsch
  • 596
  • 5
  • 18
2
votes
0 answers

IncompatibleClassChangeError when injecting proxied EntityManager into resource

I'm trying to inject an EntityManager into a jersey resource and into dependent repository services. The idea is to start and commit a transaction at the boundaries of the REST request, so the same entity manager must be used. To achieve this, I've…
Hank
  • 4,597
  • 5
  • 42
  • 84
2
votes
1 answer

How can I inject CDI event with HK2 in JUnit Test?

I would like to test my Jakarta application (Payara) with simply JUnit Test (without arquillian, without MicroShed). I use ServiceLocator from Hk2 to inject service and its work well for my JPA entity and Service. private static ServiceLocator…
bmeynier
  • 289
  • 2
  • 12
2
votes
1 answer

Jersey HK2 injection with a Generic class

I have a generic class that looks like this. public class DAO { private final Class clazz; public DAO(Class clazz) { this.clazz = clazz; } } I use it within a RestFull api powered by Jersey. I use it very often and I…
Alex Kubity
  • 364
  • 4
  • 16
2
votes
1 answer

HK2 service reification failed Warning in logs for javax.ws.rs.core.UriInfo with exception: Could not find a suitable constructor in UriInfo class

While calling certain Rest services, I am getting HK2 reification failed error. Recently I migrated my application to Open jdk 12 from JDK 8. I replaced AbstractBinder package from org.glassfish.hk2.utilities.binding.AbstractBinder to…
Aditya Batra
  • 263
  • 1
  • 2
  • 18