自动装配源中的构造函数参数

发布于 2024-10-16 06:21:03 字数 586 浏览 10 评论 0原文

到底是什么原因造成的呢?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

日暮斜阳 2024-10-23 06:21:03

修复方法可能是这样的:

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

由于您的应用程序上下文中显然有两个 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 中有此行​​:

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

请确保您没有从 org.example 包中手动连接任何 bean(否则您将拥有双 bean,这可能会导致您遇到的问题) 。

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:


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).

埋葬我深情 2024-10-23 06:21:03

org.springframework.beans.factory.NoSuchBeanDefinitionException:未定义类型为 [fi.utu.keycard.business.KeyCardManager] 的唯一 bean:期望单个匹配 bean,但找到 2:[dataBaseTarget,数据库]

看来您正在按类类型自动装配。但在同一类的上下文中有多个可用的 bean。它们是dataBase & dataBaseTarget

byType

允许自动装配属性,如果
正好有一颗豆子
容器中的属性类型。如果
不止一个,一个致命的
抛出异常,并且这
表示您不能使用 byType
该 bean 的自动装配。如果有
没有匹配的bean,什么也不会发生;
该属性未设置。如果这是
不理想,设置
dependency-check="objects" 属性
value 指定错误应该
在这种情况下被抛出。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文