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.
On Thursday, January 17, 2013 1:53:10 PM UTC+2, Bram 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/-/4al7KpcpJuEJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-guice?hl=en.