Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. class MyApplication {
  2.      private MessageService service;
  3.      
  4.     @Inject
  5.     public void setService(MessageService svc){
  6.         this.service=svc;
  7.     }
  8. }
  9.  
  10. class AppInjector extends AbstractModule {
  11.     @Override
  12.     protected void configure() {
  13.         bind(MessageService.class).to(EmailService.class);
  14.     }
  15. }
  16.  
  17. public class ClientApplication {
  18.      public static void main(String[] args) {
  19.         Injector injector = Guice.createInjector(new AppInjector());      
  20.         MyApplication app = injector.getInstance(MyApplication.class);
  21.  
  22.         app.sendMessage(); // how does injection work here if there is no call of app.setService() ?
  23.  
  24.         // is this a correct way to change binding during a runtime? i guess no ;/
  25.         app.setService(new AnotherService());
  26.         app.sendMessage();
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement