欢迎访问 生活随笔!

尊龙游戏旗舰厅官网

当前位置: 尊龙游戏旗舰厅官网 > 前端技术 > javascript >内容正文

javascript

springboot实现懒加载@lazy -尊龙游戏旗舰厅官网

发布时间:2025/1/21 javascript 20 豆豆
尊龙游戏旗舰厅官网 收集整理的这篇文章主要介绍了 springboot实现懒加载@lazy 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

@lazy使用说明

  • 一般情况下,spring容器在启动时会创建所有的bean对象,使用@lazy注解可以将bean对象的创建延迟到第一次使用bean的时候

使用方法

1、@lazy(value = true):默认为true,不执行构造方法
2、@lazy(value = false):执行构造方法

将bean加载搭到spring容器的方式

  • @configuration @bean
@configuration public class beanconfig {@bean@lazy(value = true)public people people() {return new people();} }
  • @component
@component @lazy(value = true) public class cat{public cat() {system.out.println("#######小白猫#######);} }
  • @import
@import(value = {dog.class})@lazy(value=false) public class dog{public dog() {system.out.println("#######大黄狗#######);} }

示例代码

1、不使用@lazy

/*** @author shuliangzhao* @title: mylazy* @projectname spring-boot-learn* @description: todo* @date 2019/10/10 19:27*/ @component public class mylazy {public mylazy() {system.out.println("懒加载...");}public void say() {system.out.println("say...");} } /*** @author shuliangzhao* @title: lazytest* @projectname spring-boot-learn* @description: todo* @date 2019/10/10 19:31*/ @runwith(springrunner.class) @springboottest public class lazytest {@autowiredprivate mylazy mylazy;@testpublic void test() {mylazy.say();} }

执行结果:

2、使用lazy

/*** @author shuliangzhao* @title: mylazy* @projectname spring-boot-learn* @description: todo* @date 2019/10/10 19:27*/ @component @lazy public class mylazy {public mylazy() {system.out.println("懒加载...");}public void say() {system.out.println("say...");} } /*** @author shuliangzhao* @title: lazytest* @projectname spring-boot-learn* @description: todo* @date 2019/10/10 19:31*/ @runwith(springrunner.class) @springboottest public class lazytest {@autowiredprivate mylazy mylazy;@testpublic void test() {mylazy.say();} }

执行结果:

与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是尊龙游戏旗舰厅官网为你收集整理的springboot实现懒加载@lazy的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得尊龙游戏旗舰厅官网网站内容还不错,欢迎将尊龙游戏旗舰厅官网推荐给好友。

网站地图