Micronaut-core: Add support for narrowing bean candidates by generic type arguments

Created on 9 Feb 2020  路  2Comments  路  Source: micronaut-projects/micronaut-core

If I have an interface with a generic type parameter as a bean, is it expected that Micronaut struggles to inject the right instance?

full context:
https://github.com/visionary-software-solutions/vitalizr-web/commit/46e6fb6b30219f840fa26aa73a606856d8b6be6b

Trying to run the BodyMassIndexControllerTest yields:

Unexpected error occurred: Failed to inject value for field [service] of class: visionary.software.vitalizr.web.bmi.BodyMassIndexController

Path Taken: BodyMassIndexController.service
io.micronaut.context.exceptions.DependencyInjectionException: Failed to inject value for field [service] of class: visionary.software.vitalizr.web.bmi.BodyMassIndexController

Caused by: io.micronaut.context.exceptions.NonUniqueBeanException: Multiple possible bean candidates found: [visionary.software.vitalizr.web.bmi.$BodyMassIndexControllerTest$Service0Definition$Intercepted, visionary.software.vitalizr.web.weight.WeightService]

This seems to steer dangerously close to the Guice TypeLiteral mess because of Generic Type Erasure.

But I don't see anything like that mentioned in the IOC docs, and had hoped annotation processing would enable some tricks to avoid it.

Bug: I don't expect a VitalService<Weight> to match in the IOC context with a VitalService<BodyMassIndex> when trying to inject into the controllers

Task List

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

Steps to Reproduce

  1. check out src
  2. ./gradlew test
  3. see test failures

Expected Behaviour

BodyMassIndexController is injected with only BMIService, WeightController with WeightService

Actual Behaviour

Caused by: io.micronaut.context.exceptions.NonUniqueBeanException: Multiple possible bean candidates found: [visionary.software.vitalizr.web.bmi.$BodyMassIndexControllerTest$Service0Definition$Intercepted, visionary.software.vitalizr.web.weight.WeightService]

Environment Information

  • Operating System: Ubuntu 19.04
  • Micronaut Version: 1.2.1
  • JDK Version: OpenJDK11

Example Application

enhancement

Most helpful comment

Been bitten by this too. @leadVisionary did you find any workaround for this?

All 2 comments

Been bitten by this too. @leadVisionary did you find any workaround for this?

Was this page helpful?
0 / 5 - 0 ratings