我有一個類:吉斯注射
public class FizzBuzz {
@Named("Red") private String redService;
public static void main(String[] args) {
GuiceTest testApp = new GuiceTest();
testApp.run();
}
private void run() {
Injector inj = Guice.createInjector(new MyModule());
redService = (String)inj.getInstance(String.class);
// Should print "red-service" but is instead an empty string!
System.out.println("redService = " + redService);
}
// ... Rest of class omitted for brevity
}
public class MyModule extends AbstractModule {
@Override
protected void configure() {
bind(String.class).annotatedWith(Names.named("Red")).toInstance("red-service");
}
}
在我的模塊我指示吉斯綁定所有String.class
實例@Named
「紅」字符串實例「紅色服務」,但我在輸出的打印語句中沒有看到。我如何錯誤地使用Guice?