2009/10/26 eric <[email protected]> > > Hi, I met a problem when trying @Singleton of Guice: > > import com.google.inject.Singleton; > > @Singleton > public class ConfigManager { > private String data; > > public void setData(String data) { > this.data = data; > } > > public String getData(){ > return this.data; > } > > public static void main(String[] args){ > ConfigManager config1 = Guice.createInjector().getInstance > (ConfigManager.class); > ConfigManager config2 = Guice.createInjector().getInstance > (ConfigManager.class); > config1.setData("data"); > System.out.println(config2.getData()); > } > > } > > Why this simple sample always print null? >
as the Guice javadoc states, @Singletons are per-injector: http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/Singleton.html (strictly speaking they are per-injector, per-binding-key) so config1 and config2 are two different instances from two different injectors > -- Cheers, Stuart --~--~---------~--~----~------------~-------~--~----~ 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] For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~----------~----~----~----~------~----~------~--~---
