How can an autowired list of beans include classes not explicitly marked as Components?

Viewed 22

Imagine I have @Component classes RefreshCmd and StartCmd which extend an abstract class Command.

@Autowire
List<Command> commandList; 

The list above is succesfully filled by Spring at runtime with RefreshCmd and StartCmd.

My issue is with beans which don't have their own exclusive class, let's assume:

@Configuration
//some class

@Bean
public SimpleCommand startCommand(){
    return new SimpleCommand("some arg", () -> some lambda);
}

@Bean
public SimpleCommand helpCommand(){
    return new SimpleCommand("some arg", () -> some lambda);
}

These beans are not included in the list. I want them to be included.

How can method-defined beans (so without the possibility of annotating with @Component) be included in the list?

One solution I found is to use @Resource() to autowire them to individual fields in another class and then add them to the list with @PostConstruct - this looks horrible and I'm looking for the correct way of doing things.

0 Answers
Related