Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: name qualifier and no qualifier aligns with Guice #4

Merged
merged 6 commits into from
May 21, 2024

Conversation

sdelamo
Copy link
Contributor

@sdelamo sdelamo commented May 20, 2024

For a Guice module:

public class GreeterModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(Greeter.class).to(DefaultGreeter.class).in(Scopes.SINGLETON);
        bind(Greeter.class).annotatedWith(Names.named("pirate")).to(PirateGreeter.class).in(Scopes.SINGLETON);
    }

Guice returns DefaultGreeter when you do:

Injector injector = Guice.createInjector(new GreeterModule());
Greeter greeter = injector.getInstance(Greeter.class);

However, Micronaut returns Multiple possible bean candidates found: [Greeter, Greeter] for:

@Inject
io.micronaut.context.BeanContext context;

@Test
void contextGetBean() {
context.getBean(Greeter.class)
}

Not sure if this is a behaviour we want to align.
We could register PirateGreeter with @Secondary so that BeanContext context;context..get(Greeter.class) does not throrw multiple beans exceptions or maybe DefaultGreeter with @Primary.

For a Guice module:

```java
public class GreeterModule extends AbstractModule {
    @OverRide
    protected void configure() {
        bind(Greeter.class).to(DefaultGreeter.class).in(Scopes.SINGLETON);
        bind(Greeter.class).annotatedWith(Names.named("pirate")).to(PirateGreeter.class).in(Scopes.SINGLETON);
    }
```

Guice returns `DefaultGreeter` when you dou:

```java
Injector injector = Guice.createInjector(new GreeterModule());
Greeter greeter = injector.getInstance(Greeter.class);
```

However, Micronaut returns ` Multiple possible bean candidates found: [Greeter, Greeter]` for:

``java
@Inject
io.micronaut.context.BeanContext context;

@test
void contextGetBean() {
context.getBean(Greeter.class)
}
```
Copy link

@graemerocher graemerocher merged commit 3f173a9 into master May 21, 2024
7 checks passed
@graemerocher graemerocher deleted the add-example branch May 21, 2024 08:20
@graemerocher graemerocher added the type: improvement A minor improvement to an existing feature label May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: improvement A minor improvement to an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants