Passing objects / classes with Guice

less than 1 minute read

@BindConstant will only let you to bind primitive data types such as int, char or String. If you want to pass a custom object, instance or class you need to use @Provides keyword. Let’s say your class which extends AbstractModule has a setter,

public class GAModule extends AbstractModule{
	//@Constant(value = "elements")
	protected List<Foo> elements;

	@Provides List<Foo> getElements() {
		return elements;
	}

	public void setElements(List<Foo> elements) {
		this.elements = elements;
	}
//...

Call setElements from outer class. When you need it within Guice, use @Provider

public class FooUser {
	Provider<List<Foo>> elementsProvided;

	@Inject
	public FooUser( Provider<List<Foo>> elementsProvided) {
		this.elementsProvided= elementsProvided;
		List<Foo> elements = elementsProvided.get();
		//Now you can access the elements!
	}
}

That’s it! It took me a long time to find this. In my case, I want to use Opt4J (a multi objective genetic algorithms library for Java) and I want to pass a List of objects to Creator class. I implemented setter class in Module class and used @Provider keyword in Creator class. Works like a charm now.

I hope it helps you and saves the time that cost me!

Leave a Comment