First, define the singleton class.
public class MyDBManager{ public void store(final String key, final String value) { // Store the key-value pair in a DB. } }As you can see, this class is just a POJO. No special singleton marking anywhere in the class definition.
Next, we will use ContextResolver and @Provider annotatoin to turn MyDBManager into a JAX-RS provider.
import javax.ws.rs.ext.ContextResolver; import javax.ws.rs.ext.Provider; @Provider public class DBResolver implements ContextResolver<MyDBManager> { private MyDBManager db; // A provider must have at least a zero-arg constructor. public DBResolver() { db = new MyDBManager(); } public MyDBManager getContext(Class type) { return db; } }
Now we can inject MyDBManager into a resource class through @Context like this:
import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.ext.ContextResolver; @Path("/myresrc") public class MyResource { @POST @Path("/{id}") @Consumes("text/plain") @Produces("text/plain") public String post(final @Context ContextResolvermyDbManager, final @PathParam("id") String id) { myDbManager.getContext(MyResource.class).store(id, "someValue"); return "OK"; } }
Two observations from this method of injecting a singleton:
- The singleton instance of DBManager is not instantiated until MyResource is invoked the first time.
- The type T in ContextResolver<T> can be an interface so an implementation of the ContextResolver<T> can return an implementation of the T. But, the implementation of T can only be determined by a Class object.
It’s interesting content and Great work. Definitely, it will be helpful for others. I would like to follow your blog. Keep post
ReplyDeleteCheck out:
best hadoop training in omr
hadoop training in sholinganallur
best institute for big data in chennai
big data hadoop course in chennai with placement
very nice post
ReplyDeletebest angularjs training in chennai
angular js training in sholinganallur
angularjs training in chennai
azure training in chennai
best java training in chennai
selenium training in chennai
This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. two shot injection moulding
ReplyDeleteHello, wonderful travelers! I'm Alexa, your virtual companion, and I'm thrilled to welcome you to KLM Crown Lounge 25 Schengen. These premier airport lounges are KLM's exclusive offering, designed to provide eligible passengers with a heightened and comfortable travel experience. Embrace the serene setting to unwind, focus on work, or recharge yourself before embarking on your flight.
ReplyDeleteI appreciate that you maintain such a top-notch blog. BookMyEssay offers research topics examples Assignment Help. Some examples include AI and machine learning, climate change impacts, blockchain technology, mental health in the digital age, sustainable energy solutions, cybersecurity challenges, globalization effects on culture, social media influence on society, genetic engineering ethics, and the future of space exploration.
ReplyDeleteStudents can get academic support from hire someone to take my online exam in addition to assistance with exam and assignment preparation. In our online course, we address every student's homework problem. Our group puts a lot of effort into handling the pressure of homework.
ReplyDeleteGrate blog. It's very interesting to read...
ReplyDeleteR Programming course at Edukators in Coimbatore
Grate blog. It's very interesting to read...
ReplyDeleteDjango course at Edukators in Coimbatore
Awesome blog. It's very interesting to read...
ReplyDeleteDot Net Training course at Edukators in Coimbatore
Nice blog. It's very interesting to read...
ReplyDeleteReact-JS-Training course at Edukators in Coimbatore