/sandbox/guice-sandbox/src/examples/Main6.java

http://t-2.googlecode.com/ · Java · 37 lines · 29 code · 8 blank · 0 comment · 0 complexity · 17fac66191b9d8b90c0f4b21c5983b1d MD5 · raw file

  1. package examples;
  2. import com.google.inject.AbstractModule;
  3. import com.google.inject.Guice;
  4. import com.google.inject.Injector;
  5. import com.google.inject.Module;
  6. public class Main6 {
  7. public static void main(String[] args) {
  8. Module module1 = new AbstractModule() {
  9. @Override
  10. protected void configure() {
  11. bind(Bar.class);
  12. bind(Hoge.class).toInstance(new Hoge("mukya!"));
  13. }
  14. };
  15. Module module2 = new AbstractModule() {
  16. @Override
  17. protected void configure() {
  18. bind(Foo.class).to(FooImpl.class);
  19. }
  20. };
  21. Injector injector = Guice.createInjector(module1, module2);
  22. Bar bar = injector.getInstance(Bar.class);
  23. System.out.println(bar.getMessage());
  24. System.out.println(bar.getMessage());
  25. Foo foo = injector.getInstance(Foo.class);
  26. System.out.println(foo.message());
  27. System.out.println(foo.num());
  28. }
  29. }