Skip to content

Defining multiple @Named @Bean methods returning same type in a @Factory fails to inject with NonUniqueBeanException #1097

Description

@wetted

named-factory-beans.zip

Task List

  • Steps to reproduce provided
    See below
  • Stacktrace (if present) provided
    Exception in thread "main" io.micronaut.context.exceptions.NonUniqueBeanException: Multiple possible bean candidates found: [named.factory.beans.Template, named.factory.beans.Template]
    at io.micronaut.context.DefaultBeanContext.findConcreteCandidate(DefaultBeanContext.java:1403)
    at io.micronaut.context.DefaultApplicationContext.findConcreteCandidate(DefaultApplicationContext.java:322)
    at io.micronaut.context.DefaultBeanContext.lastChanceResolve(DefaultBeanContext.java:1905)
    at io.micronaut.context.DefaultBeanContext.findConcreteCandidateNoCache(DefaultBeanContext.java:1846)
    at io.micronaut.context.DefaultBeanContext.lambda$findConcreteCandidate$50(DefaultBeanContext.java:1795)
    at io.micronaut.caffeine.cache.BoundedLocalCache.lambda$doComputeIfAbsent$14(BoundedLocalCache.java:2034)
    at java.util.concurrent.ConcurrentHashMap.compute(ConcurrentHashMap.java:1853)
    at io.micronaut.caffeine.cache.BoundedLocalCache.doComputeIfAbsent(BoundedLocalCache.java:2032)
    at io.micronaut.caffeine.cache.BoundedLocalCache.computeIfAbsent(BoundedLocalCache.java:2015)
    at io.micronaut.caffeine.cache.LocalCache.computeIfAbsent(LocalCache.java:113)
    at io.micronaut.caffeine.cache.LocalManualCache.get(LocalManualCache.java:54)
    at io.micronaut.context.DefaultBeanContext.findConcreteCandidate(DefaultBeanContext.java:1794)
    at io.micronaut.context.DefaultBeanContext.getBeanInternal(DefaultBeanContext.java:1590)
    at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:512)
    at io.micronaut.context.UnresolvedProvider.get(UnresolvedProvider.java:47)
    at named.factory.beans.ApplicationHelper.getTemplate(ApplicationHelper.java:15)
    at named.factory.beans.Application.main(Application.java:9)
  • Example that reproduces the problem uploaded to Github
    See attached
  • Full description of the issue provided (see below)

Steps to Reproduce

  1. create a Factory class
  2. create two or more Bean methods using Named qualifiers that all return the same type
  3. create a test or client class that injects one of those beans, qualified by Named value

Expected Behavior

The named bean should be injected as expected. I have also tried same type of injection in a test (@MicronautTest)

Actual Behavior

Fails with exception NonUniqueBeanException (as if I didn't qualify the beans)

Environment Information

  • Operating System: Mac Mojave/Windows 10
  • Micronaut Version: 1.0.3 + Micronaut Test 1.0.1
  • JDK Version: 1.8

Example Application

named-factory-beans.zip

  • TODO: link to github repository with example that reproduces the issue

Metadata

Metadata

Assignees

Labels

type: bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions