Inject Generic Implementation using Guice

In order to use generics with Guice you need to use the TypeLiteral class to bind the generic variants. This is an example of how you’re Guice injector configuration could look like: package your-application.com; import com.google.inject.AbstractModule; import com.google.inject.TypeLiteral; public class MyModule extends AbstractModule { @Override protected void configure() { bind(new TypeLiteral<Repository<Class1>>(){}) .to(new TypeLiteral<MyRepository<Class1>>(){}); } } … Read more

Practical advice on using Jersey and Guice for RESTful service

Guice integration with Jersey has not stagnated. The opposite is true. Thanks to Paul and his cohorts behind Jersey, the latest 1.7 release contains a special JerseyServletModule class to work with Guice-based servlets. Guice-based constructor injection into JAX-RS resource works! The issue is using JAX-RS annotations such as @QueryParam in the constructor of a JAX-RS … Read more

Why use/develop Guice, when You have Spring and Dagger? [closed]

It’s important to acknowledge that Dagger was created after Guice, by one of Guice’s creators (“Crazy Bob” Lee) who had moved to Square: Rod Johnson originally released Spring in October 2002 with his book Expert One-on-One J2EE Design and Development; Spring was then released publicly on the Apache license in June 2003 and as v1.0 … Read more

How to use Guice’s AssistedInject?

Check the javadoc of FactoryModuleBuilder class. AssistedInject allows you to dynamically configure Factory for class instead of coding it by yourself. This is often useful when you have an object that has a dependencies that should be injected and some parameters that must be specified during creation of object. Example from the documentation is a … Read more

Google Guice vs. PicoContainer for Dependency Injection

You may want to include Spring in your list of Dependency Injection frameworks you are considering. Here are some answers to your questions: Coupling to the framework Pico – Pico tends to discourage setter injection but other than that, your classes don’t need to know about Pico. It’s only the wiring that needs to know … Read more

Overriding Binding in Guice

This might not be the answer you’re looking for, but if you’re writing unit tests, you probably shouldn’t be using an injector and rather be injecting mock or fake objects by hand. On the other hand, if you really want to replace a single binding, you could use Modules.override(..): public class ProductionModule implements Module { … Read more