users@jersey.java.net

[Jersey] How to inject arbitrary objects by _at_Context

From: Sven Strittmatter <weltraumschaf_at_googlemail.com>
Date: Thu, 01 Mar 2012 18:19:31 +0100

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

-- 
"Ich habe vom Glauben zum Wissen konvertiert." (Abdel-Samad)