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.
- Spring is a comprehensive stack, Guice focuses on dependency injection.
- Guice is anything but a rehash of Spring
- Guice was born purely out of use cases from one of Google's biggest (in every way) applications: AdWords.
- 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.
- Some new users fear that the number of annotations will get out of hand.
- Google Guice 3.0 added the support for JSR-330
- Spring supports explicit configuration and auto-wiring.
- Explicit configuration is verbose but maintainable.
- Auto-wiring is concise but slow and not well suited to non-trivial applications.
- If you have 100s of developers and 100s of thousands of lines of code, auto-wiring isn't an option.
- Guice uses annotations to support a best-of-both-worlds approach which is concise but explicit (and maintainable).
- 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:
- Add jar to class path or add depency to maven
- Create Interface
- Implement Interface with annotation @Singleton
- Injecting the dependent class ie. interface
- Extending AbstractModule to bind
Step1:
com.google.inject guice 3.0public 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]");
}
}