Advertisement
Guest User

Untitled

a guest
Aug 21st, 2014
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. public class FizzBuzz {
  2. @Named("Red") private String redService;
  3.  
  4. public static void main(String[] args) {
  5. GuiceTest testApp = new GuiceTest();
  6.  
  7. testApp.run();
  8. }
  9.  
  10. private void run() {
  11. Injector inj = Guice.createInjector(new MyModule());
  12.  
  13. redService = (String)inj.getInstance(String.class);
  14.  
  15. // Should print "red-service" but is instead an empty string!
  16. System.out.println("redService = " + redService);
  17. }
  18.  
  19. // ... Rest of class omitted for brevity
  20. }
  21.  
  22. public class MyModule extends AbstractModule {
  23. @Override
  24. protected void configure() {
  25. bind(String.class).annotatedWith(Names.named("Red")).toInstance("red-service");
  26. }
  27. }
  28.  
  29. public class FizzFuzz {
  30. @Inject
  31. @Named("red")
  32. private String service;
  33.  
  34. public static void main(String[] args) {
  35. FizzFuzz fizzFuzz = Guice.createInjector(new AbstractModule() {
  36. @Override
  37. protected void configure() {
  38. bindConstant().annotatedWith(Names.named("red")).to("red-service");
  39. }
  40. }).getInstance(FizzFuzz.class);
  41.  
  42. System.out.println(fizzFuzz.service);
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement