Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-17056

Qualified beans are not autowired

    XMLWordPrintable

    Details

      Description

      Hello,

      When defining multiple beans with the same qualifier, I expect the beans to end up in a list that can be autowired. It all works fine until a certain (weird) point.

       I have a bean called MyBean:

      public MyBean(String name, Function<String, String> function) {
          this.name = name;
          this.function = function;
      }
      

       I configure multiple beans of the same type (see: RootConfiguration.java):

      @Bean
      @Qualifier("myBean")
      MyBean myBean1() {
          return new MyBean("bean1", this::functionMyBean1);
      }
      
      // ...
      
      private String functionMyBean1(String in) {
          return in;
      }
      
      // ...
      

       Then I can retrieve them all using:

      @Autowired
      private List<MyBean> myBeans;
      

       The weird issue is that if I overload the bean creation method to define the function passed as the constructor parameter, then the bean is not added to the autowired list:

      @Bean
      @Qualifier("myBean")
      MyBean myBean1() {
          return new MyBean("bean1", this::myBean1);
      }
      
      private String myBean1(String in) { // overloaded method -> bean is not added
          return in;
      }
      

       That's not a big issue since the workaround is obvious but I really would like to understand what's going on behind the scenes so the DI does not work as expected.
      Note that the bean that is not injected in the list is eventually created, but never acknowledged as part of its "siblings".

      Thanks in advance for your insight !

        Attachments

          Activity

            People

            Assignee:
            juergen.hoeller Juergen Hoeller
            Reporter:
            tholivieri Thomas OLIVIERI
            Last updater:
            Spring Issues Spring Issues
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Days since last comment:
              3 years, 30 weeks, 5 days ago