Hi,
If you are using 1.1.5-ea-SNAPSHOT you can do:
@Inject MySingleton mySingleton;
Assuming that you have:
@Singleton // Jersey scope annotation
public class MySingleton { ... }
or a singleton defined using Guice or Spring, in which case you could
use the injection mechanisms of those technologies.
If you are using an earlier version you can use ResourceContext to
obtain the singleton instance.
Paul.
On Dec 8, 2009, at 1:43 PM, Robert Koberg wrote:
> 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
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>