Re: Using Guice for a standalone application

632 views
Skip to first unread message

Alex opn

unread,
Jan 17, 2013, 10:08:01 AM1/17/13
to google...@googlegroups.com
[1] I can't say if it's the best solution but I would do it like that and can't think of another way actually.

[2] Have a look at that (bit of discussion included about pros/cons): http://stackoverflow.com/questions/6197178/is-guices-implementedby-evil-is-it-appropriate-in-some-cases


On Thu, Jan 17, 2013 at 12:53 PM, Bram <bra...@gmail.com> wrote:
For a standalone application (running as a daemon in the background) I trying to find out if Guice would be a correct solution to handle Dependency Injection. Most DI solutions are used in webapplications and I cant find the proper way to use Guice in a standalone application. I basically have two questions:

1. How would one bootstrap the application? I could see something like this in the main() method, but I wonder if this is the best solution?

public static void main(String[] arges) {
    Injector injector = Guice.createInjector(new TestModule());
    WorkerService service = injector.getInstance(WorkerService.class);
    service.start();
}

2. How do I properly configure all my bindings? By skimming through some documentation and testing out a few things, I have this to bind my testmodule, but I doubt this is the correct solution when you have allot of bindings. Can this be done (semi) automatically? Or do I have to create/configure everything I want to inject somewhere? (that will result in a long list of bind(...).to(...) in allot of modules).

public class TestModule extends AbstractModule{

    @Override
    protected void configure() {
        bind(TestService.class).to(TestServiceImpl.class);
        bind(WorkerService.class).to(WorkerServiceImpl.class);
    }
}

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/RcoHYvLsOG8J.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.

Aekold

unread,
Jan 17, 2013, 10:34:33 AM1/17/13
to google...@googlegroups.com
1. Yes, it's the best solution I know, you basically do all the job with 2 lines of code.

2. It depends on what you really want to achieve. For example:
a) if you just want to save some time writing all those bindings manually - you can either create array of classes and some method to iterate and bind each one of them, or you could use something similar to Spring classpath scanner, it will take you some time to write it but afterwards it will do the job.
b) if you need modular system you could use java.util.ServiceLoader to load proper implementation for every interface from external jars and then bind them. In this case once again you can just create array like this:
Class[] interfacesList = {FirstService.class, SecondService.class};
then iterate it and load implementation for each one of them and bind it, so adding one more binding for you will be just adding one more interface to array.
Reply all
Reply to author
Forward
0 new messages