Thanks for your answers. I have another question that is not really
answered in the docs (atleast, I can't find it). What is the best way to
set-up your modules with its bindings?
In my test I had one module with two bindings, but putting all of my class
bindings in one module does not seem right to me. Is their any best
practice in separating modules?
I could see something like this:
class FirstModule extends AbstractModule {} // bindings for package X
class SecondModule extends AbstractModule {} // bindings for package Y
Injector injector = Guice.createInjector(new FirstModule(), new
SecondModule(), ....);
Op donderdag 17 januari 2013 16:34:33 UTC+1 schreef Aekold het volgende:
>
> 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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
Visit this group at http://groups.google.com/group/google-guice?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.