mardi 5 mars 2019

Select Spring bean according to enum param

In my Spring Boot app I have a notion of Stage and StageProcessor which processes the Stage. Stage has a StageType enum property. I have different implementations of StageProcessor interface, and these implementations are Spring beans. Now, I have another Spring bean, WorkflowProcessor, which needs to invoke appropriate StageProcessor depending on StageType of the Stage. So far I have come up with the following:

@Service
public class StageConfig {
    @Autowired
    private StageProcessorA stageProcessorA;
    @Autowired
    private StageProcessorB stageProcessorB;

    public StageProcessor getProcessor(Stage stage) {
        switch(stage.getType()) {
            case A:
                return stageProcessorA;
                break;
            case B:
                return stageProcessorB;
                break;
        }
    }
}

I wonder if I am missing any design pattern, or Spring mechanism. Any ideas of a better design?

Aucun commentaire:

Enregistrer un commentaire