• Spring中@Lazy注解的使用


    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;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    查看注解源码可知,只有一个参数, 默认为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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    4 测试查看控制台

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

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

    我User被初始化了.............
    我Person被初始化了.............
    
    • 1
    • 2

    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");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

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

    Description:
    
    The dependencies of some of the beans in the application context form a cycle:
    
    • 1
    • 2
    • 3

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

    @Component
    public class PersonConfig {
    
        private UserConfig userConfig;
    
    
        public PersonConfig(@Lazy UserConfig userConfig) {
            this.userConfig = userConfig;
            System.out.println("我是用户配置 PersonConfig");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3 重启项目,查看日志

    我是用户配置 PersonConfig
    我是用户配置 UserConfig
    
    • 1
    • 2

    4 错误总结

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

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

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

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    AI与医疗保健:革命性技术如何拯救生命
    模拟问题(上)
    【LeetCode】 第 371 场周赛
    海外IP代理科普——API代理
    DDOS攻击分析
    基于C#的屏幕鼠标跟随圈圈应用 - 开源研究系列文章 - 个人小作品
    3DTiles 1.0 数据规范详解[4.2] i3dm瓦片二进制数据文件结构
    正则表达式在Java中的使用
    SpringBoot+Swagger详细使用方法
    如何拉取钉钉的外出、出差审批单
  • 原文地址:https://blog.csdn.net/m0_67393295/article/details/126116643