I'm currently injecting a list of Spring @Component which all implement the same interface. This works fine however I want to inject this same list into an instance of a bean in that list so I can act on all the other beans in that list. I can't see how to exclude a specific bean from that injection, I can filter the list after injecting it but this of course results in a Spring circular dependency exception at startup. My question is can I at the point of injecting tell spring to exclude the class being injected to from the list of beans?
public interface Foo {
String doSomething();
}
@Component
public class Foo1 implements Foo {
private final List<Foo> foos;
public Sample(final List<Foo> foos) {
//Don't include Foo1
this.foos = foos;
}
public String doSomething() {
foos.forEach(foo -> foo.doSomething());
return "aString";
}
}
@Component
public class Foo2 implements Foo {
private final List<Foo> foos;
public Sample(final List<Foo> foos) {
//Don't include Foo2
this.foos = foos;
}
public String doSomething() {
foos.forEach(foo -> foo.doSomething());
return "anotherString";
}
}
@Component
public class Foo3 implements Foo {
//and so on