Spring: Easy way to use Dependency Injection in enum classes?


ATM I have code like this:

enum class Process(val implementation: ProcessInterface) {

Is it somehow possible to use dependency injection within SomeProcess? Those processes should be very generic, some have models, repositories and stuff and should mainly maintain stuff on there own.

I’m using latest spring boot.


I would write something like this:

enum class Process {

    lateinit var implementation: ProcessInterface
fun someProcess() = SomeProcess()


I don’t know enough about your design, but have you thought about sealed classes instead of an enum?

I would avoid field injection whenever possible!


Ah, sorry, it’s different in this case. Each implementation (classes that implement ProcessInterface) should be able to use @Autowired.