4

Everything is in the question. For instance, for a Foo class containing a list of String :

public class Foo {

 private List<String> fooList;

 //getter and setter

}

in Xml, we can do the following :

<bean id="foo" class="Foo">
  <property name="fooList">
     <list>
        <value>bar</value>
        <value>baz</value>           
    </list>
</property>

Dimitri
  • 8,122
  • 19
  • 71
  • 128
  • Looks like you have to create a Provider for this, see the answer to the similar question http://stackoverflow.com/a/18105271/2807168 – leveluptor Sep 01 '14 at 17:07

1 Answers1

7

Using Dagger you would need to turn your fooList into a dependency of your Foo class. You'd do that by annotating it with @Inject. That will tell Dagger that when Foo gets created by Dagger, that also List<String> fooList needs to be injected. Keep in mind that the fooList isn't private in my example. Because Dagger doesn't use reflexion and uses code generation instead you have to declare injectable fields as public or private scope.

public class Foo {

    @Inject
    @Named("FooDependency")
    List<String> footList;

    // getter and setter

}

Here since List is a common type we annotate it with @Named("FooDependency") where "FooDependency" can be any string you desire. We do that because it's at all possible that you'd want to inject another List<String> else where in the app and we'd want to be able to differentiate between the 2. The other List<String> could be annotated with @Named("ADifferentDependency") for example.

Then you'll need to annotate a method with @Provide in one of your modules which would take care of providing that dependency.

@Module(injects = Foo.class)
public class SomeModule {
    @Provide
    @Named("FooDependency")
    public List<String> provideFooDependencyListOfString() {
        return Arrays.asList("1", "2", "3");
    }
}

Then you can use it

ObjectGraph objectGraph = ObjectGraph.create(new SomeModule());
Foo foo = objectGraph.get(Foo.class);
Miguel
  • 19,793
  • 8
  • 56
  • 46