Spring@懒惰注释
介绍:
默认情况下, Spring框架在应用程序启动时加载并热切初始化所有bean。 在我们的应用程序中,我们可能有一些非常消耗资源的bean。 我们宁愿根据需要加载此类bean。 我们可以使用Spring @Lazy批注实现此目的 。
在本教程中,我们将学习如何使用@Lazy注释延迟加载我们的bean。
延迟初始化:
如果我们用@Lazy注释标记我们的Spring配置类,那么所有带有@Bean注释的已定义bean将被延迟加载:
@Configuration @ComponentScan (basePackages = "com.programmergirl.university" ) @Lazy public class AppConfig { @Bean public Student student() { return new Student(); } @Bean public Teacher teacher() { return new Teacher(); } }
通过在方法级别使用此注释,我们还可以延迟加载单个bean:
@Bean @Lazy public Teacher teacher() { return new Teacher(); }
测试延迟加载:
让我们通过运行应用程序来快速测试此功能:
public class SampleApp { private static final Logger LOG = Logger.getLogger(SampleApp. class ); public static void main(String args[]) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig. class ); LOG.info( "Application Context is already up" ); // Beans in our Config class lazily loaded Teacher teacherLazilyLoaded = context.getBean(Teacher. class ); Student studentLazilyLoaded = context.getBean(Student. class ); } }
在控制台上,我们将看到:
Bean factory for ...AnnotationConfigApplicationContext: ...DefaultListableBeanFactory: [...] ... Application Context is already up Inside Teacher Constructor Inside Student Constructor
显然, Spring在需要时而不是在设置应用程序上下文时初始化了Student和Teacher Bean。
使用
我们还可以在注入点使用@Lazy批注:构造函数,setter或字段级。
假设我们有一个要延迟加载的Classroom类:
@Component @Lazy public class Classroom { public Classroom() { System.out.println( "Inside Classroom Constructor" ); } ... }
然后通过@Autowired注释将其连接到University bean:
@Component public class University { @Lazy @Autowired private Classroom classroom; public University() { System.out.println( "Inside University Constructor" ); } public void useClassroomBean() { this .classroom.getDetails(); ... } }
在这里,我们懒惰地注入了Classroom bean。 因此,在实例化University对象时,Spring将创建代理Classroom对象并将其映射到该对象。 最后,当我们调用useClassroomBean()时 ,才创建实际的Classroom实例:
// in our main() method AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); LOG.info( "Application Context is already up" ); University university = context.getBean(University. class ); LOG.info( "Time to use the actual classroom bean..." ); university.useClassroomBean();
上面的代码将产生以下日志:
Bean factory for ...AnnotationConfigApplicationContext: ...DefaultListableBeanFactory: [...] ... Inside University Constructor ... Application Context is already up Time to use the actual classroom bean... Inside Classroom Constructor
如我们所见, Classroom对象的实例化被延迟,直到其实际需要为止。
请注意,对于延迟注入,我们必须在组件类以及注入点上都使用@Lazy批注。
结论:
在本快速教程中,我们学习了如何延迟加载Spring Bean。 我们讨论了延迟初始化和延迟注入。
翻译自: https://www.javacodegeeks.com/2019/09/spring-lazy-annotation.html
Spring@懒惰注释相关推荐
- Spring JSR-250 注释
转载自 Spring JSR-250 注释 Spring JSR-250 注释 Spring还使用基于 JSR-250 注释,它包括 @PostConstruct, @PreDestroy 和 @R ...
- Spring @Qualifier 注释
转载自 Spring @Qualifier 注释 Spring @Qualifier 注释 可能会有这样一种情况,当你创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装 ...
- Spring @Autowired 注释
转载自 Spring @Autowired 注释 Spring @Autowired 注释 @Autowired 注释对在哪里和如何完成自动连接提供了更多的细微的控制. @Autowired 注释可 ...
- Spring @Required 注释
转载自 Spring @Required 注释 Spring @Required 注释 @Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必 ...
- spring boot注释_Spring Boot中的@SpringBootConfiguration注释
spring boot注释 Spring Boot中的 @SpringBootConfiguration注释是一个类级别的注释,它指示此类提供了应用程序配置. 通常,具有main()方法的类最适合此注 ...
- spring 定时器注释_带注释的控制器– Spring Web / Webflux和测试
spring 定时器注释 Spring Webflux和Spring Web是两个完全不同的Web堆栈. 但是, Spring Webflux继续支持基于注释的编程模型 使用这两个堆栈定义的端点可能看 ...
- spring boot注释_使用Spring Boot和注释支持配置Spring JMS应用程序
spring boot注释 1.简介 在以前的文章中,我们学习了如何使用Spring JMS配置项目. 如果查看有关使用Spring JMS进行消息传递的文章介绍 ,您会注意到它是使用XML配置的. ...
- spring aop 注释_使用Spring AOP,自定义注释和反射为您的应用程序审核基础结构
spring aop 注释 下一篇文章将演示如何使用Spring AOP和注释编写简单的审计. 审核机制将是干净,高效且易于维护的(和Kewwl!). 我将在用户管理系统上演示我的示例(假设您具有反射 ...
- Spring核心注释
介绍: org.springframework.beans.factory.annotation和org.springframework.context.annotation包中存在的Spring注释 ...
最新文章
- 卷积神经网络原理_怎样设计最优的卷积神经网络架构?| NAS原理剖析
- 基于系统科学理论的认知科学研究进展
- Nginx配置和内核优化 实现突破十万并发
- optee3.8 qemu_v8的环境搭建篇(ubuntu20.04)
- oracle服务端字符集
- Vue基础之Vue实例
- PHP+MySql获取新添加记录的ID值
- response和request
- webpack ,require.ensure配合oclazyload 实现ng1模块懒加载
- mysql单个表_mysql – 哪个更有效:一个长单表还是分布表?为什么?
- 为什么调试的时候需要编译选项中添加 -g,readelf命令.
- 微星电脑win、ctrl等组合键时而失灵
- java多线程计算pi_Java多线程——计算1-20阶乘和
- 配音软件文字转语音有哪些?
- shell笔记_重定向与文本处理命令
- 视频号怎么赚钱?4个赚钱小技巧,实现视频号流量变现!
- 利用Python获取历史双色球开奖结果
- 论文投稿指南——中文核心期刊推荐(物理学)
- 攻防世界 Ditf misc
- H3C交换机常用命令(初学)