0

What is the difference between this:

@Inject
    public GuitarPlayer(Provider<Instrument> ins) {
        instruments = new LinkedList<Instrument>();
        for (int i = 0; i < 5; i++) {
            instruments.add(ins.get());
        }
    }

and this:

@Autowired
    public GuitarPlayer(List<Instrument> ins) {
        instruments = new LinkedList<Instrument>();
        for (int i = 0; i < 5; i++) {
            instruments.add(ins.get(i);
        }
    }

I assume that in the first example every position in the list will be a separate instance. If so is it possible to inject in my xml configuration file multiple classes which implement the same interface?

milo
  • 427
  • 5
  • 14
  • Is it normal that one has `Provider` while the other has `Instrument`? – Tunaki Nov 28 '15 at 20:59
  • 6
    http://stackoverflow.com/q/7142622/1743880 – Tunaki Nov 28 '15 at 21:00
  • I'm just following the example from "Spring in action " book and it says that when I'm using @Inject I need to use Provider also. – milo Nov 28 '15 at 21:00
  • 1
    You can autowire a `List` directly if you have multiple implementations of `Instrument`. I don't know about xml because that's not needed anymore. http://stackoverflow.com/questions/7725405/spring-dependency-injection-inject-all-instances-of-interface (works with `@Autowired` as well) – zapl Nov 28 '15 at 21:24
  • Thanks it solved my problem. – milo Nov 28 '15 at 21:30

0 Answers0