SpringBoot:使用 @Lazy 注解懒加载
为什么需要懒加载?
我们知道,在 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");}
}
那么说明,在打上了 @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";}
}
解决这个问题,我们也需要在 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 注解懒加载相关推荐
- react.lazy 路由懒加载_React lazy/Suspense使用及源码解析
React v16.6.0已经发布快一年了,为保障项目迭代发布,没有及时更新react版本,最近由于开启了新项目,于是使用新的react版本进行了项目开发.项目工程如何搭建,如何满足兼容性要求,如何规 ...
- react.lazy 路由懒加载_Vue面试题: 如何实现路由懒加载?
非懒加载 import List from '@/components/list.vue' const router = new VueRouter({routes: [{ path: '/list' ...
- SAP Spartacus CMS Component的lazy loading懒加载方式
代码如下: ConfigModule.withConfig({cmsComponents: {SimpleResponsiveBannerComponent: {component: () => ...
- SAP UI5 JavaScript文件的lazy load - 懒加载
Created by Wang, Jerry, last modified on May 18, 2016 要获取更多Jerry的原创文章,请关注公众号"汪子熙":
- FetchType.LAZY和FetchType.EAGER什么区别?(懒加载和急加载的理解)
1.FetchType.LAZY:懒加载,加载一个实体时,定义懒加载的属性不会马上从数据库中加载. 2.FetchType.EAGER:急加载,加载一个实体时,定义急加载的属性会立即从数据库中加载. ...
- vue延迟渲染组件_性能优化之组件懒加载: Vue Lazy Component 介绍
这篇文章分享了从遇到前端业务性能问题,到分析.解决并且梳理出通用的Vue 2.x 组件级懒加载解决方案(Vue Lazy Component )的过程. 初始加载资源过多 问题起源于我们的一个页面,下 ...
- [译]带你揭开Kotlin中属性代理和懒加载语法糖衣
翻译说明: 原标题: How Kotlin's delegated properties and lazy-initialization work 原文地址: https://medium.com/t ...
- 解决hibernate中的懒加载(延迟加载)问题
解决hibernate中的懒加载(延迟加载)问题 我们在开发的时候经常会遇到延迟加载问题,在实体映射时,多对一和多对多中,多的一样的属性默认是lazy="true"(即,默认是延迟 ...
- vue图片加载失败使用默认图片,el-image支持懒加载,自定义占位、加载失败等
<template><d2-container><h3>image加载失败使用默认图片</h3><img src=""alt= ...
最新文章
- 2020-11-27(下标寻址和指针寻址)
- 雷锋网独家解读:阿里云原生应用的布局与策略
- C++描述杭电OJ 2016. 数据的交换输出 ||
- 第11步 git推送失败 用户模块开发
- 我的ELK搭建笔记(阿里云上部署)
- SpringCloud 入门教程(七): 熔断机制 -- 断路器
- hadoop中setup,cleanup,run和context讲解
- 算法应用三:【图像分割】+【边缘检测】canny边缘检测--冈萨雷斯--《数字图像处理》
- LeetCode(897)——递增顺序查找树(JavaScript)
- 如今前端程序员还有前途吗?
- 经典面试问题回答思路
- 波特率传输字节数计算方法
- java中composition_Java基础教程之组合(composition)
- NYOJ 137 取石子(三)(教主神题)
- jmeter持续时间设置
- h桥控制电机刹车_51单片机H桥电路控制电机正反转和PWM调速
- “救命,我就是那个廉价的实习生”
- SugarCRM 自定义选择按钮
- 计算机函数表格制作图片作业,使用VOOKUP函数,制作一个报价单简易小程序-Excel电子表格...
- 小程序标准有结论了,W3C发布小程序技术标准白皮书