PageRenderTime 758ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/langs/java/xamps/GuiceXamp.java

https://github.com/dburger/archive
Java | 160 lines | 111 code | 41 blank | 8 comment | 0 complexity | 51a7305c3734b8158c7f1400b30e9c9a MD5 | raw file
  1. package com.dburger.guice.xamp;
  2. import com.google.inject.AbstractModule;
  3. import com.google.inject.Guice;
  4. import com.google.inject.Inject;
  5. import com.google.inject.Injector;
  6. import com.google.inject.Provider;
  7. import com.google.inject.Provides;
  8. import com.google.inject.name.Named;
  9. import com.google.inject.name.Names;
  10. public class GuiceXamp {
  11. public static interface LinkedBinding {
  12. void linkedBinding();
  13. }
  14. public static interface InstanceBinding {
  15. void instanceBinding();
  16. }
  17. public static interface ProviderMethodBinding {
  18. void providerMethodBinding();
  19. }
  20. public static interface ProviderBinding {
  21. void providerBinding();
  22. }
  23. public static class LinkedBindingImpl implements LinkedBinding {
  24. public LinkedBindingImpl() {}
  25. public void linkedBinding() {
  26. System.out.println("i come from linked binding");
  27. }
  28. }
  29. public static class InstanceBindingImpl implements InstanceBinding {
  30. public void instanceBinding() {
  31. System.out.println("i come from instance binding");
  32. }
  33. }
  34. public static class ProviderBindingProvider
  35. implements Provider<ProviderBinding> {
  36. public ProviderBinding get() {
  37. return new ProviderBinding() {
  38. public void providerBinding() {
  39. System.out.println("i come from a provider binding");
  40. }
  41. };
  42. }
  43. }
  44. public static class WorldDominationModule extends AbstractModule {
  45. @Override
  46. public void configure() {
  47. // 1. "linked" binding
  48. bind(LinkedBinding.class).to(LinkedBindingImpl.class);
  49. // 2. "instance" binding -- with an annotation
  50. bind(String.class).annotatedWith(Names.named("JDBC URL"))
  51. .toInstance("jdbc:mysql://localhost/whatever");
  52. // you cannot be serious
  53. bind(InstanceBinding.class)
  54. .toInstance((InstanceBinding)new InstanceBindingImpl());
  55. // 4. Provider bindings
  56. bind(ProviderBinding.class)
  57. .toProvider(ProviderBindingProvider.class);
  58. // 5. no JIT examples
  59. // 6. method injection parameter
  60. bind(String.class).annotatedWith(Names.named("method injection"))
  61. .toInstance("i was method injected");
  62. // 7. field injection
  63. bind(String.class).annotatedWith(Names.named("field injection"))
  64. .toInstance("i was field injected");
  65. }
  66. // 3. @Provides method binding
  67. @Provides
  68. public ProviderMethodBinding provideProviderMethodBinding() {
  69. return new ProviderMethodBinding() {
  70. public void providerMethodBinding() {
  71. System.out.println("i come from a provider method binding");
  72. }
  73. };
  74. }
  75. }
  76. public static class WorldDominator {
  77. private LinkedBinding linkedBinding;
  78. private InstanceBinding instanceBinding;
  79. private String connUrl;
  80. private ProviderBinding providerBinding;
  81. private ProviderMethodBinding providerMethodBinding;
  82. private String methodInjection;
  83. @Inject @Named("field injection")
  84. private String fieldInjection;
  85. @Inject
  86. public WorldDominator(LinkedBinding linkedBinding,
  87. InstanceBinding instanceBinding,
  88. @Named("JDBC URL") String connUrl,
  89. ProviderMethodBinding providerMethodBinding,
  90. ProviderBinding providerBinding) {
  91. this.linkedBinding = linkedBinding;
  92. this.instanceBinding = instanceBinding;
  93. this.connUrl = connUrl;
  94. this.providerMethodBinding = providerMethodBinding;
  95. this.providerBinding = providerBinding;
  96. }
  97. @Inject
  98. public void setMethodInjection(@Named("method injection") String methodInjection) {
  99. this.methodInjection = methodInjection;
  100. }
  101. public void dominate() {
  102. this.linkedBinding.linkedBinding();
  103. this.instanceBinding.instanceBinding();
  104. System.out.println("the connUrl turned out to be: " + this.connUrl);
  105. this.providerMethodBinding.providerMethodBinding();
  106. this.providerBinding.providerBinding();
  107. System.out.println(this.methodInjection);
  108. System.out.println(this.fieldInjection);
  109. }
  110. }
  111. public static void main(String[] args) {
  112. System.out.println("hello world, going to inject...");
  113. System.out.println("=================================================");
  114. Injector injector = Guice.createInjector(new WorldDominationModule());
  115. WorldDominator wd = injector.getInstance(WorldDominator.class);
  116. wd.dominate();
  117. System.out.println("=================================================");
  118. }
  119. }