Spring中@Lazy注解的使用示例教程

 更新时间:2023年06月26日 15:16:19   作者:韩_师兄  
Spring在应用程序上下文启动时去创建所有的单例bean对象, 而@Lazy注解可以延迟加载bean对象,即在使用时才去初始化,这篇文章主要介绍了Spring中@Lazy注解的使用,需要的朋友可以参考下

Spring在应用程序上下文启动时去创建所有的单例bean对象, 而@Lazy注解可以延迟加载bean对象,即在使用时才去初始化.

所以,@Lazy注解, 一是可以减少Spring的IOC容器启动时的加载时间, 二是可以解决bean的循环依赖问题

1 @Lazy的简介

@Lazy注解用于标识bean是否需要延迟加载.

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Lazy {
	/**
	 * Whether lazy initialization should occur.
	 */
	boolean value() default true;
}

查看注解源码可知,只有一个参数, 默认为true, 即添加该注解的bean对象就会延迟初始化.

2 @Lazy的使用

以SpringBoot环境为例

1 准备一个Springboot环境

2 准备两个实体类对象

@Data
@NoArgsConstructor
public class User {
    private String name;
    private String phone;
    private Integer age;
    private Person person;
    public User(String name, String phone, Integer age) {
        System.out.println("我User被初始化了.............");
        this.name = name;
        this.phone = phone;
        this.age = age;
    }
}
@Data
@NoArgsConstructor
public class Person {
    private String name;
    private String phone;
    private Integer age;
    private User user;
    public Person(String name, String phone, Integer age) {
        System.out.println("我Person被初始化了.............");
        this.name = name;
        this.phone = phone;
        this.age = age;
    }
}

3 添加启动类

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    @Bean
    public User createUser() {
        return new User("韩宣生", "11111", 24);
    }
    @Bean
    @Lazy
    public Person createPerson() {
        return new Person("韩立", "11111", 24);
    }
}

4 测试查看控制台

我User被初始化了.............

5 去掉Person上的 @Lazy注解,重启项目

我User被初始化了.............
我Person被初始化了.............

3 @Lazy的作用

1 延迟加载bean对象(如上案列)

2 解决循环依赖问题

1 添加两个配置类

@Component
public class PersonConfig {
    private UserConfig userConfig;
    public PersonConfig( UserConfig userConfig) {
        this.userConfig = userConfig;
        System.out.println("我是用户配置 PersonConfig");
    }
}
@Component
public class UserConfig {
    private PersonConfig personConfig;
    public UserConfig(PersonConfig personConfig) {
        this.personConfig = personConfig;
        System.out.println("我是用户配置 UserConfig");
    }
}

2 重启项目, 项目报错,代码中存在循环依赖

Description:
The dependencies of some of the beans in the application context form a cycle:

解决办法,给其中一个添加@Lazy注解,如

@Component
public class PersonConfig {
    private UserConfig userConfig;
    public PersonConfig(@Lazy UserConfig userConfig) {
        this.userConfig = userConfig;
        System.out.println("我是用户配置 PersonConfig");
    }
}

3 重启项目,查看日志

我是用户配置 PersonConfig
我是用户配置 UserConfig

4 错误总结

1 在项目启动过程中, 遇到异常错误

'url' attribute is not specified and no embedded datasource could be configu

解决方法: 是项目没有将application.yml配置文件加载. 点击maven中clean一下项目, 重启项目即可.

到此这篇关于Spring中@Lazy注解的使用的文章就介绍到这了,更多相关Spring @Lazy注解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • MybatisPlus中的insert操作详解

    MybatisPlus中的insert操作详解

    这篇文章主要介绍了MybatisPlus中的insert操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • Java使用NIO包实现Socket通信的实例代码

    Java使用NIO包实现Socket通信的实例代码

    本篇文章主要介绍了Java使用NIO包实现Socket通信的实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • HttpsURLConnection上传文件流(实例讲解)

    HttpsURLConnection上传文件流(实例讲解)

    下面小编就为大家带来一篇HttpsURLConnection上传文件流(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-07-07
  • 使用Spring Cache时设置缓存键的注意事项详解

    使用Spring Cache时设置缓存键的注意事项详解

    在现代的Web应用中,缓存是提高系统性能和响应速度的重要手段之一,Spring框架提供了强大的缓存支持,通过​​@Cacheable​​、​​@CachePut​​、​​@CacheEvict​​等注解可以方便地实现缓存功能,本文给大家介绍了使用Spring Cache时设置缓存键的注意事项
    2025-01-01
  • Java Cookie与Session实现会话跟踪详解

    Java Cookie与Session实现会话跟踪详解

    session的工作原理和cookie非常类似,在cookie中存放一个sessionID,真实的数据存放在服务器端,客户端每次发送请求的时候带上sessionID,服务端根据sessionID进行数据的响应
    2022-11-11
  • java实现超市管理系统

    java实现超市管理系统

    这篇文章主要为大家详细介绍了java实现超市管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12
  • Java集合和IO流实现水果摊项目

    Java集合和IO流实现水果摊项目

    最近闲来无事,使用java基础知识集合和IO流做了一个简单的小项目,水果摊项目,用到GUI和Mysql数据库搭建,本文通过实例代码给大家介绍的非常详细,需要的朋友参考下吧
    2021-06-06
  • 聊聊@RequestParam,@PathParam,@PathVariable等注解的区别

    聊聊@RequestParam,@PathParam,@PathVariable等注解的区别

    这篇文章主要介绍了聊聊@RequestParam,@PathParam,@PathVariable等注解的区别,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • SpringMVC 跨重定向请求传递数据的方法实现

    SpringMVC 跨重定向请求传递数据的方法实现

    这篇文章主要介绍了SpringMVC 跨重定向请求传递数据的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • springboot-dubbo cannot be cast to问题及解决

    springboot-dubbo cannot be cast to问题及解决

    这篇文章主要介绍了springboot-dubbo cannot be cast to问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-04-04

最新评论