micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6.08k stars 1.07k forks source link

Micronaut 4.0.6 Groovy `@Inject` Not Working in `@Endpoint` class #9811

Open wmunyan opened 1 year ago

wmunyan commented 1 year ago

Issue description

The Issue

I have a groovy project using Micronaut 4.0.6 and I have annotated a class with @Endpoint, but then I cannot @Inject a dependency, it throws a NullPointerException

Expected Behavior

Dependency Injection should work

Actual Behavior

16:07:42.425 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 971ms. Server Running: http://localhost:8080
16:07:51.549 [default-nioEventLoopGroup-1-3] ERROR i.m.http.server.RouteExecutor - Unexpected error occurred: Cannot invoke method heartbeat() on null object
java.lang.NullPointerException: Cannot invoke method heartbeat() on null object

The null object is the Service class that is supposed to be @Injected

Steps to Reproduce

Run the example application at https://github.com/wmunyan/heartbeat

NOTE: When I replace the @Endpoint annotation and just make the class a regular @Controller, the dependency injection works fine.

Environment

Windows 10 Java 17 Groovy 4 Micronaut 4.0.6 (created via micronaut launch)

Example Application

https://github.com/wmunyan/heartbeat

NOTE: Submitting a "Bug Report" doesn't seem to be allowed for me. I click the button, it turns disabled, but doesn't do anything nor does it navigate away from the page, and it isn't obvious that anything is wrong with the data I entered.

dstepanov commented 1 year ago

Your example application doesn't have any classes

wmunyan commented 1 year ago

@dstepanov Sorry about that - There should be code there now

wmunyan commented 1 year ago

I've been playing with this a little bit and found an interesting bit of info... If I try to use:

@Inject
HeartbeatService heartbeatService

The code fails and the service fails to inject. However, if I use Constructor injection:

final HeartbeatService heartbeatService

HeartbeatController(HeartbeatService heartbeatService) {
    this.heartbeatService = heartbeatService
}

... the injection works and the test passes. Is there something within the @Endpoint annotation that has changed from Micronaut 3 to 4 that doesn't allow field-level injection anymore?

wmunyan commented 1 year ago

Another small update on this; I recreated the project in Java (and not Groovy) and the @Inject method works as expected, so perhaps this issue is Groovy-related with the @Endpoint annotation?

yawkat commented 1 year ago

looks like this happens because @Endpoint is annotated with @ConfigurationReader – why is that @graemerocher ?