users@jersey.java.net

basic injection question

From: Robert Koberg <rob_at_koberg.com>
Date: Tue, 8 Dec 2009 04:43:48 -0800

Hi,

(apologies for what is probably pretty basic)

I have a shared singleton class that is currently instantiated in a ServletContextListener and set as an attribute in the ServletContext.

In my resource classes, I inject the ServletContext as a field and get the singleton from that ServletContext.

Can I inject the singleton directly to skip adding/retrieving it to/from the ServletContext? E.g., something like:

public class MyResource {

 @Context MySingleton mySingleton;

}

Should I be using the @Context annotation? Something else?

I have tried various ways. First I simply added the @Provider annotation and added the package to be scanned. The logs show that the provider (MySingleton) is found. I have tried implementing ContextResolver, Injectable, SingletonTypeInjectableProvider.

Is there a simple example of how to do what I am trying to do? Is there a better way to approach it?

thanks,
-Rob