I want to set MyObject
class instance to the application context so that I can use it anywhere with the following:
@Context MyObject object
I used Jedis
which gives me access for the jedis
through the above approach.
Please help in setting the context.
I am using dropwizard (jetty,jersery and jackson)
.
I had some time and wrote up the way to do it (jersey only, no other DI framework used).
Jersey is compliant with javax.inject annotations. The reason you do NOT use a context annotation is because (by the sound of it) your MyObject class is not a context object (e.g. it doesn't change with each request like e.g. HttpServletRequest which is injectable). So we need to bind your object.
Consider my implementation of MyObject:
This object needs to be available in my jersey classes (resource, filter etc). I wrote a little resource using this bean:
Note that I am using the javax.inject.Inject annotation for this case to tell jersey I want this particular bean injected. All I need to do now is to tell jersey about this bean. In my DW application I do:
Note that I am using a binder to bind my bean. The syntax looks funky, but essentially it is doing a "bind the type to the implementation". Since my type is my implementation (I am not using an interface for MyObject), this looks like:
Now jersey knows about my bean and will happily inject it.
Running all my code prints:
Hope that brings some insights on how to use injection without a framework. Personally I would recommend using guice with dropwizard (google: dropwizard-guicey) which makes these kind of things very easy.
Regards,
Artur