micronaut-projects / micronaut-core

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

MongoClient injection fails after version 1.1.3 #1910

Closed Sabst closed 5 years ago

Sabst commented 5 years ago

Thanks for reporting an issue for Micronaut, please review the task list below before submitting the issue. Your issue report will be closed if the issue is incomplete and the below tasks not completed.

NOTE: If you are unsure about something and the issue is more of a question a better place to ask questions is on Stack Overflow (http://stackoverflow.com/tags/micronaut) or Gitter (https://gitter.im/micronautfw/). DO NOT use the issue tracker to ask questions.

Task List

Steps to Reproduce

  1. Create the app as described below using 1.1.4 or 1.2.0.RC1/RC2
  2. Navigate to the home page

Expected Behaviour

The beans are correctly injected and can be used by the application

Actual Behaviour

See below

Environment Information

Example Application

Create a new project using the appropriate version (ok with 1.1.3, fails with 1.1.4, 1.2.0.RC1/RC2):

mn create-app test-inject-v113 --features mongo-reactive,groovy

Add this controller:

package test.inject.v113
import com.mongodb.reactivestreams.client.MongoClient
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import javax.inject.Inject
import javax.inject.Named

@Controller("/")
class TestController {

    // Using @Inject will not solve the problem
    private MongoClient useraMongoClient
    private MongoClient userbMongoClient

    TestController(
            @Named("usera") MongoClient useraMongoClient,
            @Named("userb") MongoClient userbMongoClient) {
        this.useraMongoClient = useraMongoClient
        this.userbMongoClient = userbMongoClient
    }

@Get(produces = MediaType.TEXT_PLAIN)
    String home() {
        return "ok"
    }

}

And in application.yml, replace the existing mongodb entry by:

---
mongodb:
  cluster:
    maxWaitQueueSize: 5
  connectionPool:
    maxSize: 20
  servers:
    usera:
      uri: mongodb://usera:usera@${MONGO_HOST:127.0.0.1}:${MONGO_PORT:30002}/?authMechanism=SCRAM-SHA-1&authSource=admin
    userb:
      uri: mongodb://userb:userb@${MONGO_HOST:127.0.0.1}:${MONGO_PORT:30002}/?authMechanism=SCRAM-SHA-1&authSource=admin

Navigate to the home page and using 1.1.4 for example you get the error:

Message: Multiple possible bean candidates found: [com.mongodb.reactivestreams.client.MongoClient, com.mongodb.reactivestreams.client.MongoClient]
Path Taken: new TestController([MongoClient useraMongoClient],MongoClient userbMongoClient)
io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type  [test.inject.v114.TestController]

Message: Multiple possible bean candidates found: [com.mongodb.reactivestreams.client.MongoClient, com.mongodb.reactivestreams.client.MongoClient]
Path Taken: new TestController([MongoClient useraMongoClient],MongoClient userbMongoClient)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:1512)
        at io.micronaut.context.DefaultBeanContext.lambda$getScopedBeanForDefinition$53(DefaultBeanContext.java:1883)
        at java.util.concurrent.ConcurrentHashMap.computeIfAbsent(ConcurrentHashMap.java:1660)
        at io.micronaut.context.DefaultBeanContext.getScopedBeanForDefinition(DefaultBeanContext.java:1872)
        at io.micronaut.context.DefaultBeanContext.getBeanForDefinition(DefaultBeanContext.java:1854)
        at io.micronaut.context.DefaultBeanContext.getBeanInternal(DefaultBeanContext.java:1832)
        at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:997)
        at io.micronaut.context.AbstractBeanDefinition.getBeanForConstructorArgument(AbstractBeanDefinition.java:982)
        at test.inject.v114.$TestControllerDefinition.build(Unknown Source)

Using 1.1.3, the page contains "ok" as expected

graemerocher commented 5 years ago

Two things to note:

  1. This issue can be worked around if you include at least one connection that is the default connection
  2. It is resolved in core but the Mongo module will need to be recompiled with a version of Micronaut 1.1.5 or newer when it is released
l0co commented 5 years ago

What does it mean it's fixed and belongs to Milestone 1.1.5? Because it seems this fix isn't included in recently released 1.2.0. What is the relation between 1.1.5 and 1.2.0, then?

jameskleeh commented 5 years ago

@l0co It means the earliest release (by number) that this issue is resolved in is 1.1.5. As Graeme said the Mongo library will need compiled with 1.1.5 or greater. The fix is included in 1.2.0. All changes are merged up

Sabst commented 5 years ago

I confirm the fix is ok with 1.2.0 after recompiling micronaut mongo-reactive 1.1.1.BUILD-SNAPSHOT with this micronaut version.