Guest User

Untitled

a guest
Apr 15th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. bind(String.class)
  2. .annotatedWith(Names.named("JDBC URL"))
  3. .toInstance("jdbc:mysql://localhost/pizza");
  4.  
  5. class SomeClass {
  6. @Inject
  7. SomeClass(@Named("JDBC URL") String jdbcUrl);
  8. this.jdbcUrl = jdbcUrl;
  9. }
  10. }
  11.  
  12. class SomeClass {
  13. @Inject
  14. SomeClass(@JdbcUrl String jdbcUrl);
  15. this.jdbcUrl = jdbcUrl;
  16. }
  17.  
  18. @Retention(RetentionPolicy.RUNTIME)
  19. @Target({ElementType.FIELD, ElementType.PARAMETER})
  20. @BindingAnnotation
  21. public @interface JdbcUrl {}
  22. }
  23.  
  24. public class SomeModule extends AbstractModule {
  25. private final String jdbcUrl; // set in constructor
  26.  
  27. protected void configure() {
  28. bindConstant().annotatedWith(SomeClass.JdbcUrl.class).to(jdbcUrl);
  29. }
  30. }
  31.  
  32. class SomeOtherClass {
  33. @Inject
  34. SomeOtherClass(SomeClass someClass);
  35. this.someClass = someClass;
  36. }
  37.  
  38. SomeClass instance = Guice.createInjector(new MyModule("any string i like to use")).getInstance(SomeClass.class);
Add Comment
Please, Sign In to add comment