服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - Spring中bean的生命周期之getSingleton方法

Spring中bean的生命周期之getSingleton方法

2021-09-23 00:38北漂程序员 Java教程

今天给大家带来的是关于Spring的相关知识,文章围绕着Spring中bean的生命周期之getSingleton方法展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下

Spring中bean的生命周期

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

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

Spring中bean的生命周期之getSingleton方法

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

  1. @Override
  2. public void refresh() throws BeansException, IllegalStateException {
  3. synchronized (this.startupShutdownMonitor) {
  4. // Prepare this context for refreshing.
  5. prepareRefresh();
  6.  
  7. // Tell the subclass to refresh the internal bean factory.
  8. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
  9.  
  10. // Prepare the bean factory for use in this context.
  11. prepareBeanFactory(beanFactory);
  12.  
  13. try {
  14. // Allows post-processing of the bean factory in context subclasses.
  15. postProcessBeanFactory(beanFactory);
  16.  
  17. // Invoke factory processors registered as beans in the context.
  18. invokeBeanFactoryPostProcessors(beanFactory);
  19.  
  20. // Register bean processors that intercept bean creation.
  21. registerBeanPostProcessors(beanFactory);
  22.  
  23. // Initialize message source for this context.
  24. initMessageSource();
  25.  
  26. // Initialize event multicaster for this context.
  27. initApplicationEventMulticaster();
  28.  
  29. // Initialize other special beans in specific context subclasses.
  30. onRefresh();
  31.  
  32. // Check for listener beans and register them.
  33. registerListeners();
  34.  
  35. // Instantiate all remaining (non-lazy-init) singletons.
  36. finishBeanFactoryInitialization(beanFactory);
  37.  
  38. // Last step: publish corresponding event.
  39. finishRefresh();
  40. }
  41.  
  42. catch (BeansException ex) {
  43. if (logger.isWarnEnabled()) {
  44. logger.warn("Exception encountered during context initialization - " +
  45. "cancelling refresh attempt: " + ex);
  46. }
  47.  
  48. // Destroy already created singletons to avoid dangling resources.
  49. destroyBeans();
  50.  
  51. // Reset 'active' flag.
  52. cancelRefresh(ex);
  53.  
  54. // Propagate exception to caller.
  55. throw ex;
  56. }
  57.  
  58. finally {
  59. // Reset common introspection caches in Spring's core, since we
  60. // might not ever need metadata for singleton beans anymore...
  61. resetCommonCaches();
  62. }
  63. }
  64. }

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

  1. @Override
  2. public void preInstantiateSingletons() throws BeansException {
  3. if (logger.isTraceEnabled()) {
  4. logger.trace("Pre-instantiating singletons in " + this);
  5. }
  6.  
  7. // Iterate over a copy to allow for init methods which in turn register new bean definitions.
  8. // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
  9. List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
  10.  
  11. // Trigger initialization of all non-lazy singleton beans...
  12. for (String beanName : beanNames) {
  13. RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
  14. if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
  15. if (isFactoryBean(beanName)) {
  16. Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
  17. if (bean instanceof FactoryBean) {
  18. final FactoryBean<?> factory = (FactoryBean<?>) bean;
  19. boolean isEagerInit;
  20. if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
  21. isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
  22. ((SmartFactoryBean<?>) factory)::isEagerInit,
  23. getAccessControlContext());
  24. }
  25. else {
  26. isEagerInit = (factory instanceof SmartFactoryBean &&
  27. ((SmartFactoryBean<?>) factory).isEagerInit());
  28. }
  29. if (isEagerInit) {
  30. getBean(beanName);
  31. }
  32. }
  33. }
  34. else {
  35. getBean(beanName);
  36. }
  37. }
  38. }
  39.  
  40. // Trigger post-initialization callback for all applicable beans...
  41. for (String beanName : beanNames) {
  42. Object singletonInstance = getSingleton(beanName);
  43. if (singletonInstance instanceof SmartInitializingSingleton) {
  44. final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
  45. if (System.getSecurityManager() != null) {
  46. AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
  47. smartSingleton.afterSingletonsInstantiated();
  48. return null;
  49. }, getAccessControlContext());
  50. }
  51. else {
  52. smartSingleton.afterSingletonsInstantiated();
  53. }
  54. }
  55. }
  56. }

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

  1. @Override
  2. public Object getBean(String name) throws BeansException {
  3. return doGetBean(name, null, null, false);
  4. }

