我在尝试 @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());
}
}
为什么这个简单的示例总是打印 null?