1 简介

懒加载:使用Bean时实例化。降低启动内存消耗和启动时间。提高获取Bean时间。
正常加载:SpringBoot启动时(容器)实例化Bean。提高启动内存消耗和启动时间。降低获取Bean时间。

2 配置

通过@Lazy直接标记懒加载的Bean,默认为正常加载。
测试样例中新建了两种Bean,正常加载和懒加载。

package com.monkey.tutorial.common.mybean;

import com.monkey.tutorial.common.constant.BooleanConstant;
import com.monkey.tutorial.modules.user.vo.BaseUserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.ResolvableType;

import java.util.Map;

/**
 * 载入Bean.
 *
 * @author xindaqi
 * @date 2021-10-08 14:49
 */
public class LoadBeanTest {

    private static final Logger logger = LoggerFactory.getLogger(LoadBeanTest.class);

    @Bean(name = "baseUserVOTwo")
    @Lazy
    public BaseUserVO getBaseUserVOTwo() {
        return new BaseUserVO("123-2", "InitMethod-Two", "female");
    }

    @Autowired(required = BooleanConstant.FALSE)
    BaseUserVO baseUserVO;

    public static void main(String[] args) {
//        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(LoadBeanTest.class);
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(LoadBeanTest.class);

        String[] beanNames = applicationContext.getBeanDefinitionNames();

        for(String beanName : beanNames) {
            System.out.println("Bean name:" + beanName);
        }
        LoadBeanTest loadBeanTest = applicationContext.getBean(LoadBeanTest.class);
        System.out.println("Bean baseUser:" + loadBeanTest.baseUserVO);
        BeanDefinition beanDefinitionLoadBean = applicationContext.getBeanFactory().getBeanDefinition("loadBeanTest");
        String parentName = beanDefinitionLoadBean.getParentName();
        String beanClassName = beanDefinitionLoadBean.getBeanClassName();
        String scope = beanDefinitionLoadBean.getScope();
        boolean isLazyInit = beanDefinitionLoadBean.isLazyInit();
        String[] dependsOn = beanDefinitionLoadBean.getDependsOn();
        boolean isAutowireCandidate = beanDefinitionLoadBean.isAutowireCandidate();
        boolean isPrimary = beanDefinitionLoadBean.isPrimary();
        String factoryBeanName = beanDefinitionLoadBean.getFactoryBeanName();
        String factoryMethodName = beanDefinitionLoadBean.getFactoryMethodName();
        ConstructorArgumentValues constructorArgumentValues = beanDefinitionLoadBean.getConstructorArgumentValues();
        Map<Integer, ConstructorArgumentValues.ValueHolder> constructorArgumentValues1 = constructorArgumentValues.getIndexedArgumentValues();
        MutablePropertyValues mutablePropertyValues = beanDefinitionLoadBean.getPropertyValues();
        String initMethodName = beanDefinitionLoadBean.getInitMethodName();
        String destroyMethodName = beanDefinitionLoadBean.getDestroyMethodName();
        int role = beanDefinitionLoadBean.getRole();
        String description = beanDefinitionLoadBean.getDescription();
        ResolvableType resolvableType = beanDefinitionLoadBean.getResolvableType();
        String resourceDescription = beanDefinitionLoadBean.getResourceDescription();
        BeanDefinition originatingBeanDefinition = beanDefinitionLoadBean.getOriginatingBeanDefinition();

        System.out.println(">>>>>>>>loadBeanTest\nParentName:" + parentName + ",\nBeanClassName:" + beanClassName +
                ",\nscope:" + scope +
                ",\n isLazyInit:" + isLazyInit +
                ",\ndependsOn:" + dependsOn +
                ",\nisAutowireCandidate:" + isAutowireCandidate +
                ",\nisPrimary:" + isPrimary +
                ",\nfactoryBeanName:" + factoryBeanName +
                ",\nfactoryMethodName:" + factoryMethodName +
                ",\nconstructorArgumentValues:" + constructorArgumentValues1 +
                ",\nmutablePropertyValues:" + mutablePropertyValues +
                ",\ninitMethodName:" + initMethodName +
                ",\ndestroyMethodName:" + destroyMethodName +
                ",\nrole:" + role +
                ",\ndescription:" + description +
                ",\nresolvableType:" + resolvableType +
                ",\nresourceDescription:" + resourceDescription +
                ",\noriginatingBeanDefinition:" + originatingBeanDefinition);

        BeanDefinition beanDefinitionBase = applicationContext.getBeanFactory().getBeanDefinition("baseUserVOTwo");
        String parentNameBase = beanDefinitionBase.getParentName();
        String beanClassNameBase = beanDefinitionBase.getBeanClassName();
        String scopeBase = beanDefinitionBase.getScope();
        boolean isLazyInitBase = beanDefinitionBase.isLazyInit();
        String[] dependsOnBase = beanDefinitionBase.getDependsOn();
        boolean isAutowireCandidateBase = beanDefinitionBase.isAutowireCandidate();
        boolean isPrimaryBase = beanDefinitionBase.isPrimary();
        String factoryBeanNameBase = beanDefinitionBase.getFactoryBeanName();
        String factoryMethodNameBase = beanDefinitionBase.getFactoryMethodName();
        ConstructorArgumentValues constructorArgumentValuesBase = beanDefinitionBase.getConstructorArgumentValues();
        Map<Integer, ConstructorArgumentValues.ValueHolder> constructorArgumentValues1Base = constructorArgumentValuesBase.getIndexedArgumentValues();
        MutablePropertyValues mutablePropertyValuesBase = beanDefinitionBase.getPropertyValues();
        String initMethodNameBase = beanDefinitionBase.getInitMethodName();
        String destroyMethodNameBase = beanDefinitionBase.getDestroyMethodName();
        int roleBase = beanDefinitionBase.getRole();
        String descriptionBase = beanDefinitionBase.getDescription();
        ResolvableType resolvableTypeBase = beanDefinitionBase.getResolvableType();
        String resourceDescriptionBase = beanDefinitionBase.getResourceDescription();
        BeanDefinition originatingBeanDefinitionBase = beanDefinitionBase.getOriginatingBeanDefinition();

        System.out.println(">>>>>>>>baseUserVOTwo\nParentName:" + parentNameBase + ",\nBeanClassName:" + beanClassNameBase +
                ",\nscope:" + scopeBase +
                ",\n isLazyInit:" + isLazyInitBase +
                ",\ndependsOn:" + dependsOnBase +
                ",\nisAutowireCandidate:" + isAutowireCandidateBase +
                ",\nisPrimary:" + isPrimaryBase +
                ",\nfactoryBeanName:" + factoryBeanNameBase +
                ",\nfactoryMethodName:" + factoryMethodNameBase +
                ",\nconstructorArgumentValues:" + constructorArgumentValues1Base +
                ",\nmutablePropertyValues:" + mutablePropertyValuesBase +
                ",\ninitMethodName:" + initMethodNameBase +
                ",\ndestroyMethodName:" + destroyMethodNameBase +
                ",\nrole:" + roleBase +
                ",\ndescription:" + descriptionBase +
                ",\nresolvableType:" + resolvableTypeBase +
                ",\nresourceDescription:" + resourceDescriptionBase +
                ",\noriginatingBeanDefinition:" + originatingBeanDefinitionBase);
    }
}

2.1 正常加载

正常加载的Bean,Bean信息如图2.2所示:
scope:singleton
isLazyInit:false
由此可知,默认的Bean作用域为singlton,懒加载标志位为false。
在这里插入图片描述

图2.1 Bean正常加载

2.2 懒加载

懒加载的Bean,Bean信息如图2.2所示:
scope:singleton
isLazyInit:true
由此可知,默认的Bean作用域为singlton,懒加载标志位为true,即懒加载。

在这里插入图片描述

图2.2 Bean懒加载
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