[英]How does dependency injection instantiate a class without constructor?
I followed the following example of dependency injection: http://www.tutorialspoint.com/spring/spring_autowired_annotation.htm 我遵循以下依赖项注入示例: http : //www.tutorialspoint.com/spring/spring_autowired_annotation.htm
For example the TextEditor class (from the above link): 例如,TextEditor类(来自上面的链接):
public class TextEditor {
private SpellChecker spellChecker;
@Autowired
public void setSpellChecker( SpellChecker spellChecker ){
this.spellChecker = spellChecker;
}
public SpellChecker getSpellChecker( ) {
return spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
How can these dependencies/classes be instantiated, while they don't have any constructor? 没有任何构造函数时,如何实例化这些依赖项/类?
Is Java simply making an object of that type, that is empty? Java是否只是在创建该类型的对象,该对象为空? Like an empty parameter constructor without any code?
就像没有任何代码的空参数构造函数一样?
Thanks for making this more clear! 感谢您让这一点变得更清楚!
Unless specified otherwise, every Java class has the default constructor. 除非另有说明,否则每个Java类都有默认的构造函数。 So here, you have a default
public TextEditor()
constructor, even though you haven't coded for it. 因此,即使没有进行编码,这里也有一个默认的
public TextEditor()
构造函数。 (You could code it if you needed to change its visibility from public, declare a thrown exception, etc.) (如果需要更改其公开范围,声明抛出异常等,则可以对其进行编码。)
So yes, Spring calls this default constructor - then calls the setSpellChecker
method (as annotated, and through reflection) to populate it. 因此,是的,Spring调用了此默认构造函数-然后调用
setSpellChecker
方法(作为注释,并通过反射)来填充它。
If no constructor is defined, a class can be instantiated via the no-argument default constructor. 如果未定义构造函数,则可以通过无参数默认构造函数实例化一个类。
So, the framework calls that constructor (supposedly using reflection) and then uses the set method to set the one field of the freshly created class. 因此,该框架调用该构造函数(假定使用反射),然后使用set方法设置新创建的类的一个字段。
The example above is using Spring annotations and Spring context file and those are the main and most important parts of the project, considering the DI. 上面的示例使用Spring批注和Spring上下文文件,考虑到DI,它们是项目的主要和最重要的部分。
So in the context file you have following line: 因此,在上下文文件中,您具有以下行:
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
this defines a class with reference spellChecker
that mapps to a class com.tutorialspoint.SpellChecker
and once the compiler find such property in a method that is marked as @Autowired
on instantiation of the object it injects/sets the relevant version of the required dependency. 这个定义与引用的类
spellChecker
该MAPPS一类com.tutorialspoint.SpellChecker
并且一旦编译器发现在作为标记的方法,例如属性@Autowired
上它注入/设置所需的相关的相关版本的对象的实例化。
In cases where a property doesn't match a reference tag in the applicationContext.xml file Spring is trying to map the type eg property with name mySpecialSpellChecker
which has type of com.tutorialspoint.SpellChecker
still will be mapped to bean with id="spellChecker"
if there are more than one of same type Spring won't instantiate your object and you might get compile time error as Spring can't know which version of the two (or more) is the correct one so this requires developer input. 如果属性与applicationContext.xml文件中的引用标记不匹配,Spring会尝试映射类型,例如名称为
mySpecialSpellChecker
属性,其类型为com.tutorialspoint.SpellChecker
仍将映射到id="spellChecker"
com.tutorialspoint.SpellChecker
bean id="spellChecker"
如果有多个相同类型的Spring不会实例化您的对象,并且您可能会遇到编译时错误,因为Spring无法知道这两个(或多个)的哪个版本是正确的,因此这需要开发人员输入。
This is the order of execution: 这是执行顺序:
textEditor
, this has default constructor that is not visible in the code public TextEditor ()
textEditor
,它具有默认构造函数,该构造函数在代码public TextEditor ()
不可见 textEditor
textEditor
的可用对象池中设置的 spellChecker
and add to the pool of available object with relevant reference/label spellChecker
并添加具有相关参考/标签的可用对象池 @Autowired
properties/methods are set/called with relevant objects in this case Spring calls: setSpellChecker(spellChecker)
@Autowired
属性/方法/ Spring调用: setSpellChecker(spellChecker)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.