今天主要从以下几方面来介绍一下@Lazy注解

  • @Lazy注解是什么

  • @Lazy注解怎么使用

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,也就是说只要加了这个注解就会延迟加载

2,@Lazy注解怎么使用

 

没加注解之前主要容器启动就会实例化bean,如下:

AnnotationConfigApplicationContext applicationContext2 = new AnnotationConfigApplicationContext(MainConfig.class);
创建user实例

而加上@Lazy注解则必须在第一次调用的时候才会加载如下:

/**
    * 定义一个bean对象
    * @return
    */
   @Scope
   @Lazy
   @Bean(value="user0",name="user0",initMethod="initUser",destroyMethod="destroyUser")
   public User getUser(){
       System.out.println("创建user实例");
       return new User("张三",26);
   }
AnnotationConfigApplicationContext applicationContext2 = new AnnotationConfigApplicationContext(MainConfig.class);
User bean2 = applicationContext2.getBean(User.class);
创建user实例
实例1 === User [userName=张三, age=26]

@Lazy注解注解的作用主要是减少springIOC容器启动的加载时间

spring注解之@Lazy的更多相关文章

  1. Spring注解之@Lazy注解

    @Lazy用于指定该Bean是否取消预初始化.主要用于修饰Spring Bean类,用于指定该Bean的预初始化行为, 使用该Annotation时可以指定一个boolean型的value属性,该属性 ...

  2. Spring注解之@Lazy注解,源码分析和总结

    一 关于延迟加载的问题,有次和大神讨论他会不会直接或间接影响其他类.spring的好处就是文档都在代码里,网上百度大多是无用功. 不如,直接看源码.所以把当时源码分析的思路丢上来一波. 二 源码分析 ...

  3. 【Spring注解驱动开发】使用@Lazy注解实现懒加载

    写在前面 Spring在启动时,默认会将单实例bean进行实例化,并加载到Spring容器中.也就是说,单实例bean默认在Spring容器启动的时候创建对象,并将对象加载到Spring容器中.如果我 ...

  4. spring注解大全

    出自http://www.cnblogs.com/xiaoxi/p/5935009.html 1.@Autowired @Autowired顾名思义,就是自动装配,其作用是为了消除代码Java代码里面 ...

  5. spring注解简单记录

    @Autowired 自动匹配,按类型 @qualifiter("beanname") 当有多个bean匹配时,可指定bean名称 @Resource byname优先匹配,然后b ...

  6. Spring注解 系列之Spring常用注解总结

    参考:Spring系列之Spring常用注解总结 (1) Resource 默认是byName的方式进行bean配置,@AutoWired默认是按照byType的方式进行装配bean的:(2)Comp ...

  7. Spring 注解驱动(一)基本使用规则

    Spring 注解驱动(一)基本使用规则 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.基本使用 @Configur ...

  8. Spring 注解原理(三)@Qualifier @Value

    Spring 注解原理(三)@Qualifier @Value Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.Aut ...

  9. Spring注解 @Configuration

    Spring注解 @Configuration 一.@Configuration的作用 二.@Configuration的Spring容器启动方式 三.不加@Configuration的@Bean的解 ...

随机推荐

  1. HDU1848 Fibonacci again and again 博弈 SG函数

    题意:三堆石子,每次能拿走斐波那契数个石子,先取完石子胜,问先手胜还是后手胜  石子个数<=1000 多组数据 题目链接:http://acm.hdu.edu.cn/showproblem.ph ...

  2. Oracle表中添加外键约束

    添加主键约束: ALTER TABLE GA_AIRLINE ADD CONSTRAINT PK_AIRLINE_ID PRIMARY KEY(AIRLINE_ID); 有三种形式的外键约束: 1.普 ...

  3. apache(httpd)配置

    1.简单配置 1 监听地址 2 主页目录 3 别名 4 目录访问的身份验证 5 https 6 MPM(under linux) * 配置文件中路径.文件名均不支持中文. <<<&l ...

  4. 使用tpcc-mysql进行性能测试

    tpcc-mysql的github地址:https://github.com/Percona-Lab/tpcc-mysql tpcc-mysql源代码进行编译,服务器上需要安装有mysql git c ...

  5. VMware里的linux系统里的命令行里会有bee的声音,要如何关掉

    取消bell报警声的方法:登陆linux系统vi /etc/inputrc找到set bell-style none 将前面的#去掉,之后重启系统即可解决声音问题 若不见效可以通过下面的方式解决下be ...

  6. uva-387-暴力枚举

    题意: 给你一些小方块,问是不是能组成一个4X4的大方块,所有方块全部要使用,裸枚举 #include <iostream> #include <stdio.h> #inclu ...

  7. 读取配置文件properties的几种方式

    介绍几种读取方式:参考:https://www.cnblogs.com/sebastian-tyd/p/7895182.html .基于ClassLoder读取配置文件 注意:该方式只能读取类路径下的 ...

  8. C库中strcpy函数的参数为NULL问题

    在标准C库的字符串处理函数 strxxx 一系列函数中,对参数传入NULL会导致程序崩溃. 如下代码中,无论那个参数为NULL,都会让程序崩溃,所以我们必须自己对参数进行传入前的检查. int mai ...

  9. air 调用jsfl 执行对应函数,并传参

    jsflPath = "WindowSWF/dt_tool_jsfl/" + event.item.fileName+".jsfl"; var element_ ...

  10. upcast

    class A { public: A():i(){} int get_i() { cout << "A.get_i" << endl; return i; ...