@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 注解详解_第1张图片
直接被初始化

加@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);
	}

}

结果

@Lazy 注解详解_第2张图片
没有被初始化

使用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 注解详解_第3张图片
正确初始化,所以当加了@Lazy的注解在容器启动时不会初始化,会在第一次被使用时初始化。

你可能感兴趣的:(java,开发语言)