How to set an object to context so that i can get it anywhere in the application with @Context


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:

public class MyObject { String get() { return "I am an object"; } }

This object needs to be available in my jersey classes (resource, filter etc). I wrote a little resource using this bean:

@Path("context") public class ContextResource { @Inject MyObject o; @GET public String get() { return o.get(); } }

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:

public class Application extends io.dropwizard.Application<Configuration>{ @Override public void run(Configuration configuration, Environment environment) throws Exception { environment.jersey().register(ContextResource.class); environment.jersey().register(new AbstractBinder() { @Override protected void configure() { bind(MyObject.class).to(MyObject.class); } }); } public static void main(String[] args) throws Exception { new Application().run("server", "/home/artur/dev/repo/sandbox/src/main/resources/config/test.yaml"); } }

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:

artur@pandaadb:~/dev/vpn$ curl localhost:9085/api/context I am an object

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.




  • Resend request angular 2
  • AngularFire / Firebase - checking the authentication state
  • React Native: Native Android UI component with JWPlayer shows no video image
  • StartForeground Bad Notification Error
  • C# String Filepath Question
  • HttpClient with WebView
  • AOT and JIT with build-optimizer
  • Angular5 Service Worker update(SWUpdate) not detecting on firefox. Working on chrome
  • three.js WebVR example code works on threejs.org but not on my local server
  • Disabling sound of embedded flash object with html
  • Why isn't my “Fizz Buzz” test in R working?
  • Knockout custom binding handler
  • abstracting over a collection
  • Can't remove headers after they are sent
  • Why use database factory in asp.net mvc?
  • Can someone please explain to me in the most layman terms how to use EventArgs?
  • C++ pointer value changes with static_cast
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • jQuery show() function is not executed in Safari if submit handler returns true
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • python regex in pyparsing
  • JTable with a ScrollPane misbehaving
  • How do I configure my settings file to work with unit tests?
  • How to get Windows thread pool to call class member function?
  • apache spark aggregate function using min value
  • Is it possible to post an object from jquery to bottle.py?
  • unknown Exception android
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?