I am unable to get a bean to get injected on the most barebones app created using the Micronaut cli.
sdk install Micronaut to get the RC3 version of micronautmn create-app my-app --features jdbc-hikari@Controller("/") path.@Get("/") path / endpointThe endpoint should show "hello"
An error is produced with the following text
{"message":"Internal Server Error: Failed to inject value for parameter [someService] of class: my.app.SomeController\n\nMessage: No bean of type [my.app.SomeService] exists. Ensure the class is declared a bean and if you are using Java or Kotlin make sure you have enabled annotation processing.\nPath Taken: new SomeController([SomeService someService])"}
Full stack trace: https://gist.github.com/adityasrini/d0a76a716b8f793659951bdeb634ac2f
SomeService needs to be annotated with either @Singleton or @Prototype
https://github.com/adityasrini/my-app/pull/1 fixes the problem.
Now doing the same for DataSource dataSource throws the error:
{"message":"Internal Server Error: Failed to inject value for parameter [dataSource] of class: my.app.SomeService\n\nMessage: No bean of type [javax.sql.DataSource] exists. Ensure the class is declared a bean and if you are using Java or Kotlin make sure you have enabled annotation processing.\nPath Taken: new SomeController([SomeService someService]) --> new SomeService([DataSource dataSource])"}
All I added was the field with a constructor in the SimpleService class. The lines are here
Ok I added more properties to the application.yml and defined datasources this way
datasources:
default:
url: jdbc:postgresql://localhost:5432/postgres
username: postgres
password: ""
driverClassName: org.postgresql.Driver
and it was able to inject the datasource successfully!
@graemerocher May I suggest making a clearer DataSource error? Instead of having an injection failure message, having a DataSource exception somewhere in the stack trace would've made debugging Datasource errors a bit clearer.
BTW, you can just annotate
@Inject
Datasource datasource
No need for the constructor