简体   繁体   English

依赖注入如何在没有构造函数的情况下实例化一个类?

[英]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: 这是执行顺序:

  1. instantiate textEditor , this has default constructor that is not visible in the code public TextEditor () 实例化textEditor ,它具有默认构造函数,该构造函数在代码public TextEditor ()不可见
  2. the new instance is set in a pool of available objects with reference textEditor 新实例是在带有引用textEditor的可用对象池中设置的
  3. instantiate spellChecker and add to the pool of available object with relevant reference/label 实例化spellChecker并添加具有相关参考/标签的可用对象池
  4. all @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.

相关问题 Spring依赖项注入如何在带有@Component注释但没有构造函数注释的类中工作? - How does Spring dependency injection work inside a class that is annotated @Component but any without constructor annotation? Spring依赖注入:如何基于系统属性实例化类? - Spring Dependency Injection: How to Instantiate Class based on system property? Spring依赖注入到具有构造函数的类 - Spring dependency injection to a class with a constructor JUnit:如何通过构造函数创建具有依赖注入的测试类 - JUnit: How to create a test class with dependency injection via the constructor 如何获取在其构造函数中使用依赖注入的类的实例 - How to get instance of class that uses dependency injection in its constructor 如何在抽象 class 中使用构造函数注入注入特定依赖项? - How to inject specific dependency using constructor injection in an abstract class? 基于构造函数的依赖注入如何影响不变性? - How does constructor-based dependency injection affects immutability? Spring Boot 中没有构造函数的依赖注入 - Dependency Injection without constructor in Spring Boot 在具有参数化构造函数的类上使用依赖注入 - Using dependency injection on class having parameterized constructor Byte Buddy实例化没有构造函数参数的类 - Byte Buddy instantiate class without parameters for constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM