简体   繁体   English

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

[英]Can't find where @Resource object is instantiated in Spring xml

I'm trying to find out where a java object is being instantiated. 我试图找出正在实例化Java对象的位置。 I have a Spring controller and in the controller is a dataService field: 我有一个Spring控制器,并且在控制器中是dataService字段:

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

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

( DataService is an interface. The class that implements this service is DataServiceImpl ). DataService是一个接口。实现此服务的类是DataServiceImpl )。

From my understanding, when there is @Resource above a field, it indicates that the object is instantiated using a spring bean which is usually defined in an xml file. 据我了解,当字段上方有@Resource时,它表示该对象是使用通常在xml文件中定义的spring bean实例化的。 However, i've searched my workspace for this object's name (and many variants) and looked in all the usual spring bean locations in the project but I cant find any mention of this anywhere. 但是,我已经在工作区中搜索了该对象的名称(以及许多变体),并查看了项目中所有通常的spring bean位置,但是在任何地方都找不到任何提及。 Does anyone know what could be happening here? 有人知道这里会发生什么吗?

From spring framework documentation, 从Spring框架文档中,

@Resource takes a name attribute, and by default Spring interprets that value as the bean name to be injected. @Resource具有名称属性,默认情况下,Spring将该值解释为要注入的Bean名称。 If no name is specified explicitly, the default name is derived from the field name or setter method. 如果未明确指定名称,则默认名称是从字段名称或setter方法派生的。 In case of a field, it takes the field name; 如果是字段,则以字段名称为准; in case of a setter method, it takes the bean property name 如果使用setter方法,则使用bean属性名称

In case name attribute is not provided then spring fall backs to autowiring, which will search and inject for bean of matching type.So DataServiceImpl may be defined in spring bean xml file or annotated using @Component . 如果没有提供name属性,那么spring将退回到自动装配,它将搜索并注入匹配类型的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. 在没有指定显式名称且与@Autowired类似的情况下,@ Resource使用的特殊情况是,@ Resource查找主类型匹配而不是特定的命名bean,并解析众所周知的可解决依赖项。

More on @Resource. 有关@Resource的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM