为什么需要懒加载?

我们知道,在 SpringBoot 应用程序启动的时候,会实例化一些对象加入到 IOC 容器里边,这个过程是非常耗时的,那我们想要减少这个耗时的过程就需要 @Lazy 注解

对象加入容器的时机

如下代码

package com.startdusk.forgot.service;import org.springframework.stereotype.Component;@Service
public class LazyService {public LazyService() {System.out.println("LazyService add to ioc container");}public void print() {System.out.println("lazy");}
}

由于我们在 LazyService 中打上了 @Service 注解,那么当程序启动的时候,LazyService 就会被立即实例化,我们在 LazyService 写了个无参的构造函数来测试,启动程序,会打印出

那么说明,在打上了 @Service 注解后,在程序启动的时候就立即实例化对象加入到容器

使用 @Lazy 延迟加入容器

那我们来改写下代码,延迟加入容器:

package com.startdusk.forgot.service;import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;@Service
@Lazy
public class LazyService {public LazyService() {System.out.println("LazyService add to ioc container");}public void print() {System.out.println("lazy");}
}

再来运行一下程序:

看到运行的结果,并没有打印 “LazyService add to ioc container” 也就没有实例化 LazyService ,说明 @Lazy 注解起作用了,我们延迟了对象加入容器的时机

一个奇怪的现象

那么在实际情况中,我们会在 controller 中调用这个 service,即在 controller 中注入这个对象:

package com.startdusk.forgot.api.v1;import com.startdusk.forgot.service.LazyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/v1/lazy")
public class LazyController {@Autowiredprivate LazyService lazyService;@GetMapping("/test")public String test() {lazyService.print();return "Hello, world";}
}

保持 LazyService 中的 @Lazy,我们运行程序

发现 LazyService 居然被实例化了,难道打上 @Lazy 注解的类被注入之后 @Lazy 就不起作用了吗?我们来看下这个 LazyController 的代码,这里其实我们也把 LazyController 加入了容器(即打上了 @RestController 注解),在 SpringBoot 中,加入 IOC 容器就必须实例化对象,而实例化对象就要求,这个对象的里面的成员变量,方法都要被初始化,就包括 lazyService 这个要被注入的变量,即使 LazyService 打上了 @Lazy 注解。

解决这个问题,我们也需要在 LazyController 打上 @Lazy 注解,才能延迟加入 IOC 容器

package com.startdusk.forgot.api.v1;import com.startdusk.forgot.service.LazyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/v1/lazy")
@Lazy
public class LazyController {@Autowiredprivate LazyService lazyService;@GetMapping("/test")public String test() {lazyService.print();return "Hello, world";}
}

@Lazy 会延迟到什么时候

会延迟到对象被调用的时候。如,运行程序,访问 localhost:8080/v1/lazy/test 就是访问 test 这个方法,那么就会调用 lazyService.print() 就会打印出:

我们可以看到,当被注入的对象被调用的时候,才会把对象加入 IOC 容器,然后注入对象

使用 @Lazy 的缺点

我们来调整下代码:
LazyService 注释掉 @Service 和 @Lazy

package com.startdusk.forgot.service;import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;// @Service
// @Lazy
public class LazyService {public LazyService() {System.out.println("LazyService add to ioc container");}public void print() {System.out.println("lazy");}
}

那么运行代码,再来访问下 localhost:8080/v1/lazy/test
,那么,程序会给我们抛一个错误:

Error creating bean with name 'lazyController': Unsatisfied dependency expressed through field 'lazyService';......

就是找不到这个这个注入的对象。那么这个报错是在程序运行中报错,而不是程序启动的时候就报错了。那么我们在 LazyController 中去掉 @Lazy

package com.startdusk.forgot.api.v1;import com.startdusk.forgot.service.LazyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/v1/lazy")
public class LazyController {@Autowiredprivate LazyService lazyService;@GetMapping("/test")public String test() {lazyService.print();return "Hello, world";}
}

启动程序,那么就会在程序启动的时候直接报错,不让你运行

Field lazyService in com.startdusk.forgot.api.v1.LazyController required a bean of type 'com.startdusk.forgot.service.LazyService' that could not be found.

