java - 找不到@Resource对象在Spring xml中实例化的位置

标签 java xml spring spring-mvc controller

我试图找出 java 对象被实例化的位置。我有一个 Spring Controller , Controller 中有一个 dataService 字段:

@SuppressWarnings("restriction")
@Controller
public class DataController {

   /**
    * Service for which to delegate retrieval of data
    */
    @Resource
    private DataService dataService;

(DataService是一个接口(interface)。实现此服务的类是DataServiceImpl)。

据我了解,当字段上方有@Resource时,表示该对象是使用spring bean实例化的,通常在xml文件中定义。然而,我在我的工作空间中搜索了该对象的名称(以及许多变体),并查看了项目中所有常见的 spring bean 位置,但我在任何地方都找不到任何提及此内容的地方。有谁知道这里会发生什么?

最佳答案

来自 spring 框架文档,

@Resource takes a name attribute, and by default Spring interprets that value as the bean name to be injected. If no name is specified explicitly, the default name is derived from the field name or setter method. In case of a field, it takes the field name; in case of a setter method, it takes the bean property name

如果未提供 name 属性,则 spring 会回退到 Autowiring ,这将搜索并注入(inject)匹配类型的 bean。所以 DataServiceImpl可以在 spring bean xml 文件中定义或使用 @Component 进行注释。

In the exclusive case of @Resource usage with no explicit name specified, and similar to @Autowired, @Resource finds a primary type match instead of a specific named bean and resolves wellknown resolvable dependencies.

More on @Resource.

关于java - 找不到@Resource对象在Spring xml中实例化的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42876191/

相关文章:

java - 从 JTable 添加/删除行后如何添加和减去总和值

java - Spring @ConditionalOnBean 结合@Component

php - 在 Magento 中嵌套不同类型的 block

java - Runtime.exec() 的安全问题

java - 实现异步任务: An error occured while executing doInBackground() throws Runtime exception

python - 使用 Python 保存来自 GET 调用的 XML 响应

xml - XSLT 和 XSLFO 命名空间

mysql - 查询速度极慢——使用 google sql cloud

java - 使用外键查找所有约束

java - 在 Spring Boot 中使用系统变量