Wiring beans that do not implement an tag interface in list using Spring

riccardo.cardin :

I have a set of classes that extends an abstract class Executor. Let's say that these classes are ExecutorOne, ExecutorTwo and ExecutorThree. Then, I have a fourth class extending the Executor type, ExecutorFour, but also implementing the interface NotUsable.

I am using Spring 4.x to inject an instance of all the above beans into a list.

@Autowired
private final List<Executor> executors;

Is there any mechanism that allows me to not inject a bean of type ExecutorFour inside the list executors? I want the list executors to contain only three beans, respectively of type ExecutorOne, ExecutorTwo and ExecutorThree.

I tried to have a look at the @Conditional annotation, but it seems it is no use in this situation.

Thanks in advance.

Mark Bramnik :

There is another option, it helps to achieve what you try, but doesn't answer the question directly.

Spring doesn't have an exclusion mechanism like this, after all, all 4 beans are valid spring beans and are created.

However, you can work with Usable interface, instead of NotUsable marker interface. Here is a (pretty much self-explanatory) example:

interface Executor {}

interface UsableExecutor extends Executor {}

class Executor1 implements UsableExecutor{...}
class Executor2 implements UsableExecutor{...}
class Executor3 implements UsableExecutor{...}
class Executor4 implements Executor {}  // note, doesn't implement UsableExecutor

@Component
class SomeClassWithUsableExecutors {

   private final List<UsableExecutor> usableExecutors;

   // an autowired constructor - only 3 elements will be in the list 
   public SomeClassWithUsableExecutors(List<UsableExecutor> usableExecutors) {
      this.usableExecutors = usableExecutors;
   } 
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=97438&siteId=1