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