Home > Software design >  Autowire beans by interface but exclude specific bean at injection time
Autowire beans by interface but exclude specific bean at injection time

Time:01-07

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

CodePudding user response:

Instead of injecting a list in the constructor, you can look up all beans of type Foo in the doSomething methods, and then exclude the current one. To look up, use any of the methods from BeanFactoryUtils, like beanNamesForTypeIncludingAncestors, and then exclude foo1, which is the bean name for class Foo1. The look up can even be coded into a default method in interface Foo.

CodePudding user response:

I agree with @robertobatts, it really looks like an antipattern and you simply should avoid it. If you could clarify the problem context maybe we can help you out with a better approach for what you would like to achieve!

  •  Tags:  
  • Related