{"id":1897,"date":"2019-07-08T07:59:25","date_gmt":"2019-07-07T23:59:25","guid":{"rendered":"https:\/\/coderbee.net\/?p=1897"},"modified":"2019-10-27T15:20:19","modified_gmt":"2019-10-27T07:20:19","slug":"springboot-%e5%90%af%e5%8a%a8%e5%88%86%e6%9e%90%e4%ba%94-%e4%b8%8a%e4%b8%8b%e6%96%87%e7%9a%84%e5%88%b7%e6%96%b0%e8%bf%87%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/framework\/20190708\/1897","title":{"rendered":"SpringBoot \u542f\u52a8\u5206\u6790(\u4e94) &#8212; \u4e0a\u4e0b\u6587\u7684\u5237\u65b0\u8fc7\u7a0b"},"content":{"rendered":"<h1>1. SpringApplication.refreshContext<\/h1>\n<p>\u9996\u5148\u6765\u770b SpringApplication \u91cc\u5237\u65b0\u4e0a\u4e0b\u6587\u7684\u903b\u8f91\uff1a<\/p>\n<pre><code class=\"java\">private void refreshContext(ConfigurableApplicationContext context) {\n    refresh(context);\n    if (this.registerShutdownHook) {\n        try {\n            context.registerShutdownHook();\n        } catch (AccessControlException ex) {\n            \/\/ Not allowed in some environments.\n        }\n    }\n}\n\nprotected void refresh(ApplicationContext applicationContext) {\n    Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);\n    ((AbstractApplicationContext) applicationContext).refresh();\n}\n<\/code><\/pre>\n<p>\u5237\u65b0\u7684\u903b\u8f91\u662f\u5728 <code>AbstractApplicationContext.refresh<\/code> \u65b9\u6cd5\u5b8c\u6210\u7684\uff0c\u5237\u65b0\u5b8c\u540e\u6ce8\u518c\u4e86 JVM \u7684\u5173\u95ed\u56de\u8c03\u94a9\u5b50\u3002<\/p>\n<h1>2. AbstractApplicationContext.refresh<\/h1>\n<p><!--more--><\/p>\n<pre><code class=\"java\">public void refresh() throws BeansException, IllegalStateException {\n    synchronized (this.startupShutdownMonitor) {\n        \/\/ Prepare this context for refreshing.\n        prepareRefresh();\n\n        \/\/ \u8ba9\u5b50\u7c7b\u63d0\u4f9b\u8981\u5237\u65b0\u7684 BeanFactory\n        ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();\n\n        \/\/ Prepare the bean factory for use in this context.\n        prepareBeanFactory(beanFactory);\n\n        try {\n            \/\/ Allows post-processing of the bean factory in context subclasses.\n            postProcessBeanFactory(beanFactory);\n\n            \/\/ Invoke factory processors registered as beans in the context.\n            \/\/ \u6838\u5fc3\u903b\u8f91\uff1a\u8c03\u7528\u4e0a\u4e0b\u6587\u91cc\u4ee5Bean\u5f62\u5f0f\u6ce8\u518c\u7684\u6240\u6709 BeanFactoryPostProcessor\n            invokeBeanFactoryPostProcessors(beanFactory);\n\n            \/\/ Register bean processors that intercept bean creation.\n            \/\/ \u6838\u5fc3\u903b\u8f91\uff1a\u5411beanFactory\u6ce8\u518c BeanPostProcessor\uff0c\n            \/\/ \u7528\u4e8e\u5728\u521b\u5efa bean \u65f6\u56de\u8c03\uff0c\u4ee5\u589e\u5f3a bean\n            registerBeanPostProcessors(beanFactory);\n\n            \/\/ Initialize message source for this context.\n            \/\/ \u521d\u59cb\u5316 MessageSource\n            initMessageSource();\n\n            \/\/ Initialize event multicaster for this context.\n            initApplicationEventMulticaster();\n\n            \/\/ Initialize other special beans in specific context subclasses.\n            onRefresh();\n\n            \/\/ Check for listener beans and register them.\n            registerListeners();\n\n            \/\/ Instantiate all remaining (non-lazy-init) singletons.\n            \/\/ \u6838\u5fc3\u903b\u8f91\uff1a\u5b9e\u4f8b\u5316\u6240\u6709\u975e\u5ef6\u8fdf\u521d\u59cb\u5316\u7684\u5355\u4f8b\n            finishBeanFactoryInitialization(beanFactory);\n\n            \/\/ Last step: publish corresponding event.\n            finishRefresh();\n        }\n\n        catch (BeansException ex) {\n            if (logger.isWarnEnabled()) {\n                logger.warn(\"Exception encountered during context initialization - \" +\n                        \"cancelling refresh attempt: \" + ex);\n            }\n\n            \/\/ Destroy already created singletons to avoid dangling resources.\n            destroyBeans();\n\n            \/\/ Reset 'active' flag.\n            cancelRefresh(ex);\n\n            \/\/ Propagate exception to caller.\n            throw ex;\n        }\n\n        finally {\n            \/\/ Reset common introspection caches in Spring's core, since we\n            \/\/ might not ever need metadata for singleton beans anymore...\n            resetCommonCaches();\n        }\n    }\n}\n<\/code><\/pre>\n<p>\u4e0a\u4e0b\u6587\u5237\u65b0\u7684\u6838\u5fc3\u903b\u8f91\u5728\u4e8e\uff1a<br \/>\n1\u3001 \u8c03\u7528\u4e0a\u4e0b\u6587\u91cc\u4ee5 Bean \u5f62\u5f0f\u6ce8\u518c\u7684\u6240\u6709 BeanFactoryPostProcessor\u3002<\/p>\n<blockquote>\n<p>\u5bf9\u4e8e <code>AnnotationConfigEmbeddedWebApplicationContext<\/code> \u548c <code>AnnotationConfigApplicationContext<\/code> \u4e0a\u4e0b\u6587\uff0c\u5b83\u4eec\u5728\u6784\u5efa\u7684\u65f6\u5019\u4f1a\u521b\u5efa <code>AnnotatedBeanDefinitionReader<\/code>\uff0c<code>AnnotatedBeanDefinitionReader<\/code>\u5728\u6784\u9020\u51fd\u6570\u91cc\u4f1a\u5f80\u4e0a\u4e0b\u6587\u91cc\u6ce8\u518c\u4e00\u4e9b\u57fa\u7840\u7ec4\u4ef6\uff0c\u5f88\u91cd\u8981\u7684\u4e00\u4e2a\u662f<code>ConfigurationClassPostProcessor<\/code>\uff0c\u4f1a\u5728\u8fd9\u91cc\u88ab\u8c03\u7528\uff0c\u7528\u4e8e\u89e3\u6790\u914d\u7f6e\u7c7b\u3001\u52a0\u8f7dbean\u5b9a\u4e49\u7b49\u3002\u5176\u4ed6\u7684\u5177\u4f53\u89c1 <code>AnnotationConfigUtils.registerAnnotationConfigProcessors(BeanDefinitionRegistry registry, Object source)<\/code> \u65b9\u6cd5\u3002<\/p>\n<\/blockquote>\n<p>2\u3001 \u5411 <code>beanFactory<\/code> \u6ce8\u518c <code>BeanPostProcessor<\/code>\uff0c\u4ee5\u8fd9\u4e9b <code>BeanPostProcessor<\/code> \u88ab\u5e94\u7528\u7684\u987a\u5e8f\u6ce8\u518c\u7684\uff1a<\/p>\n<blockquote>\n<ul>\n<li>\n<p>\u4e0d\u540c\u7ec4\u4ef6\u7684\u5b9e\u73b0\u6709\u5982\u4e0b\u4e09\u7c7b\u5206\u7c7b<br \/>\n      1. \u5b9e\u73b0 PriorityOrdered \u63a5\u53e3<br \/>\n      2. \u5b9e\u73b0 Ordered \u63a5\u53e3<br \/>\n      3. \u975e\u7ee7\u627f\u4e0a\u8ff0\u4e24\u4e2a\u63a5\u53e3\u7684<\/p>\n<\/li>\n<li>\n<p>\u6267\u884c\u987a\u5e8f\u89c4\u5219<br \/>\n      1. PriorityOrdered -> Ordered -> \u975e\u7ee7\u627f\u4e0a\u8ff0\u63a5\u53e3\u7684<br \/>\n      2. \u540c\u4e00\u7c7b\u5b9e\u73b0\u7684 order \u503c\u8d8a\u4f4e\u4f18\u5148\u7ea7\u8d8a\u9ad8<\/p>\n<\/li>\n<\/ul>\n<\/blockquote>\n<p>3\u3001 \u5b9e\u4f8b\u5316\u6240\u6709\u975e\u5ef6\u8fdf\u521d\u59cb\u5316\u7684\u5355\u4f8b\u3002<\/p>\n<p>4\u3001\u5982\u679c\u5237\u65b0\u7684\u8fc7\u7a0b\u51fa\u73b0 BeansException \u5f02\u5e38\u5219\u9500\u6bc1\u5df2\u521b\u5efa\u7684 bean\u3001\u628a\u4e0a\u4e0b\u6587\u6807\u8bb0\u4e3a\u672a\u6fc0\u6d3b\u3002<\/p>\n<p>5\u3001\u6700\u7ec8\u6e05\u7406\u7f13\u5b58\u3002<\/p>\n<h2>\u5b9e\u4f8b\u5316\u524d\u7684\u51c6\u5907\u5de5\u4f5c<\/h2>\n<pre><code class=\"java\">protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {\n    \/\/ \u5982\u679cbeanfactory\u91cc\u6ce8\u518c\u6709ConversionService\u5219\u4f7f\u7528\n    if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &amp;&amp;\n            beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {\n        beanFactory.setConversionService(\n                beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));\n    }\n\n    \/\/ \u5982\u679c\u9700\u8981\uff0c\u6dfb\u52a0\u4e00\u4e2aEmbeddedValueResolver\uff0c\u4e3b\u8981\u7528\u4e8e\u5904\u7406\u6ce8\u89e3\u5c5e\u6027\u7684\u503c(${})\n    if (!beanFactory.hasEmbeddedValueResolver()) {\n        beanFactory.addEmbeddedValueResolver(new StringValueResolver() {\n            @Override\n            public String resolveStringValue(String strVal) {\n                return getEnvironment().resolvePlaceholders(strVal);\n            }\n        });\n    }\n\n    \/\/ Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.\n    String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);\n    for (String weaverAwareName : weaverAwareNames) {\n        getBean(weaverAwareName);\n    }\n\n    \/\/ Stop using the temporary ClassLoader for type matching.\n    beanFactory.setTempClassLoader(null);\n\n    \/\/ Allow for caching all bean definition metadata, not expecting further changes.\n    \/\/ \u51bb\u7ed3\u914d\u7f6e\uff0c\u4f7f\u6240\u6709 bean \u5b9a\u4e49\u7684\u5143\u6570\u636e\u4e0d\u518d\u53d8\u5316\uff0c\u4ece\u800c\u53ef\u4ee5\u7f13\u5b58\u8fd9\u4e9b\n    beanFactory.freezeConfiguration();\n\n    \/\/ Instantiate all remaining (non-lazy-init) singletons.\n    \/\/ \u5b9e\u4f8b\u5316\u5269\u4f59\u6240\u6709\u975e\u5ef6\u8fdf\u52a0\u8f7d\u7684\u5355\u4f8b\n    beanFactory.preInstantiateSingletons();\n}\n<\/code><\/pre>\n<p>\u5b9e\u4f8b\u5316 bean \u4e4b\u524d\u7684\u4e3b\u8981\u5de5\u4f5c\uff1a<br \/>\n1. \u6307\u5b9a bean \u7c7b\u578b\u8f6c\u6362\u7c7b ConversionService\uff1b<br \/>\n2. \u51c6\u5907\u4e00\u4e2a EmbeddedValueResolver \u7528\u4e8e\u5904\u7406\u6ce8\u89e3\u91cc\u7684\u503c(<code>#{}\/ ${}<\/code>)\uff1b<br \/>\n3. \u51bb\u7ed3\u914d\u7f6e\uff0c\u4f7f bean \u5b9a\u4e49\u4e0d\u518d\u53d8\u5316\uff0c\u4ece\u800c\u53ef\u4ee5\u7f13\u5b58\u5b83\u7684\u7684\u5143\u6570\u636e\u3002<\/p>\n<hr \/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. SpringApplication.refreshContext \u9996\u5148\u6765\u770b &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/framework\/20190708\/1897\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[255],"tags":[172],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1897"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1897"}],"version-history":[{"count":3,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1897\/revisions"}],"predecessor-version":[{"id":2024,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1897\/revisions\/2024"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}