Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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");
- }
- }
- public class FizzFuzz {
- @Inject
- @Named("red")
- private String service;
- public static void main(String[] args) {
- FizzFuzz fizzFuzz = Guice.createInjector(new AbstractModule() {
- @Override
- protected void configure() {
- bindConstant().annotatedWith(Names.named("red")).to("red-service");
- }
- }).getInstance(FizzFuzz.class);
- System.out.println(fizzFuzz.service);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement