2

我了解到,当IoC容器初始化时,它会创建实例并注入依赖关系。IoC如何为bean创建实例

它是如何创建对象的?它是使用新操作符创建的吗?

回答

2

在Java中,实例化对象的唯一方法是调用构造函数。 您可以使用new运算符或反射来调用构造函数。

春天使用反射instanciate一个对象。

0

好的这里:

Spring IoC容器管理一个或多个bean。这些bean是使用已经提供给容器的配置元数据中定义的指令(通常以XML定义的形式)创建的。 在容器内部,这些bean定义表示为的BeanDefinition对象,其中包含(以及其他信息)以下元数据:

1. A package-qualified class name: this is normally the actual implementation class of the bean being defined. However, if the bean is to be instantiated by invoking a static factory method instead of using a normal constructor, this will actually be the class name of the factory class. 
    2. Bean behavioral configuration elements, which state how the bean should behave in the container (prototype or singleton, autowiring mode, initialization and destruction callbacks, and so forth). 
    3. Constructor arguments and property values to set in the newly created bean. An example would be the number of connections to use in a bean that manages a connection pool (either specified as a property or as a constructor argument), or the pool size limit. 
    4. Other beans which are needed for the bean to do its work, that is collaborators (also called dependencies). 

所以,你看,容器本身对如何创建豆类无柄。在java中创建对象可以用new运算符轻松完成。

0

1如果在配置元数据中定义了非私有构造函数并声明了相同的类,则使用反射对其进行实例化。 getDeclaredConstructor() of a classAPI

  1. 一些类正在使用如果在元数据中所定义的静态或非静态工厂方法实例化。

请阅读部分4.3.2 spring documentation

实例化bean
相关问题