doGetBean方法定义如下,

  1. protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
  2. @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
  3.  
  4. final String beanName = transformedBeanName(name);
  5. Object bean;
  6.  
  7. // Eagerly check singleton cache for manually registered singletons.
  8. Object sharedInstance = getSingleton(beanName);
  9. if (sharedInstance != null && args == null) {
  10. if (logger.isTraceEnabled()) {
  11. if (isSingletonCurrentlyInCreation(beanName)) {
  12. logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
  13. "' that is not fully initialized yet - a consequence of a circular reference");
  14. }
  15. else {
  16. logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
  17. }
  18. }
  19. bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
  20. }
  21.  
  22. else {
  23. // Fail if we're already creating this bean instance:
  24. // We're assumably within a circular reference.
  25. if (isPrototypeCurrentlyInCreation(beanName)) {
  26. throw new BeanCurrentlyInCreationException(beanName);
  27. }
  28.  
  29. // Check if bean definition exists in this factory.
  30. BeanFactory parentBeanFactory = getParentBeanFactory();
  31. if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
  32. // Not found -> check parent.
  33. String nameToLookup = originalBeanName(name);
  34. if (parentBeanFactory instanceof AbstractBeanFactory) {
  35. return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
  36. nameToLookup, requiredType, args, typeCheckOnly);
  37. }
  38. else if (args != null) {
  39. // Delegation to parent with explicit args.
  40. return (T) parentBeanFactory.getBean(nameToLookup, args);
  41. }
  42. else if (requiredType != null) {
  43. // No args -> delegate to standard getBean method.
  44. return parentBeanFactory.getBean(nameToLookup, requiredType);
  45. }
  46. else {
  47. return (T) parentBeanFactory.getBean(nameToLookup);
  48. }
  49. }
  50.  
  51. if (!typeCheckOnly) {
  52. markBeanAsCreated(beanName);
  53. }
  54.  
  55. try {
  56. final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
  57. checkMergedBeanDefinition(mbd, beanName, args);
  58.  
  59. // Guarantee initialization of beans that the current bean depends on.
  60. String[] dependsOn = mbd.getDependsOn();
  61. if (dependsOn != null) {
  62. for (String dep : dependsOn) {
  63. if (isDependent(beanName, dep)) {
  64. throw new BeanCreationException(mbd.getResourceDescription(), beanName,
  65. "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'");
  66. }
  67. registerDependentBean(dep, beanName);
  68. try {
  69. getBean(dep);
  70. }
  71. catch (NoSuchBeanDefinitionException ex) {
  72. throw new BeanCreationException(mbd.getResourceDescription(), beanName,
  73. "'" + beanName + "' depends on missing bean '" + dep + "'", ex);
  74. }
  75. }
  76. }
  77.  
  78. // Create bean instance.
  79. if (mbd.isSingleton()) {
  80. sharedInstance = getSingleton(beanName, () -> {
  81. try {
  82. return createBean(beanName, mbd, args);
  83. }
  84. catch (BeansException ex) {
  85. // Explicitly remove instance from singleton cache: It might have been put there
  86. // eagerly by the creation process, to allow for circular reference resolution.
  87. // Also remove any beans that received a temporary reference to the bean.
  88. destroySingleton(beanName);
  89. throw ex;
  90. }
  91. });
  92. bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
  93. }
  94.  
  95. else if (mbd.isPrototype()) {
  96. // It's a prototype -> create a new instance.
  97. Object prototypeInstance = null;
  98. try {
  99. beforePrototypeCreation(beanName);
  100. prototypeInstance = createBean(beanName, mbd, args);
  101. }
  102. finally {
  103. afterPrototypeCreation(beanName);
  104. }
  105. bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
  106. }
  107.  
  108. else {
  109. String scopeName = mbd.getScope();
  110. final Scope scope = this.scopes.get(scopeName);
  111. if (scope == null) {
  112. throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
  113. }
  114. try {
  115. Object scopedInstance = scope.get(beanName, () -> {
  116. beforePrototypeCreation(beanName);
  117. try {
  118. return createBean(beanName, mbd, args);
  119. }
  120. finally {
  121. afterPrototypeCreation(beanName);
  122. }
  123. });
  124. bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
  125. }
  126. catch (IllegalStateException ex) {
  127. throw new BeanCreationException(beanName,
  128. "Scope '" + scopeName + "' is not active for the current thread; consider " +
  129. "defining a scoped proxy for this bean if you intend to refer to it from a singleton",
  130. ex);
  131. }
  132. }
  133. }
  134. catch (BeansException ex) {
  135. cleanupAfterBeanCreationFailure(beanName);
  136. throw ex;
  137. }
  138. }
  139.  
  140. // Check if required type matches the type of the actual bean instance.
  141. if (requiredType != null && !requiredType.isInstance(bean)) {
  142. try {
  143. T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
  144. if (convertedBean == null) {
  145. throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
  146. }
  147. return convertedBean;
  148. }
  149. catch (TypeMismatchException ex) {
  150. if (logger.isTraceEnabled()) {
  151. logger.trace("Failed to convert bean '" + name + "' to required type '" +
  152. ClassUtils.getQualifiedName(requiredType) + "'", ex);
  153. }
  154. throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
  155. }
  156. }
  157. return (T) bean;
  158. }

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

  1. @Nullable
  2. protected Object getSingleton(String beanName, boolean allowEarlyReference) {
  3. Object singletonObject = this.singletonObjects.get(beanName);
  4. if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
  5. synchronized (this.singletonObjects) {
  6. singletonObject = this.earlySingletonObjects.get(beanName);
  7. if (singletonObject == null && allowEarlyReference) {
  8. ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
  9. if (singletonFactory != null) {
  10. singletonObject = singletonFactory.getObject();
  11. this.earlySingletonObjects.put(beanName, singletonObject);
  12. this.singletonFactories.remove(beanName);
  13. }
  14. }
  15. }
  16. }
  17. return singletonObject;
  18. }

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

Spring中bean的生命周期之getSingleton方法

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

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

到此这篇关于Spring中bean的生命周期之getSingleton方法的文章就介绍到这了,更多相关Spring中bean的生命周期内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/teach/p/14951175.html

延伸 · 阅读

精彩推荐