夯实spring(十二):bean延迟初始化

bean初始化时有两种方式:

  • 实时初始化
  • 延迟初始化

1,实时初始化

在容器启动过程中被创建组装好的bean,称为实时初始化的bean,spring中默认定义的bean都是实时初始化的bean,这些bean默认都是单例的,在容器启动过程中会被创建好,然后放在spring容器中以供使用。
实时初始化可以:
更早发下bean定义的错误:实时初始化的bean如果定义有问题,会在容器启动过程中会抛出异常,让开发者快速发现问题
查找bean更快:容器启动完毕之后,实时初始化的bean已经完全创建好了,此时被缓存在spring容器中,当我们需要使用的时候,容器直接返回就可以了,速度是非常快的

案例

public class Car {
    private String name;
    private Double price;


    public Car() {
        System.out.println("我是实时初始化的Car!");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<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
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
    <bean id="car1" class="com.chen.Car"/>
</beans>

测试输出

public class Main {
    public static void main(String[] args) {
        System.out.println("容器启动中...");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean.xml");
        System.out.println("容器启动完毕...");
    }
}
容器启动中...
我是实时初始化的Car!
容器启动完毕...

可以看出car1这个bean是在容器启动过程中被创建好的。

2,延迟初始化

实时初始化的bean都会在容器启动过程中创建好,如果程序中定义的bean非常多,并且有些bean创建的过程中比较耗时的时候,会导致系统消耗的资源比较多,并且会让整个启动时间比较长,使用spring开发的系统比较大的时候,整个系统启动耗时是比较长的,基本上多数时间都是在创建和组装bean。
spring对这些问题也提供了解决方案:bean延迟初始化

延迟初始化,就是和实时初始化刚好相反,延迟初始化的bean在容器启动过程中不会创建,而是需要使用的时候才会去创建

bean什么时候会被使用?

  • 被其他bean作为依赖进行注入的时候,比如通过property元素的ref属性进行引用,通过构造器注入、通过set注入、通过自动注入,这些都会导致被依赖bean的创建。
  • 自己写代码向容器中查找bean的时候,如调用容器的getBean方法获取bean。

在bean定义的时候通过lazy-init属性来配置bean是否是延迟加载,true:延迟初始化,false:实时初始化

<bean lazy-init="是否是延迟初始化" />

案例1
把上面的bean.xml修改成:

<?xml version="1.0" encoding="UTF-8"?>
<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
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
    <bean id="car1" class="com.chen.Car" lazy-init="true"/>
</beans>

测试输出

public class Main {
    public static void main(String[] args) {
        System.out.println("容器启动中...");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean.xml"); //启动spring容器
        System.out.println("容器启动完毕...");
        System.out.println("从容器中开始查找car1");
        Car car = context.getBean(Car.class);
        System.out.println("car1Bean:" + car);
    }
}
容器启动中...
容器启动完毕...
从容器中开始查找car1
我是延时初始化的Car!
car1Bean:Car{name='null', price=null}}

可以看出来,car1在容器启动过程中并没有创建,当我们调用context.getBean方法的时候,car1才被创建的。

案例2
案例1这种方式是我们主动从容器中获取bean的时候,延迟初始化的bean才被容器创建的,下面来看一下当延迟初始化的bean被其他实时初始化的bean依赖的时候,是什么时候创建的。

public class Garage {
    private String addr;
    private String name;

    public Garage() {
        System.out.println("我是被依赖的车库");
    }


}
public class Car {
    private String name;
    private Double price;
 	private Garage garage;

    public Car() {
        System.out.println("我是实时时初始化的Car!");
    }

    public Garage getGarage() {
        return garage;
    }

    public void setGarage(Garage garage) {
        System.out.println("Car.setGarage()");
        this.garage = garage;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<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
    http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
    <bean id="car1" class="com.chen.Car">
        <property name="garage" ref="garage"/>
    </bean>

    <bean id="garage" class="com.chen.Garage" lazy-init="true"></bean>

</beans>

上面定义了2个bean:

garage:lazy-init为true,说明这个bean是延迟创建的

car1:通过property元素来注入garage,car1中没有指定lazy-init,默认为false,表示是实时创建的bean,会在容器创建过程中被初始化。

测试输出

public class Main {
    public static void main(String[] args) {
        System.out.println("容器启动中...");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:bean.xml"); //启动spring容器
        System.out.println("容器启动完毕...");
    }
}
容器启动中...
我是实时时初始化的Car!
我是被依赖的车库
Car.setGarage()
容器启动完毕...

可以看到,xml中定义的2个bean都在容器启动过程中被创建好了。 garage的lazy-init为true,也在容器启动过程中被创建了,是因为car1为实时初始化的bean,而这个bean在创建过程中需要用到garage,此时容器会去查找garage这个bean,然后会进行初始化,所以这2个bean都在容器启动过程中被创建的。

总结

延迟初始化的bean无法在程序启动过程中迅速发现bean定义的问题,第一次获取的时候可能耗时会比较长。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值