Spring中IOC创建对象的两种方式和创建时机

偏执的太偏执、 2023-01-18 09:17 95阅读 0赞

最近在学习Spring相关知识,官网文档如下
Spring Core Technologies: https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-constructor-injection.

一、创建方式

方式1:通过无参构造方法来创建

  1. package com.zhang.pojo;
  2. public class User {
  3. private String name;
  4. public User(){
  5. System.out.println("This is a no args method!");
  6. }
  7. public void show(){
  8. System.out.println("This is a show method!");
  9. }
  10. public String getName() {
  11. return name;
  12. }
  13. public void setName(String name) {
  14. this.name = name;
  15. }
  16. }

beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
  3. <bean id="User" class="com.zhang.pojo.User">
  4. <property name="name" value="张三丰"></property>
  5. </bean>
  6. </beans>

MyTest

  1. import com.zhang.pojo.User;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class MyTest {
  6. @Test
  7. public void test1(){
  8. ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
  9. User user = (User)ac.getBean("User");
  10. user.show();
  11. }
  12. }

结果可以发现,在调用show方法之前,User对象已经通过无参构造初始化了!执行结果如下:
在这里插入图片描述
方式2:通过有参构造方法来创建
User

  1. package com.zhang.pojo;
  2. public class User {
  3. private String name;
  4. public User(String name){
  5. //System.out.println("This is a no args method!");
  6. this.name = name;
  7. }
  8. public void show(){
  9. System.out.println("This is a show method!");
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. }

这里是官网详细配置使用方式
在这里插入图片描述
beans.xml

  1. <!--第一种:下标赋值-->
  2. <bean id="user" class="com.zhang.pojo.User">
  3. <constructor-arg index="0" value="张三丰"></constructor-arg>
  4. </bean>
  5. <!--第二种:类型赋值。 如果两个参数都是String类型,则没有办法使用。 所以不建议使用第二种 -->
  6. <bean id="user" class="com.zhang.pojo.User">
  7. <constructor-arg type="java.lang.String" value="张三丰"/>
  8. </bean>
  9. <!--第三种:直接通过参数名实现-->
  10. <bean id="user" class="com.zhang.pojo.User">
  11. <constructor-arg name="name" value="张三丰"></constructor-arg>
  12. </bean>

二、什么时候创建对象?

创建时机
1:默认的在Spring容器启动的时候
2:在getBean的时候创建对象(就是拿到配置文件中的标识符的时候创建对象)

在Spring的配置文件bean中有一个属性lazy-init=“default/true/false”
(1)如果lazy-init为”default/false”在启动spring容器时创建对象
(2)如果lazy-init为”true”,在context.getBean时才要创建对象

意义
(1)在启动spring容器的时候,检查spring容器配置文件的正确性,如果spring容器不能正常启动,整个tomcat就不能正常启动。但是这样的缺点是把一些bean过早的放在了内存中,如果有数据,则对内存来是一个消耗,但是可以过早发现错误。
(2)getBean时创建对象可以减少内存的消耗,但是不容易发现错误。

注意
如果一个bean的scope属性为scope=”pototype”时,即使设置了lazy-init=“false”,容器启动时不实例化bean,而是调用getBean方法是实例化的。

三、IOC容器创建对象过程

(1)先通过createBeanFactory创建出一个bean工厂(利用顶级接口BeanFactory下的DefaultListableBeanFactory接口)。
(2)循环创建对象,由于容器中bean默认都是单例,则优先通过getBean,doGetBean从容器中查找。
(3)如果找不到则通过createBean,doCreateBean方法,以反射形式创建对象。一般情况下使用的是无参构造方法(getDeclaredConstructor,newInscance)。
(4)进行对象的属性填充(populateBean)。
(5)进行其它初始化操作(initializingBean)。

参考文章
https://blog.csdn.net/z19950712/article/details/112335806

发表评论

表情:
评论列表 (有 0 条评论,95人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring--三对象创建方式

    第一种方式:空参构造创建对象 最基本的对象创建方式,只需要有一个无参构造函数和字段的set方法。本质上就是使用无参构造器创建对象,然后使用set方法为字段赋值。 Us