spring中的懒加载详细解读
作者:RayBreslin
一、懒加载介绍
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>总结
Spring 懒加载是一种优化应用程序性能和资源利用率的机制。通过延迟加载对象的创建和初始化,可以减少应用程序启动时的负载和内存占用。
懒加载可以通过使用@Lazy注解、配置文件中的lazy-init属性或编程方式来实现。在处理大量对象或对象初始化较为耗时的情况下,懒加载可以显著提高应用程序的响应速度和效率。
然而,需要注意的是,懒加载可能会导致一些副作用,如延迟了对象的初始化和依赖注入,可能会在运行时出现错误。
因此,在使用懒加载时,需要仔细考虑对象的依赖关系和初始化顺序,以确保应用程序的正确性和稳定性。
到此这篇关于spring中的懒加载详细解读的文章就介绍到这了,更多相关spring懒加载内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
