1

I am using Jersey 2.x and HK2 which is built-in into Jersey. I need to decorate certain methods in my services marked by annotation, i.e. I'd like to perform some additional actions before and after such methods calls. Unfortunately, HK2 doesn't have any AOP capabilities. I thought that I could bind my factory to a service's interface and create a proxy of the service whenever factory's provide method is called. However, I need a couple of things to my factory to work:

1) the service's class and interface to create a proxy

2) ServiceLocator instance to properly inject all service's dependencies

And I just don't see any way to have both. If I bind factory using bindFactory(MyFactory.class) then I can't pass information about the service into it, but can have ServiceLocator instance injected into it. And if I bind factory using bindFactory(new MyFactory(Service.class, ServiceImpl.class)) then I have information about the service, but I don't have ServiceLocator instance to properly create that service, because HK2 doesn't inject anything into factories instances and I don't see anyway to get a ServiceLocator instance from AbstractBinder to provide my factory with locator during binding.

I would really appreciate any suggestions and recommendations. I hope I am not the first one who wants to uniformly proxy my services.

Fury
  • 71
  • 6

1 Answers1

0

In the most recent version of hk2 (2.2.0-b25) we have added the ability to add AOP alliance interceptors to any method. But this feature is not yet fully baked (we will be adding constructor injection) and not yet fully documented. But you might want to start playing around with it, as it will give you the ability to add AOP MethodInterceptors to any method on your service.

jwells131313
  • 2,364
  • 1
  • 16
  • 26
  • Hi, thanks a lot for the response. Actually, I've solved my problem by injecting ServiceLocator into ResourceConfig implementation and then passing it into my binder and from the binder into my factory instance. But I'll check your answer as accepted so that should anyone else need this kind of functionality they will be able to implement it using built-in HK2 capabilities. – Fury Dec 16 '13 at 06:15
  • @jwells131313 could you point me to some documentation on that? I'm trying to answer [this](http://stackoverflow.com/q/20556200/1415732) and an AOP `MethodInterceptor` might help, but I don't want to use Guice, would rather stick with the built in hk2. – Alden Dec 17 '13 at 16:20
  • The only documentation that exists right now is this: https://hk2.java.net/2.2.0-b27/apidocs/org/glassfish/hk2/api/InterceptionService.html. The hk2 team is currently working on getting an example and documentation to go with that example. I am not sure about the plans for Jersey to uptake the version of hk2 that has this feature, though I think it may be fairly soon. – jwells131313 Dec 18 '13 at 03:21
  • @Fury could you possibly put the ResourceConfig code you used to configure and inject the ServiceLocator into an answer to this question? – slim Jan 07 '14 at 13:10
  • 1
    The documentation for the AOP feature of HK2 can be found here: https://hk2.java.net/2.2.0/aop-example.html. This version of HK2 will be integrated into Jersey this week or early next week – jwells131313 Jan 13 '14 at 22:54
  • That's good to hear @jwells, and the example looks great. I've been bashing my head against guice for a while now trying to get AOP to work on a jersey resource method with the hk2 bridge. Please let me know when it's integrated with Jersey and I'll upgrade immediately. Just tried registering a method interceptor with jersey 2.5.1, and I'm either doing it incorrectly or it doesn't work yet. – Alden Jan 18 '14 at 07:53
  • 1
    The latest Jersey source has integrated a version of hk2 with the AOP capability so it will be in the next jersey release (I do not know when that will be) – jwells131313 Jan 24 '14 at 13:37