@Lazy 注解详解

作用:

@Lazy注解在Spring框架中用于声明一个bean的懒加载行为。当一个bean被标记为@Lazy时,它不会在容器启动时立即初始化,而是在第一次真正需要使用这个bean的时候才进行实例化。

源码

package org.springframework.context.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Lazy {
    boolean value() default true;
}

这个注解可以用在类或接口、方法上、字段上等。

例子

不加@Lazy

不加@Lazy注解启动程序

package com.example.springtest.lazy;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service("TestService10")
public class TestService {
    public TestService() {
        System.out.println("DictService初始化完成");
    }

    public void hello(){
        System.out.println();
    }
}

启动类

package com.example.springtest;

import com.example.springtest.lazy.TestService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages={"com.example.springtest","com.example.child"})
public class Application {

	public static void main(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
	}

}

结果

在这里插入图片描述
直接被初始化

加@Lazy

package com.example.springtest.lazy;

import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service("TestService10")
@Lazy
public class TestService {
    public TestService() {
        System.out.println("DictService初始化完成");
    }

    public void hello(){
        System.out.println();
    }
}

启动类

package com.example.springtest;

import com.example.springtest.lazy.TestService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages={"com.example.springtest","com.example.child"})
public class Application {

	public static void main(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
	}

}

结果

在这里插入图片描述
没有被初始化

使用TestService

我们在容器启动后去容器中拿TestService 对象看看会被初始化吗?

启动类改一下,增加获取对象

package com.example.springtest;

import com.example.springtest.lazy.TestService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages={"com.example.springtest","com.example.child"})
public class Application {

	public static void main(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
		TestService testService10 = (TestService) context.getBean("TestService10");
	}

}

在这里插入图片描述
正确初始化,所以当加了@Lazy的注解在容器启动时不会初始化,会在第一次被使用时初始化。

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值