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

标签 spring spring-mvc jakarta-ee

我在用

<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/

相关文章:

java - 如何仅扫描加载的Spring中的特定文件夹?

java - Spring MVC - 找不到页面

Spring 和 thymeleaf : changing locale and stay on the current page

java - Spring Boot 2 测试存储库因 SQL 语法错误而失败

spring - Spring Boot 中 application.properties 可用的属性列表?

java - 如何编写具有可变数量的表单字段和文件的 Spring MVC Controller 方法?

java - 带有 Spring 4 依赖注入(inject)的 Jersey

jakarta-ee - JAX-RS (Apache CXF) 通过 setter 进行上下文注入(inject)

java - 如何使用java读取pdf文件?

java - hibernate查询在java spring中不起作用