A slightly modified singleton injection.

45 views
Skip to first unread message

Nick Tsitlakidis

unread,
Dec 29, 2013, 5:54:35 PM12/29/13
to robo...@googlegroups.com
Hey guys,

I have a weird use case in front of me and I thought I should ask for assistance.

I'm using an external library (the Otto event bus) and I want a singleton instance of the Bus class to be injected each time in my objects. I've managed to do this, it was easy. But! The problem is I need to change something
in the instance and the only way to do it is through it's constructor call.
Think of it as something like this.

Bus b = new Bus(ThreadEnforcer.ANY)


Having Roboguice injecting it as a singleton prevents me from running the constructor myself. So I though of using a provider for this but I don't know how to make the singleton thing work if I use a provider.

Do I make any sense? :)

Russell Zornes

unread,
Dec 29, 2013, 6:37:52 PM12/29/13
to robo...@googlegroups.com
Something like this...

bind(Bus.class).toProvider(BusProvider.class).in(Scopes.SINGLETON);

public class BusProvider implements Provider<Bus> {
    @Override
    public Bus get() {
        return new Bus(ThreadEnforcer.ANY);
    }
}

-Russ

Nick Tsitlakidis

unread,
Dec 29, 2013, 6:41:38 PM12/29/13
to robo...@googlegroups.com
wow, fast reply. Thanks a lot Russell. Going to try it right now. :)
Reply all
Reply to author
Forward
0 new messages