Git Product home page Git Product logo

Comments (4)

hrothwell avatar hrothwell commented on May 28, 2024

When you specify the replacement bean using @Singleton I would assume it would then remove all non-singletons of said type and thus replace your qualified bean.

Also in this example, what benefit is there to using @Replaces? If you need a specific instance of this type that you can access without using qualifiers (ie a default) you might be best suited using @Primary in this example, assuming that is the intention of the replacing going on

from micronaut-core.

hrothwell avatar hrothwell commented on May 28, 2024

not sure if what I described above is your use case, but if so this along the lines of what I was describing

import io.micronaut.context.annotation.*;
import jakarta.inject.Inject;
import jakarta.inject.Named;

@Factory
@Prototype
class MyFactory {

    @Named("blah")
    @Prototype
    String blah() {
        return "blah";
    }

    @Bean
    @Primary
    String primary() {
        return "primary";
    }
}

@Prototype
class MyClass {

    String myString;

    @Inject
    public MyClass(@Named("blah") String s) {
        myString = s;
    }

    public String getMyString(){
        return myString;
    }
}

@Prototype
class MyClassTwo{
    String myString;
    @Inject
    public MyClassTwo(String s){
        myString = s;
    }

    public String getMyString(){
        return myString;
    }
}
import io.micronaut.inject.qualifiers.Qualifiers
import io.micronaut.runtime.EmbeddedApplication
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import jakarta.inject.Inject
import spock.lang.Specification

@MicronautTest
class ExampleSpec extends Specification{
    @Inject
    EmbeddedApplication<?> application

    def 'named bean can be found'(){
        when:
        def bean = application.getApplicationContext().getBean(String.class, Qualifiers.byName('blah'))

        then:
        bean == 'blah'
    }

    def 'when no qualifier return primary'(){
        when:
        def bean = application.getApplicationContext().getBean(String.class)

        then:
        bean == 'primary'
    }

    def 'classes that inject can use the named or the primary'(){
        when:
        def myClass = application.getApplicationContext().getBean(MyClass.class)
        def myClassTwo = application.getApplicationContext().getBean(MyClassTwo.class)

        then:
        myClass.getMyString() == 'blah'
        myClassTwo.getMyString() == 'primary'
    }
}

from micronaut-core.

RonBarkan avatar RonBarkan commented on May 28, 2024

Also in this example, what benefit is there to using @Replaces?

The complete example has a Micronaut provided default instance, which needs to be replaced. Think that there's another @Factory somewhere that provides the same unqualified type, which needs replacement. However, it is not necessary in order to show the defect. I updated the first post anyway to note this.

The singleton question is only tangentially related, and should definitely not affect the qualified bean, because the qualified and unqualified beans should be unrelated and separate.

Indeed, @Primary does work, but I don't see any reason why @Replaces does not.
In particular, @Replaces has named and qualifier modifiers, which allows replacing @Named("blah") (in theory, I did not check), but they seem to be useless to not erase the qualified bean.

from micronaut-core.

RonBarkan avatar RonBarkan commented on May 28, 2024

In any case, the current behavior does not make sense.
Either:

  1. The @Replaces actually replaces all instances, including differently qualified. This will be a questionable design, but maybe someone can justify it.
  2. It impacts only the very specific type + name/qualifier (if any) and not any other of same type but different qualifier. This seems the most logical and easy to reason about.

Instead, the code erases the differently qualified beans from existence (!) and replaces the one that requires replacement.

from micronaut-core.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.