Micronaut-core: Bean injection fails on barebones app

Created on 21 Oct 2018  路  5Comments  路  Source: micronaut-projects/micronaut-core

Task List

  • [X] Steps to reproduce provided
  • [X] Stacktrace (if present) provided
  • [X] Example that reproduces the problem uploaded to Github
  • [x] Full description of the issue provided (see below)

I am unable to get a bean to get injected on the most barebones app created using the Micronaut cli.

Steps to Reproduce

  1. Run sdk install Micronaut to get the RC3 version of micronaut
  2. Run mn create-app my-app --features jdbc-hikari
  3. Create empty service class
  4. Create controller class with service class as field. Use @Controller("/") path.
  5. Create constructor for controller with service class instantiation (Repo has code example)
  6. Create method with "hello" String return for a @Get("/") path
  7. Run app and hit / endpoint

Expected Behaviour

The endpoint should show "hello"

Actual Behaviour

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

Environment Information

  • Operating System: macOS 10.14
  • Micronaut Version: RC3
  • JDK Version: 11
  • Gradle Version: 4.10 (the problem persists with 5.0-milestone-1 as well)

Example Application

https://github.com/adityasrini/my-app

notabug

All 5 comments

SomeService needs to be annotated with either @Singleton or @Prototype

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

Was this page helpful?
0 / 5 - 0 ratings