自动装配源中的构造函数参数
到底是什么原因造成的呢?
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)
我需要的是自动装配 3 个东西:验证器、ldap 连接和数据库连接。
我称之为:
@Controller
Controller(KeyCardManager database,
LdapPersonDao personManager,
GiveFormValidator validator)
如果我更改这些参数的顺序,该错误似乎是由另一个 bean 引起的。我没有登录,所以我没有 UserDetails 等。
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 personManager,
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
修复方法可能是这样的:
由于您的应用程序上下文中显然有两个
KeyCardManager
类型的 bean,因此您需要告诉上下文要连接哪一个。不幸的是,@Qualifier 机制不适用于 bean 名称,您必须使用相应的 @Qualifier 注释实际 bean 或添加< XML bean 定义的 /code> 元素。
@Resource
注解适用于 bean 名称,但它不支持构造函数参数(这不是 Spring 的错,而是 JSR-250 标准注释,带有@Target({TYPE, FIELD, METHOD})
)参考:
使用限定符自动装配
故障排除
如果您不知道为什么上下文中有两个相同类型的bean,首先导航到bean接口(我假设
KeyCardManager
是一个接口,如果没有,则对该类执行相同的操作),如果使用 Eclipse,请选择Navigate >打开类型层次结构
。如果您发现多个继承自KeyCardManager
的具体类(包括KeyCardManager
本身),那么可能就是您的问题。如果情况并非如此,则您的应用程序上下文中可能有两个相同类型的 bean。可能发生的一种情况是通过 XML 和 类路径扫描。即,如果您的 XML 中有此行:
请确保您没有从 org.example 包中手动连接任何 bean(否则您将拥有双 bean,这可能会导致您遇到的问题) 。
The fix is probably something like this:
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 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 selectNavigate > Open Type Hierarchy
. If you find more than one concrete class that inherits fromKeyCardManager
(includingKeyCardManager
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:
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).看来您正在按类类型自动装配。但在同一类的上下文中有多个可用的 bean。它们是
dataBase
&dataBaseTarget
byType
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