我有一堂课:
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");
}
}
在我的模块中,我指示 Guice 将所有String.class
实例@Named
“Red”绑定到字符串实例“red-service”,但是我在输出的 print 语句中没有看到。我如何错误地使用 Guice?