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

There is more than one active context for org.glassfish.jersey.process.internal.RequestScoped

I'm having difficulties retrieving a RequestScoped object inside a ContainerRequestFilter, which I attempt to do by injecting a Provider into my filter. My application configuration is as follows: class MyResourceConfig extends ResourceConfig { …
lhaat
  • 31
  • 3
2
votes
1 answer

HK2 Jersey. How to get bean from container?

I have a chain of Features. I would like to bind some beans inside ConfigFeature and then get them inside the MongoDBFeature. What methods should I use for that? public final class IoCBinder extends AbstractBinder { @Override protected void…
Aleksey Kozel
  • 319
  • 1
  • 9
  • 16
2
votes
2 answers

hk2: why bind(X.class).to(X.class)

I am learning Java, but found the following piece of code. I am confused. What is bind(X.class).to(X.class); for? import org.glassfish.hk2.utilities.binding.AbstractBinder; public class ApplicationBinder extends AbstractBinder { @Override …
BAE
  • 8,550
  • 22
  • 88
  • 171
2
votes
1 answer

How to use Mockito with HK2?

I'm using HK2 for dependency injection and want to replace a Singleton Object with a Mockito-mock in the context of a JUnit-Test. The simplest setting would be as follows: import javax.inject.Inject; import…
Tobias Walter
  • 436
  • 2
  • 7
2
votes
1 answer

Jersy2 inject slf4j Logger

I'm trying to understand Jersey 2 development and context-dependency injection. I don't understand how to inject into a resource an object that needs initialization parameters in the constructor. For example: I'd like to @Inject slf4j Logger, built…
besil
  • 1,308
  • 1
  • 18
  • 29
2
votes
1 answer

@Inject not working in MethodInterceptor managed by HK2

I am currently refactoring a Jersey Web App and want to bundle some cross cutting concerns in their own classes and use annotations to intercept methods. For example there are a lot of methods where I need to check if the user is the owner of the…
Inkvine
  • 129
  • 7
2
votes
1 answer

How do I define a "default" implementation in HK2?

I am using HK2 to resolve dependencies of services in my Jersey / Jetty web service. I have a situation where for one particular interface, I want to use a specific implementation as a "default" implementation. By "default" I mean no names or…
Technetium
  • 5,902
  • 2
  • 43
  • 54
2
votes
1 answer

HK2 / GlassFish / Jersey incompatibility

I'm trying to debug an ugly issue I really don't understand - not sure if it is a bug or not. I have an application which works fine, using this pattern: ServiceLocator locator =…
user2163960
  • 1,871
  • 19
  • 22
2
votes
1 answer

Inject not working for nested objects[Jersey 2.22.1]

I have a Jersey resource with a facade object injected. This is configured in my ResourceConfig and the facade gets injected fine. The facade contains a DAO class which also should be injected and is configured in the same ResourceConfig. Now to my…
2
votes
1 answer

Class def not found: org/glassfish/hk2/ComponentException

I need to write junit tests for Jersey application. Now I want to test dependency injection and the problem is I created class like this: @RunWith(Hk2Runner.class) public class BinderTest { @Inject private SomeClass someClass; @Test …
azalut
  • 4,094
  • 7
  • 33
  • 46
2
votes
1 answer

HK2 Custom annotation Annotation disallowed on Constructor

Using HK2 injection framework i developed a custom annotation for injecting my custom object inside my classes. All works fine if i annotate my objects as class variables: public class MyClass { @MyCustomAnnotation MyType obj1 …
Alex
  • 1,515
  • 2
  • 22
  • 44
2
votes
1 answer

HK2 Factory with Interface Type and InjectionResolver

in my use case i need to develop a custom annotation by wich i can instanziate the implementation of a DAO. So i have the interface: public interface IDAO{ public void method1(); public void method2(); } and the resource config…
Alex
  • 1,515
  • 2
  • 22
  • 44
2
votes
1 answer

What exactly does "reify" mean in the context of dependency injection

I've read a few books on different DI frameworks, and I see the term "reify" or "reified" (past tense) thrown around, but I can't get a firm grasp on what that actually means code-wise. The wikipedia definition states make (something abstract) more…
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
2
votes
1 answer

@inject doesn't seem to work NPE when injecting an object

I've been struggling with this issue for a while now, so I came here to share it with you. First I have a class in which I want to inject an Object: public class MyClass { @javax.inject.Inject private MyInterface interface /.../ public void…
2
votes
1 answer

Google Appengine with Jersey 2.1x works fine in dev server but not in Appengine servers

I have been using Jersey v2.19 with Google Appengine v1.9.22 and it works fine in local devserver without any issue, but when i tried to deploy the application to appengine. I get multiple exceptions Frameworks Used : AppEngine Java 1.9.22 Jersey…
Ramesh Lingappa
  • 2,448
  • 20
  • 33