Google Guice Difference between Spring Autowire vs Guice - SGajre/Tutorial GitHub Wiki

Google Guice is the framework to automate the dependency injection in applications. Google Guice is one of the leading frameworks whose main work is to provide automatic implementation of dependency injection.

  1. Spring is a comprehensive stack, Guice focuses on dependency injection.
  2. Guice is anything but a rehash of Spring
  3. Guice was born purely out of use cases from one of Google's biggest (in every way) applications: AdWords.
  4. Guice wholly embraces annotations and generics, thereby enabling you to wire together and test objects with measurably less effort. Guice proves you can use annotations instead of error-prone, refactoring-adverse string identifiers.
  5. Some new users fear that the number of annotations will get out of hand.
  6. Google Guice 3.0 added the support for JSR-330
  7. Spring supports explicit configuration and auto-wiring.
  8. Explicit configuration is verbose but maintainable.
  9. Auto-wiring is concise but slow and not well suited to non-trivial applications.
  10. If you have 100s of developers and 100s of thousands of lines of code, auto-wiring isn't an option.
  11. Guice uses annotations to support a best-of-both-worlds approach which is concise but explicit (and maintainable).
  12. Guice works with Spring when possible. You can bind your existing Spring beans. Guice supports AOP Alliance method interceptors which means you can use Spring's popular transaction interceptor.

Other points:

Spring objects are recognized based on their names and you cannot have two objects with the same name. (for just understanding example Map<String, Object>)

Guice objects are recognized based on their classes and you cannot have two objects of the same type. (for just understanding example Map<Class<?>, Object>)

Google Guice support both setter-based and constructor-based dependency injection.

==================================================================================================================================================

@Provide : When you need code to create an object, use an @Provides method. Ex:@Provide [@paypal] TransactionXX provideMethod(){}

@Provides @PayPal CreditCardProcessor providePayPalCreditCardProcessor( @Named("PayPal API key") String apiKey) { PayPalCreditCardProcessor processor = new PayPalCreditCardProcessor(); processor.setApiKey(apiKey); return processor; }

Throwing Exception: Wrapped in a ProvisionException -- Bad Practice

@CheckedProvides

==============================================================================================

Guice, are a good fit: objects created either at class loading time or very early in your application. These two aspects are covered by either direct injection or by providers,

STEPS:

  1. Add jar to class path or add depency to maven
  2. Create Interface
  3. Implement Interface with annotation @Singleton
  4. Injecting the dependent class ie. interface
  5. Extending AbstractModule to bind

Step1:

com.google.inject guice 3.0

Step 2:

public interface MessageService {

boolean sendMessage(String msg, String receipient);

}


@Singleton public class EmailService implements MessageService {

public boolean sendMessage(String msg, String receipient) {
    //some fancy code to send email
    System.out.println("Email Message sent to "+receipient+" with message="+msg);
    return true;
}

}


Injecting the dependent class:

public class MyApplication {

private MessageService service;

// constructor based injector // @Inject // public MyApplication(MessageService svc){ // this.service=svc; // }

//setter method injector
@Inject
public void setService(MessageService svc){
    this.service=svc;
}
 
public boolean sendMessage(String msg, String rec){
    //some business logic here
    return service.sendMessage(msg, rec);
}

}


Create binding:

public class AppInjector extends AbstractModule {

@Override
protected void configure() {
    //bind the service to implementation class
    //bind(MessageService.class).to(EmailService.class);
     
    //bind MessageService to Facebook Message implementation
    bind(MessageService.class).to(FacebookService.class);
     
}

}

Client:

public class ClientApplication {

public static void main(String[] args) {
    Injector injector = Guice.createInjector(new AppInjector());       
     
    MyApplication app = injector.getInstance(MyApplication.class);
     
    app.sendMessage("Hi Pankaj", "[email protected]");
}

}

⚠️ **GitHub.com Fallback** ⚠️