hk2

Using a custom hk2 InjectionResolver to inject application configuration

假装没事ソ 提交于 2019-12-01 03:56:56
问题 Kind of a follow up to my previous question. I'm trying to inject application configuration data using JSR-330 standard annotations and the HK2 framework bundled with jersey. Ideally I'd like to create a custom InjectionResolver for the Named annotation, which will lookup the desired values in a Map or Properties object that I will populate from data read elsewhere. In my first attempt I've created an Application instance like public class MyApplication extends ResourceConfig { ... packages

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

时间秒杀一切 提交于 2019-12-01 03:13:33
问题 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 an ExecutorService, and they might need an EntityManager. If I attempt to @Inject the EntityManager into the class, nothing happens. Injecting it into a @Path -annotated jersey resource class, injecting works fine. The application is running as a

How do I make a HK2 ServiceLocator use the Singleton service instances from the ServiceLocator that it's bridged from?

泪湿孤枕 提交于 2019-12-01 00:26:32
We are using using ExtrasUtilities.bridgeServiceLocator() to inject existing Singleton application services created in one ServiceLocator into Jersey RESTful Web Services by bridging the app ServiceLocator into the Jersey ServiceLocator. However the Singletons that exist in the 'outer' locator aren't being used - each of the services is being created once again when injected into the Jersey services. It seems the Singleton is only visible within the scope of a ServiceLocator, even if it's bridged. Is this the intended behaviour? And if so is there any way to change this behaviour and have a

How do I make a HK2 ServiceLocator use the Singleton service instances from the ServiceLocator that it's bridged from?

泄露秘密 提交于 2019-11-30 20:16:50
问题 We are using using ExtrasUtilities.bridgeServiceLocator() to inject existing Singleton application services created in one ServiceLocator into Jersey RESTful Web Services by bridging the app ServiceLocator into the Jersey ServiceLocator. However the Singletons that exist in the 'outer' locator aren't being used - each of the services is being created once again when injected into the Jersey services. It seems the Singleton is only visible within the scope of a ServiceLocator, even if it's

Jersey and HK2 ServiceLocator

徘徊边缘 提交于 2019-11-30 19:47:59
问题 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 ServiceLocator locator)... When I try to use the locator at any point, I still can't create instances of things that I have registered in an AbstractBinder using the locator.create(MyThing.class) method. I'm certain that they are bound correctly because they are injected

How to get HK2 ServiceLocator in Jersey 2.12?

对着背影说爱祢 提交于 2019-11-30 19:16:10
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 constructor, the ResourceConfig is then passed in to the Grizzly factory method like so: ResourceConfig resourceConfig = new DeviceServiceApplication(); LOGGER.info("Starting grizzly2..."); return GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig, mServiceLocator); The problem that remains is how to get a reference to the Jersey

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

泪湿孤枕 提交于 2019-11-30 18:55:42
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) { // this works However, for setter/field/constructor injection, the HK2 Factory is invoked before the jersey filter, which means the provide() method returns null: @Override public MyObject provide() { // returns null because the filter has not yet run, // and the property has not yet been set return (MyObject)context.getProperty("myObject"); } Is

Jersey + HK2 + Grizzly: Proper way to inject EntityManager?

混江龙づ霸主 提交于 2019-11-30 07:23:35
I've managed to set up injection (into resource classes) of my own service classes in Jersey, HK2 and a plain GrizzlyServer. (Basically followed this example .) I'm now curious what the best is to inject JPA EntityManagers into my resource classes? (I'm currently considering one request as one unit of work). One option that I'm currently exploring is to use a Factory<EntityManager> in the following way: class MyEntityManagerFactory implements Factory<EntityManager> { EntityManagerFactory emf; public MyEntityManagerFactory() { emf = Persistence.createEntityManagerFactory("manager1"); }

Jersey custom method parameter injection with inbuild injection

旧城冷巷雨未停 提交于 2019-11-30 05:27:26
问题 Hello I am building an application using dropwizard, that is using jersey 2.16 internally as REST API framework. For the whole application on all resource methods I need some information so to parse that information I defined a custom filter like below @java.lang.annotation.Target(ElementType.PARAMETER) @java.lang.annotation.Retention(RetentionPolicy.RUNTIME) public @interface TenantParam { } The tenant factory is defined below public class TenantFactory implements Factory<Tenant> { private

How to get HK2 ServiceLocator in Jersey 2.12?

ぃ、小莉子 提交于 2019-11-30 04:21:41
问题 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 constructor, the ResourceConfig is then passed in to the Grizzly factory method like so: ResourceConfig resourceConfig = new DeviceServiceApplication(); LOGGER.info("Starting grizzly2..."); return GrizzlyHttpServerFactory.createHttpServer(BASE_URI,