什么是懒加载
为什么使用懒加载
在Web应用程序中,系统的瓶颈常在于系统的响应速度。如果系统响应速度过慢,用户就会出现埋怨情绪,系统的价值也因此会大打折扣。因此,提高系统响应速度,是非常重要的。
Web应用程序做的最多就是和后台数据库交互,而查询数据库是种非常耗时的过程。当数据库里记录过多时,查询优化更显得尤为重要。为了解决这种问题,有人提出了缓存的概念。缓存就是将用户频繁使用的数据放在内存中以便快速访问。在用户执行一次查询操作后,查询的记录会放在缓存中。当用户再次查询时,系统会首先从缓存中读取,如果缓存中没有,再查询数据库。缓存技术在一定程度上提升了系统性能,但是当数据量过大时,缓存就不太合适了。因为内存容量有限,把过多的数据放在内存中,会影响电脑性能。而另一种技术,懒加载可以解决这种问题。

所谓懒加载(lazy)就是延时加载,延迟加载。

在spring的IOC容器中,可以通过设置来设置是否为懒加载模式,懒加载的意思就是说是否在spring容器加载的时候将bean加载到容器中。在没有设置的情况下,默认是false的,就是说不使用懒加载模式。

至于为什么要用懒加载呢,就是当我们要访问的数据量过大时,明显用缓存不太合适,
因为内存容量有限 ,为了减少并发量,减少系统资源的消耗,
我们让数据在需要的时候才进行加载,这时我们就用到了懒加载。

默认情况下我们都是如下配置方式

package com.spring.minor;

public class Student {
	
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void sayHello() {
		System.out.println("Hello,"+name);
	}	
}

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.xsd">
	<!-- 配置bean -->
	<!-- id属性:用于获取IOC容器中的Bean对象,具有唯一性 -->
	<!-- class属性:指定Bean对象由哪个类创建,为类的全类名 -->
	<bean name="helloWorld" class="com.spring.minor.Student">
		<!-- 为name属性赋值 -->
		<property name="name" value="newbie "></property>
	</bean>
</beans>

java文件

package com.spring.minor;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Work {
	public static void main(String[] args) {
		//创建Spring IOC容器对象
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring_config.xml");
		//从IOC容器中获取Bean对象
		Student helloWorld = (Student) applicationContext.getBean("helloWorld");
		//调用sayHello方法
		helloWorld.sayHello();
		//关闭IOC容器
		applicationContext.close();
	}
}

实现懒加载有两种方式
a、在beans标签中添加default-lazy-init=“true”,则在该标签中配置的所有bean将实现懒加载(即不创建对象,在使用时才创建);
b、在对应的bean标签中添加lazy-init=“true”,则该bean将实现懒加载,该属性没有继承性;
注意:bean标签中设置lazy-init的优先级高于在beans标签中设置default-lazy-init
我们在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.xsd">

<beans >
	<bean id="helloworld" class="com.spring.minor.HelloWorld" lazy-init="true">
	</bean>
</beans>
</beans>

在java文件中验证

package com.spring.minor;

import org.springframework.context.support.ClassPathXmlApplicationContext;

class HelloWorld {

	public HelloWorld() {
		System.out.println("HelloWorld!");
	}
}
public class StuTest {

	public static void main(String[] args) {
		//创建Spring IOC容器对象
		ClassPathXmlApplicationContext applicationContext =new ClassPathXmlApplicationContext("bean.xml");
		System.out.println("~~~~~~~~~~~");
		applicationContext.getBean("helloworld");
	}
}

控制台输出

七月 10, 2019 11:21:45 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@817b38: startup date [Wed Jul 10 11:21:45 CST 2019]; root of context hierarchy
七月 10, 2019 11:21:45 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
~~~~~~~~~~~
HelloWorld!

HelloWorld是在getBean时输出,即获取对象时输出。