Search code examples
javadependency-injectionmicronaut

DI error in Micronaut shows up in runtime instead of compile time?


I recently made a broken bean and got an error seemingly pertaining to DI: Message: No bean of type [java.lang.String] exists. Ensure the class is declared a bean and if you are using Java or Kotlin make sure you have enabled annotation processing.

I'm not surprised that i got the error - my bean was obviously broken. What did surprise me however was that it broke at run-time (when sending the first request where the broken bean had to be instantiated) whereas I would've expected such an error at compile time when using Micronaut.

The bean in question uses the @Inject annotation on the constructor, not on any private field where I understand run-time reflection becomes necessary.

How come such an error is not detected when the DI engine is run - at compile time?


Solution

  • It's impossible to know that a bean won't exist at compile time. A jar with bean definitions may only be on the runtime classpath.