gpt4 book ai didi

spring - 自动连线……不止一个?

转载 作者:行者123 更新时间:2023-12-03 22:56:27 25 4
gpt4 key购买 nike

我在用

<context:component-scan base-package="com.package.dao"/> (Spring 3)

并在我的 Controller 中使用 @Autowired 将自动检索我需要的 DAO。那是完美的并且有效。
问题是当我需要多个 DAO 管理器时。
我能怎么做?
我试过 @限定符但我不确定道经理的名字。类名不起作用

有人可以帮助我吗?

谢谢!

最佳答案

spring documentation :

@Autowired is fundamentally about type-driven injection with optional semantic qualifiers. This means that qualifier values, even when using the bean name fallback, always have narrowing semantics within the set of type matches; they do not semantically express a reference to a unique bean id

...

If you intend to express annotation-driven injection by name, do not primarily use @Autowired - even if is technically capable of referring to a bean name through @Qualifier values. Instead, prefer the JSR-250 @Resource annotation which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process.


改用@Resource,见 here .

关于spring - 自动连线……不止一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6091474/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com