Wednesday, December 16, 2015

Injecting Request Scoped Objects into Singleton Scoped Object with HK2 and Jersey

This content has been moved to my new GitHub Pages site.

4 comments :

  1. I have tried this technique and I get errors stating the "the descriptor is in an unproxiable scope." This technique only works if the "RequestScoped" annotation is itself annotated with the @Proxiable annotation as described on the HK2 website, yes?

    https://hk2.java.net/hk2-api/extensibility.html

    If so, what RequestScoped annotation are you using that works with Jersey? The org.glassfish.jersey.process.internal.RequestScoped annotation, which is what I'm using, does not have this annotation, and you cannot extend annotations in Java.

    ReplyDelete
    Replies
    1. Technetium, apologies for the late reply. I haven't been maintaining this blog like I should be.

      As far as your comment, I have never run into this problem. Please have a look at the following gist

      https://gist.github.com/psamsotha/fd77d9bf9f5362453ac0f8bad9fdd751

      It's a complete runnable test that shows that it is doable. If you still have problems, maybe you can provide your own gist that I can check out. Or ask a question on Stack Overflow. I am a lot more active there than I am on this blog.

      Delete
    2. Thanks for responding. After more experimenting I was making a flawed assumption. If you have a service that depends on a RequestScoped dependency, you do not need to specify that the service is RequestScoped. HK2 just figures it out. However, once you make that service a proxy that inference goes out the window, and you need to explicitly state that the service with a RequestScoped dependency is also RequestScoped.

      So what you have in your blog here is correct because you always list the scope explicitly on your dependencies. You do not expect HK2 to infer the scope based upon the scope of the dependencies injected into the proxied service.

      Cheers.

      Delete