SpringBoot:使用 @Lazy 注解懒加载相关推荐

  1. react.lazy 路由懒加载_React lazy/Suspense使用及源码解析

    React v16.6.0已经发布快一年了,为保障项目迭代发布,没有及时更新react版本,最近由于开启了新项目,于是使用新的react版本进行了项目开发.项目工程如何搭建,如何满足兼容性要求,如何规 ...

  2. react.lazy 路由懒加载_Vue面试题: 如何实现路由懒加载?

    非懒加载 import List from '@/components/list.vue' const router = new VueRouter({routes: [{ path: '/list' ...

  3. SAP Spartacus CMS Component的lazy loading懒加载方式

    代码如下: ConfigModule.withConfig({cmsComponents: {SimpleResponsiveBannerComponent: {component: () => ...

  4. SAP UI5 JavaScript文件的lazy load - 懒加载

    Created by Wang, Jerry, last modified on May 18, 2016 要获取更多Jerry的原创文章,请关注公众号"汪子熙":

  5. FetchType.LAZY和FetchType.EAGER什么区别?(懒加载和急加载的理解)

    1.FetchType.LAZY:懒加载,加载一个实体时,定义懒加载的属性不会马上从数据库中加载. 2.FetchType.EAGER:急加载,加载一个实体时,定义急加载的属性会立即从数据库中加载. ...

  6. vue延迟渲染组件_性能优化之组件懒加载: Vue Lazy Component 介绍

    这篇文章分享了从遇到前端业务性能问题,到分析.解决并且梳理出通用的Vue 2.x 组件级懒加载解决方案(Vue Lazy Component )的过程. 初始加载资源过多 问题起源于我们的一个页面,下 ...

  7. [译]带你揭开Kotlin中属性代理和懒加载语法糖衣

    翻译说明: 原标题: How Kotlin's delegated properties and lazy-initialization work 原文地址: https://medium.com/t ...

  8. 解决hibernate中的懒加载(延迟加载)问题

    解决hibernate中的懒加载(延迟加载)问题 我们在开发的时候经常会遇到延迟加载问题,在实体映射时,多对一和多对多中,多的一样的属性默认是lazy="true"(即,默认是延迟 ...

  9. vue图片加载失败使用默认图片,el-image支持懒加载,自定义占位、加载失败等

    <template><d2-container><h3>image加载失败使用默认图片</h3><img src=""alt= ...

最新文章

  1. 2020-11-27(下标寻址和指针寻址)
  2. 雷锋网独家解读:阿里云原生应用的布局与策略
  3. C++描述杭电OJ 2016. 数据的交换输出 ||
  4. 第11步 git推送失败 用户模块开发
  5. 我的ELK搭建笔记(阿里云上部署)
  6. SpringCloud 入门教程(七): 熔断机制 -- 断路器
  7. hadoop中setup,cleanup,run和context讲解
  8. 算法应用三:【图像分割】+【边缘检测】canny边缘检测--冈萨雷斯--《数字图像处理》
  9. LeetCode(897)——递增顺序查找树(JavaScript)
  10. 如今前端程序员还有前途吗?
  11. 经典面试问题回答思路
  12. 波特率传输字节数计算方法
  13. java中composition_Java基础教程之组合(composition)
  14. NYOJ 137 取石子(三)(教主神题)
  15. jmeter持续时间设置
  16. h桥控制电机刹车_51单片机H桥电路控制电机正反转和PWM调速
  17. “救命,我就是那个廉价的实习生”
  18. SugarCRM 自定义选择按钮
  19. 计算机函数表格制作图片作业,使用VOOKUP函数,制作一个报价单简易小程序-Excel电子表格...
  20. 小程序标准有结论了,W3C发布小程序技术标准白皮书

热门文章

  1. C/C++项目实战:实现在线考试系统,附源码
  2. eve 服务器维护 残骸,凭一己之力改变游戏规则!她是EVE陨落飞行员纪念碑的建造者...
  3. springboot 部署微信小程序后台
  4. 白帽子讲Web安全——客户端安全
  5. 白帽子讲web安全 | 第9章 认证与会话管理
  6. SVM分类器C++语言实现
  7. Mybatis Plus一对多完整版实战教学!
  8. 关于百度编辑器编辑模板
  9. 好看的皮囊千篇一律,有趣的书籍万里挑一,学习Java必读的两款书籍推荐
  10. 程序员怎样在35岁前实现财务自由?