Open rsenden opened 2 years ago
This seems to be somewhat related to the following block in build.gradle
:
micronaut {
testRuntime("junit5")
processing {
incremental(true)
annotations("org.sample.micronaut.*")
}
}
The annotations
option in combination with our custom annotations seems to make Micronaut properly process class hierarchies that have any of our custom annotations. Removing the annotations
option makes the other commands (with our custom annotations) fail as well.
However, I'd expect Micronaut to automatically pick up any jakarta.inject
annotations independent of whether there are any custom annotations defined in the class hierarchy. Also, removing the annotations
option (just in case we are overriding some defaults like jakarta.inject.*
), or explicitly specifying annotations("jakarta.inject.*")
doesn't fix the issue.
I ran into a similar problem. I discovered if the abstract class has an @Inject field, that doesn't make the subclass a Micronaut bean. You need to either add an @Inject field to the subclass or make it @Singleton or @Prototype.
Expected Behavior
In our Micronaut/Picocli based command line application, we have multiple abstract classes containing fields annotated with
@Inject
. These abstract classes are used as base classes for concrete Picocli@Command
implementations. We'd expect Micronaut to properly inject these fields with the corresponding@Singleton
instances.Actual Behaviour
@Inject
get injected properly.@Inject
do not get injected and remain null.Steps To Reproduce
./gradlew clean shadowJar
java -jar build/libs/mncli.jar annotated-superclass
; outputsSingleton called
java -jar build/libs/mncli.jar annotated-subclass
; outputsSingleton called
java -jar build/libs/mncli.jar field-annotation
; outputsSingleton called
java -jar build/libs/mncli.jar no-annotation
; fails with NPE because singleton was not injectedjava -jar build/libs/mncli.jar standard-annotation
; fails with NPE because singleton was not injectedEnvironment Information
Example Application
https://github.com/rsenden/micronaut-inject-issue
Version
3.4.2