栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring中@Lazy注解的使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring中@Lazy注解的使用

Spring中@Lazy注解的使用
  • 1 @Lazy的简介
  • 2 @Lazy的使用
        • 1 准备一个Springboot环境
        • 2 准备两个实体类对象
        • 3 添加启动类
        • 4 测试查看控制台
        • 5 去掉Person上的 @Lazy注解,重启项目
  • 3 @Lazy的作用
      • 1 延迟加载bean对象(如上案列)
      • 2 解决循环依赖问题
        • 1 添加两个配置类
        • 2 重启项目, 项目报错,代码中存在循环依赖
        • 3 重启项目,查看日志
  • 4 错误总结
        • 1 在项目启动过程中, 遇到异常错误

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 {

	
	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一下项目, 重启项目即可.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/351224.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号