我有這樣的吉斯代碼爲什麼Guice會爲我的單身人士創建一個新實例?
public class MainModule extends AbstractModule {
@Override
protected void configure() {
bind(GlobalSettings.class).toProvider(GlobalSettingsProvider.class).in(Singleton.class);
}
和
public class GlobalSettingsProvider implements Provider<GlobalSettings> {
@Override
public GlobalSettings get() {
GlobalSettings globalSettings = new GlobalSettings();
globalSettings.isDummyRun = Boolean.parseBoolean(System.getProperty("isDummyRun", "false"));
globalSettings.inputFlavor = System.getProperty("input_flavor", "none");
}
}
和
public class A(){
public A() {
this.injector = Guice.createInjector(new MainModule());
injector.getInstance(IHttpClientReRunWrapper.class);
globalSettings = injector.getInstance(GlobalSettings.class);
resultsComparerRunner1 = injector.getInstance(ResultsComparerRunner.class);
resultsComparerRunner2 = injector.getInstance(ResultsComparerRunner.class);
resultsComparerRunner3 = injector.getInstance(ResultsComparerRunner.class);
}
}
和
public class ResultsComparerRunner(){
public class ResultsComparerRunner(){
initStaticFromInjector();
}
private void initStaticFromInjector() {
routingResponseShortRepository = injector.getInstance(IRoutingResponseShortRepository.class);
globalSettings = injector.getInstance(GlobalSettings.class); //verify only one injector per run
}
爲什麼我在instanc看到globalSettings
A
和ResultsComparerRunner
的實例是不同的?
我想這是singelton