广告
返回顶部
首页 > 资讯 > 精选 >Spring中getSingleton的使用方法
  • 953
分享到

Spring中getSingleton的使用方法

2023-06-20 12:06:52 953人浏览 安东尼
摘要

本篇内容主要讲解“spring中getSingleton的使用方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring中getSingleton的使用方法”吧!Spring中bean的生命

本篇内容主要讲解“spring中getSingleton的使用方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring中getSingleton的使用方法”吧!

Spring中bean的生命周期

要想讲清楚spring中bean的生命周期,真的是不容易,以AnnotationConfigApplicationContext上下文为基础来讲解bean的生命周期,AnnotationConfigApplicationContext是基于注解的上下文,使用XML的方式现在很少见,所以以此上下文为基础,使用XML的上下文ClassPathXmlApplicationContext的步骤和AnnotationConfigApplicationContext类似。

了解过Spring源码的都知道,在AnnotationConfigApplicationContext中调用了父类AbstractApplicationContext的refresh()方法,

Spring中getSingleton的使用方法

refresh方法中调用了10个左右的方法,具体方法不一一细看,看今天分析的重点方法finishBeanFactoryInitialization方法,

@Override    public void refresh() throws BeansException, IllegalStateException {        synchronized (this.startupShutdownMonitor) {            // Prepare this context for refreshing.            prepareRefresh();             // Tell the subclass to refresh the internal bean factory.            ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();             // Prepare the bean factory for use in this context.            prepareBeanFactory(beanFactory);             try {                // Allows post-processing of the bean factory in context subclasses.                postProcessBeanFactory(beanFactory);                 // Invoke factory processors reGIStered as beans in the context.                invokeBeanFactoryPostProcessors(beanFactory);                 // Register bean processors that intercept bean creation.                registerBeanPostProcessors(beanFactory);                 // Initialize message source for this context.                initMessageSource();                 // Initialize event multicaster for this context.                initApplicationEventMulticaster();                 // Initialize other special beans in specific context subclasses.                onRefresh();                 // Check for listener beans and register them.                registerListeners();                 // Instantiate all remaining (non-lazy-init) singletons.                finishBeanFactoryInitialization(beanFactory);                 // Last step: publish corresponding event.                finishRefresh();            }             catch (BeansException ex) {                if (logger.isWarnEnabled()) {                    logger.warn("Exception encountered during context initialization - " +                            "cancelling refresh attempt: " + ex);                }                 // Destroy already created singletons to avoid dangling resources.                destroyBeans();                 // Reset 'active' flag.                cancelRefresh(ex);                 // Propagate exception to caller.                throw ex;            }             finally {                // Reset common introspection caches in Spring's core, since we                // might not ever need metadata for singleton beans anymore...                resetCommonCaches();            }        }    }

在该方法中又调用了preInstantiateSingletons()方法,这个方法才是今天的主角,在该方法中会生成所有非懒加载的单例bean。方法定义如下

@Override    public void preInstantiateSingletons() throws BeansException {        if (logger.isTraceEnabled()) {            logger.trace("Pre-instantiating singletons in " + this);        }         // Iterate over a copy to allow for init methods which in turn register new bean definitions.        // While this may not be part of the regular factory bootstrap, it does otherwise work fine.        List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);         // Trigger initialization of all non-lazy singleton beans...        for (String beanName : beanNames) {            RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);            if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {                if (isFactoryBean(beanName)) {                    Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);                    if (bean instanceof FactoryBean) {                        final FactoryBean<?> factory = (FactoryBean<?>) bean;                        boolean isEagerInit;                        if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {                            isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)                                            ((SmartFactoryBean<?>) factory)::isEagerInit,                                    getAccessControlContext());                        }                        else {                            isEagerInit = (factory instanceof SmartFactoryBean &&                                    ((SmartFactoryBean<?>) factory).isEagerInit());                        }                        if (isEagerInit) {                            getBean(beanName);                        }                    }                }                else {                    getBean(beanName);                }            }        }         // Trigger post-initialization callback for all applicable beans...        for (String beanName : beanNames) {            Object singletonInstance = getSingleton(beanName);            if (singletonInstance instanceof SmartInitializingSingleton) {                final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;                if (System.getSecurityManager() != null) {                    AccessController.doPrivileged((PrivilegedAction<Object>) () -> {                        smartSingleton.afterSingletonsInstantiated();                        return null;                    }, getAccessControlContext());                }                else {                    smartSingleton.afterSingletonsInstantiated();                }            }        }    }

该方法完成的主要逻辑是遍历所有的beanName,调用getBean(beanName)方法生成单例bean,在遍历过程中对FactoryBean做了特殊的判断,大家都知道FactoryBean是一种特殊的bean,在《spring中FactoryBean是什么bean》重点分析该bean。重点看getBean(beanName)方法,getBean(beanName)方法调用了doGetBean(beanName)方法,

@Override    public Object getBean(String name) throws BeansException {        return doGetBean(name, null, null, false);    }

doGetBean方法定义如下,

protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,            @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {         final String beanName = transfORMedBeanName(name);        Object bean;         // Eagerly check singleton cache for manually registered singletons.        Object sharedInstance = getSingleton(beanName);        if (sharedInstance != null && args == null) {            if (logger.isTraceEnabled()) {                if (isSingletonCurrentlyInCreation(beanName)) {                    logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +                            "' that is not fully initialized yet - a consequence of a circular reference");                }                else {                    logger.trace("Returning cached instance of singleton bean '" + beanName + "'");                }            }            bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);        }         else {            // Fail if we're already creating this bean instance:            // We're assumably within a circular reference.            if (isPrototypeCurrentlyInCreation(beanName)) {                throw new BeanCurrentlyInCreationException(beanName);            }             // Check if bean definition exists in this factory.            BeanFactory parentBeanFactory = getParentBeanFactory();            if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {                // Not found -> check parent.                String nameToLookup = originalBeanName(name);                if (parentBeanFactory instanceof AbstractBeanFactory) {                    return ((AbstractBeanFactory) parentBeanFactory).doGetBean(                            nameToLookup, requiredType, args, typeCheckOnly);                }                else if (args != null) {                    // Delegation to parent with explicit args.                    return (T) parentBeanFactory.getBean(nameToLookup, args);                }                else if (requiredType != null) {                    // No args -> delegate to standard getBean method.                    return parentBeanFactory.getBean(nameToLookup, requiredType);                }                else {                    return (T) parentBeanFactory.getBean(nameToLookup);                }            }             if (!typeCheckOnly) {                markBeanAsCreated(beanName);            }             try {                final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);                checkMergedBeanDefinition(mbd, beanName, args);                 // Guarantee initialization of beans that the current bean depends on.                String[] dependsOn = mbd.getDependsOn();                if (dependsOn != null) {                    for (String dep : dependsOn) {                        if (isDependent(beanName, dep)) {                            throw new BeanCreationException(mbd.getResourceDescription(), beanName,                                    "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");                        }                        registerDependentBean(dep, beanName);                        try {                            getBean(dep);                        }                        catch (NoSuchBeanDefinitionException ex) {                            throw new BeanCreationException(mbd.getResourceDescription(), beanName,                                    "'" + beanName + "' depends on missing bean '" + dep + "'", ex);                        }                    }                }                 // Create bean instance.                if (mbd.isSingleton()) {                    sharedInstance = getSingleton(beanName, () -> {                        try {                            return createBean(beanName, mbd, args);                        }                        catch (BeansException ex) {                            // Explicitly remove instance from singleton cache: It might have been put there                            // eagerly by the creation process, to allow for circular reference resolution.                            // Also remove any beans that received a temporary reference to the bean.                            destroySingleton(beanName);                            throw ex;                        }                    });                    bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);                }                 else if (mbd.isPrototype()) {                    // It's a prototype -> create a new instance.                    Object prototypeInstance = null;                    try {                        beforePrototypeCreation(beanName);                        prototypeInstance = createBean(beanName, mbd, args);                    }                    finally {                        afterPrototypeCreation(beanName);                    }                    bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);                }                 else {                    String scopeName = mbd.getScope();                    final Scope scope = this.scopes.get(scopeName);                    if (scope == null) {                        throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");                    }                    try {                        Object scopedInstance = scope.get(beanName, () -> {                            beforePrototypeCreation(beanName);                            try {                                return createBean(beanName, mbd, args);                            }                            finally {                                afterPrototypeCreation(beanName);                            }                        });                        bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);                    }                    catch (IllegalStateException ex) {                        throw new BeanCreationException(beanName,                                "Scope '" + scopeName + "' is not active for the current thread; consider " +                                "defining a scoped proxy for this bean if you intend to refer to it from a singleton",                                ex);                    }                }            }            catch (BeansException ex) {                cleanupAfterBeanCreationFailure(beanName);                throw ex;            }        }         // Check if required type matches the type of the actual bean instance.        if (requiredType != null && !requiredType.isInstance(bean)) {            try {                T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);                if (convertedBean == null) {                    throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());                }                return convertedBean;            }            catch (TypeMismatchException ex) {                if (logger.isTraceEnabled()) {                    logger.trace("Failed to convert bean '" + name + "' to required type '" +                            ClassUtils.getQualifiedName(requiredType) + "'", ex);                }                throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());            }        }        return (T) bean;    }

doGetBean方法中有getSingleton、getObjectForBeanInstance、createBean等方法是很重要的。在doGetBean方法中首先调用getSingleton方法检查单例缓存中是否有该bean,如果没有则判断当前bean的作用域是单例(singleton)还是原型(prototype),如果是单例的,再次调用getSingleton方法,不过这次的该方法是重载的一个;如果是原型的则调用createBean方法生成bean,上面几个步骤生成的beanInstance均要调用getObjectForBeanInstance方法获得bean对象。先看getSingleton方法

@Nullable    protected Object getSingleton(String beanName, boolean allowEarlyReference) {        Object singletonObject = this.singletonObjects.get(beanName);        if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {            synchronized (this.singletonObjects) {                singletonObject = this.earlySingletonObjects.get(beanName);                if (singletonObject == null && allowEarlyReference) {                    ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);                    if (singletonFactory != null) {                        singletonObject = singletonFactory.getObject();                        this.earlySingletonObjects.put(beanName, singletonObject);                        this.singletonFactories.remove(beanName);                    }                }            }        }        return singletonObject;    }

在该方法中涉及到三个对象singletonObjects、earlySingletonObjects、singletonFactories,使用这三个对象可以很好的解决循环依赖的问题,这里暂时不讲这个,先看其中的逻辑,

Spring中getSingleton的使用方法

执行的逻辑大概是上面的过程。对singletonObject、earlySingletonObjects、singletonFactories逐级判断,其中singletonFactories中存储的是提前暴露的实例工厂,用来生成一个实例或者实例的代理类。

本文重点对spring生成bean的入门进行了分析,重点分析了代码的调用过程及getSingleton方法,该方法还有一个重载的方法,放到后面分析。

到此,相信大家对“Spring中getSingleton的使用方法”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

--结束END--

本文标题: Spring中getSingleton的使用方法

本文链接: https://www.lsjlt.com/news/296713.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Spring中getSingleton的使用方法
    本篇内容主要讲解“Spring中getSingleton的使用方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring中getSingleton的使用方法”吧!Spring中bean的生命...
    99+
    2023-06-20
  • Spring中bean的生命周期之getSingleton方法
    Spring中bean的生命周期 要想讲清楚spring中bean的生命周期,真的是不容易,以AnnotationConfigApplicationContext上下文为基础来讲解b...
    99+
    2022-11-12
  • spring中redis的使用方法
    这篇文章将为大家详细讲解有关spring中redis的使用方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。spring中redis怎么用在Spring中使用RedisJ...
    99+
    2022-10-18
  • 【Spring】JdbcTemplate的使用方法
    JdbcTemplate(概念和准备) 什么是 JdbcTemplate Spring 框架对 JDBC 进行封装,使用 JdbcTemplate 方便实现对数据库操作 准备工作 引入相关 jar 包 在 spring 配置文件配...
    99+
    2018-06-11
    【Spring】JdbcTemplate的使用方法 数据库入门 数据库基础教程 数据库 mysql
  • Spring中ApplicationContextAware的使用方法详解
    ApplicationContextAware 通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicat...
    99+
    2023-03-22
    Spring中ApplicationContextAware作用 Spring ApplicationContextAware
  • 使用Spring AntPathMatcher的doMatch方法
    目录AntPathMatcher的doMatch方法有4个步骤Spring的AntPathMatcher工具类用法AntPathMatcher下面是模糊匹配规则AntPathMatc...
    99+
    2022-11-12
  • Spring中的使用@Async异步调用方法
    目录使用@Async异步调用方法Async简介给Spring的TaskExecutor去完成本次记录Async使用场景异步请求与异步调用的区别异步请求的实现SpringBoot中异步...
    99+
    2022-11-12
  • Spring Boot中使用Spring Retry重试框架的操作方法
    目录Spring Retry 在SpringBoot 中的应用Maven依赖注解使用开启Retry功能注解@Retryable注解@Recover注解@CircuitBreakerR...
    99+
    2022-11-13
  • RedisTemplate方法如何在spring中使用
    本篇文章给大家分享的是有关RedisTemplate方法如何在spring中使用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。需要的jar包 spring-data-Redis...
    99+
    2023-05-31
    spring redistemplate emp
  • spring @Primary-在spring中的使用方式
    spring @Primary在spring的使用 在spring 中使用注解,常使用@Autowired, 默认是根据类型Type来自动注入的。但有些特殊情况,对同一个接口,可能会...
    99+
    2022-11-13
  • @ModelAttribute注解在spring mvc中的使用方法
    @ModelAttribute注解在spring mvc中的使用方法?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。在spring mvc中非常重要的注解@Mod...
    99+
    2023-05-31
    springmvc @modelattribute
  • Spring中@Scheduled功能的使用方法详解
    目录前言一、Spring @Scheduled Annotation1.2 如何启用@Scheduled 注释1.3 使用@Scheduled 注释二、固定的延时和频率使用@Sche...
    99+
    2022-11-13
  • Spring中的refresh方法怎么用
    这篇文章主要介绍“Spring中的refresh方法怎么用”,在日常操作中,相信很多人在Spring中的refresh方法怎么用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Spring中的refresh方法...
    99+
    2023-06-30
  • 在spring boot3中使用native image的最新方法
    目录简介安装GraalVM添加Native Image支持构建spring boot3应用总结简介 在之前spring boot3文章中我们介绍了,spring boot3的一个重要...
    99+
    2023-01-31
    spring boot3使用native image spring boot3 native image
  • spring项目中切面及AOP的使用方法
    使用AOP的原因(AOP简介) 我们知道,spring两大核心,IOC(控制反转)和AOP(切面),那为什么要使用AOP,AOP是什么呢,严格来说,AOP是一种编程规范,是一种编程思...
    99+
    2022-11-12
  • Spring中拦截器的原理与使用方法
    这篇文章主要讲解了“Spring中拦截器的原理与使用方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Spring中拦截器的原理与使用方法”吧!1.Spring中的拦截器在web开发中,拦截...
    99+
    2023-07-02
  • Spring Boot中Thymeleaf使用方法有哪些
    这篇文章主要介绍“Spring Boot中Thymeleaf使用方法有哪些”,在日常操作中,相信很多人在Spring Boot中Thymeleaf使用方法有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”S...
    99+
    2023-06-02
  • mybatis spring配置SqlSessionTemplate的使用方法
    这篇文章主要讲解了“mybatis spring配置SqlSessionTemplate的使用方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“mybatis spring配置SqlSess...
    99+
    2023-06-20
  • Spring Boot 集成PageHelper的使用方法
    目录前言:一、基本集成引入jar包Yml配置文件中添加相关配置封装相关分页方法示例代码前段传入参数执行结果二、分页中的排序字段如何防止SQL注入问题三、复杂的SQL分页语句四、分页失...
    99+
    2022-11-13
  • Spring使用注解开发的方法
    这篇文章主要介绍了Spring使用注解开发的方法的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Spring使用注解开发的方法文章都会有所收获,下面我们一起来看看吧。在Spring4之后 要使用注解开发 必须保证...
    99+
    2023-06-30
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作