What's new? | Help | Directory | Sign in
Google
google-guice
Guice (pronounced 'juice') is a lightweight dependency injection framework for Java 5 and above, brought to you by Google.
  
  
  
  
    
Search
for
Updated Sep 02, 2007 by bslesinsky
Labels: Featured
GuiceBestPractices  
Guice best practices

Field vs. method vs. constructor injection

Field injection

Method injection

Constructor injection

Choosing a scope

Eager singletons

A common way to create an eager singleton is

binder.bind(Service.class).toInstance(new MyService());

But this is not such a great way to do it!

Why not let Guice do what Guice does best (creating things)?

binder.bind(Service.class).to(MyService.class).asEagerSingleton();

The main disadvantage of this is that you are forced to name your class, while you might have provided an anonymous instance above. Oh well.


Sign in to add a comment