Spring 4.1 @Qualifier doesnt'work

前端 未结 8 1069
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 06:11

I have classes, xml configuration file and error stack trace like this. I have no idea why @Qualifier doesn\'t work. I see on errors that he don\'t even do anything.

相关标签:
8条回答
  • 2020-12-18 07:12

    @Qualifier(name="..") annotation and give the name of the bean that we want Spring to inject

    and name of your beans are size1 and size2 .

    so try

       @Qualifier("size1")
    
    0 讨论(0)
  • 2020-12-18 07:18

    @Qualifier is used to reference a bean by its name or id. Since it can't find an xml entry that has a name or id of 'small' it tries to match by type, of which it found two instances of Size.

    The following would work:

      <bean id="small" class="com.tests.test2.Size">      
        <property name="height" value="2"/>
        <property name="width" value="1"/>
    </bean>
    

    Though it appears you would like to treat instances of Size as pre-configured beans. If that were the case you could declare instances of Dog in your xml file and refer to Size beans ... something like this:

      <bean id="rex" class="com.tests.test2.SimpleDog">
         <property name="name" value="Puppy" />
         <property name="size" ref="size1"/>
      </bean>
    
    0 讨论(0)
提交回复
热议问题