users@jersey.java.net

[Jersey] Re: Singleton class in Jersey

From: Robert DiFalco <robert.difalco_at_gmail.com>
Date: Fri, 30 Jan 2015 13:21:26 -0800

I'm not exactly sure what you are asking but if you have some service that
you want to be able to inject into your Resource instances you can just add
this to the constructor of your ResourceConfig class:

register(new AbstractBinder() {
    @Override
    protected void configure() {
        bind(MyService.getInstance()).to(MyService.class);
    }
});

On Fri, Jan 30, 2015 at 1:11 PM, Dietz, Randall <randall_at_dietz.id.au> wrote:

> Hi all,
>
> Can someone please point me to some up-to-date documentation that has an
> example of how to add a singleton class to a Jersey (v2.15) REST API?
>
> I have successfully built a REST application in Jersey, now all I want to
> do is inject a Singleton class (that controls access to my central
> configuration) in my service classes that is shared across all requests.
> Really, really simple use case... but I've wasted hours trying to get it
> working.
>
> The problem I've encountered is that there are a number of "solutions" on
> the InterWeb, but they're either out of date, confusing or just plain wrong.
>
> Would appreciate some guidance or examples of how it should be done in the
> latest Jersey.
>
> Thanks -- Randall
>
>