users@jersey.java.net

[Jersey] Re: How to inject arbitrary objects by _at_Context

From: Pavel Bucek <pavel.bucek_at_oracle.com>
Date: Tue, 06 Mar 2012 07:42:25 +0100

Hello Sven,

getSingletons is there for returning already instantiated resource and
provider classes, not any arbitrary class you would like to inject (it
is just ignored in this case (no relevant annotation is found)).

What you can do is implement InjectableProvider<Context, Type> or maybe
SingletonTypeInjectableProvider. See javadocs and test for details.

javadoc:
http://jersey.java.net/nonav/apidocs/1.12/jersey/com/sun/jersey/spi/inject/class-use/InjectableProvider.html
http://jersey.java.net/nonav/apidocs/1.12/jersey/com/sun/jersey/spi/inject/InjectableProvider.html

test: (don't mind container part, look for
SingletonTypeInjectableProvider and relevant @Context declarations)
http://java.net/projects/jersey/sources/svn/content/trunk/jersey/jersey-tests/src/test/java/com/sun/jersey/impl/container/grizzly/web/ApplicationTest.java?rev=5667

Regards,
Pavel

On 3/1/12 6:19 PM, Sven Strittmatter wrote:
> Hello list,
>
> I build a Jersey web app which uses Neo4J. Now I'm confrontd with the
> problem how to share a single Neo4J database object instance to all
> resources. After some research I wanted to utilize @Context and
> Application. I've coded a Application class which provides the
> database object:
>
> public class CiterApplication extends Application {
>
> @Override
> public Set<Object> getSingletons() {
> final Set<Object> objectSet = Collections.emptySet();
> objectSet.add(new EmbeddedGraphDatabase("/tmp/citer.db"));
> return objectSet;
> }
>
> }
>
> And add a context anno to my resources:
>
> @Path("/") public class Index {
>
> @Context GraphDatabaseService database;
>
> ...
> }
>
> But this doesn't work. Do I understand it wrong that I can provide any
> arbitrary object instance via Application.getSingletons() to my
> resource classes? How to I share global resources like database etc.
> to my resource classes?
>
> Kind regards
> -Sven
>