开发者

Constructor arguments in autowiring sources

What exactly causes this?

org.springframework.beans.factory.NoSuchBeanDefinitionException: \
No unique bean of type [fi.utu.keycard.business.KeyCardManager] \
is defined: expected single matching bean but found 2: \
[dataBaseTarget, database]

// etc. (rest of Stack Trace is irrelevant)

What I need is autowiring 3 things: validator, ldap connection and database connection.

I call it:

@Controller
Controller(KeyCardManager database,
           LdapPersonDao perso开发者_如何学运维nManager,
           GiveFormValidator validator)

The error seems to cause by another bean, if I change the order of these parameters. I have no signing-in, so I dont have UserDetails or so.


The fix is probably something like this:

public Controller(
    @Qualifier("beanQualifier") KeyCardManager database,
    LdapPersonDao personManager,
    GiveFormValidator validator
)

Since there are apparently two beans of type KeyCardManager in your application context, you need to tell the context which one to wire.

Unfortunately the @Qualifier mechanism doesn't work with bean names, you must either annotate the actual bean with a corresponding @Qualifier or add a <qualifier> element to the XML bean definition.

The @Resource annotation works with bean names, but it doesn't support Constructor parameters (that's not Spring's fault, it's a JSR-250 standard annotation with @Target({TYPE, FIELD, METHOD}))

Reference:

  • Autowiring Collaborators
  • Fine-tuning annotation-based autowiring with qualifiers

Troubleshooting

If you don't know why there are two beans of the same type in the context, first of all navigate to the bean interface (I assume KeyCardManager is an interface, if not, do the same thing for the class anyway) and if you use Eclipse select Navigate > Open Type Hierarchy. If you find more than one concrete class that inherits from KeyCardManager (including KeyCardManager itself), then there's probably your problem.

If that is not the case, you probably have two beans of the same type in your application context. One way that can happen is when you define a bean through both XML and classpath scanning. I.e. if you have this line in your XML:

<context:component-scan base-package="org.example"/>

Make sure you don't manually wire any beans from the org.example package (or you will have double beans, which can lead to the problem you have).


org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [fi.utu.keycard.business.KeyCardManager] is defined: expected single matching bean but found 2: [dataBaseTarget, database]

It seems you are autowiring by class type. but there are multiple bean available in the context with same class. which are dataBase & dataBaseTarget

byType

Allows a property to be autowired if there is exactly one bean of the property type in the container. If there is more than one, a fatal exception is thrown, and this indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens; the property is not set. If this is not desirable, setting the dependency-check="objects" attribute value specifies that an error should be thrown in this case.

  • Document
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