目录
一、懒加载介绍
1.概念
Spring容器会在创建容器时提前初始化Singleton作用域的bean,即在创建环境ApplicationContext的时候,单例作用域的Bean就会被实例化。注意:如果是prototype作用域的bean,则其是在调用该bean的时候创建的(已经验证)
但是如果Bean被标注了lazy-init="true",则该Bean只有在其被需要的时候才会被初始化。
2.作用
如果某个Bean再程序运行周期中都可能不会被适用,那么可以设定该Bean为懒加载。优势是尽量节省了服务器的资源,缺点是可能会导致某个相应的时间增加。
二、环境
1.pom.xml文件
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
2.Spring.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"
default-autowire="byName"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
</beans>
三、代码实现
1.创建bean
package com.spring.ioc;
/**
* Created by Administrator on 2019/10/26.
*/
public class Bean1 {
public Bean1() {
System.out.println("Bean1 has been created ");
}
}
2.交给spring管理:对于单例模式,非懒加载
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p ="http://www.springframework.org/schema/p"
default-autowire="byName"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean class="com.spring.ioc.Bean1" id="bean1" />
</beans>
-》测试
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2019/10/26.
*/
public class testcode {
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
System.out.println("context已经被创建");
Bean1 bean1=context.getBean("bean1",Bean1.class);
System.out.println("bean1 = " + bean1);
}
}
结果,Bean在加载spring环境的时候就已经被加载了
Bean1 has been created
context已经被创建
bean1 = com.spring.ioc.Bean1@7d0587f1
3.交给spring管理:对于单例模式,懒加载
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p ="http://www.springframework.org/schema/p"
default-autowire="byName"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!--<bean class="com.spring.ioc.Bean1" id="bean1" />-->
<bean class="com.spring.ioc.Bean1" id="bean1" lazy-init="true"/>
</beans>
-》测试
package com.spring.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by Administrator on 2019/10/26.
*/
public class testcode {
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
System.out.println("context已经被创建");
Bean1 bean1=context.getBean("bean1",Bean1.class);
System.out.println("bean1 = " + bean1);
}
}
结果,Bean是在调用的时候,再加载创建的。
context已经被创建
Bean1 has been created
4.将spring.xml下所有bean统一都设置为懒加载
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p ="http://www.springframework.org/schema/p"
default-autowire="byName"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"
default-lazy-init="true"
>
<!--<bean class="com.spring.ioc.Bean1" id="bean1" />-->
<bean class="com.spring.ioc.Bean1" id="bean1" lazy-init="true"/>
</beans>
四、总结
1.上传代码到github~