{"id":28,"date":"2025-05-29T14:52:53","date_gmt":"2025-05-29T06:52:53","guid":{"rendered":"http:\/\/byname6.cn\/?p=28"},"modified":"2025-06-30T14:53:32","modified_gmt":"2025-06-30T06:53:32","slug":"springboot%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"http:\/\/byname6.cn\/index.php\/2025\/05\/29\/springboot%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90\/","title":{"rendered":"Springboot\u6e90\u7801\u5206\u6790"},"content":{"rendered":"\n<p>\u7b80\u5355\u6982\u62ec\u4e00\u4e0bSpringboot\u7684\u6e90\u7801\u6d41\u7a0b\u67b6\u6784\uff0c\u4e00\u4e9b\u6bd4\u8f83\u91cd\u8981\u7684\u7c7b\u548c\u7ec6\u8282\u4f1a\u8be6\u7ec6\u5199\u51fa\u6765\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Springboot\u7684\u542f\u52a8\u6d41\u7a0b<\/h1>\n\n\n\n<p>\u6211\u4eec\u77e5\u9053\uff0cSpringboot\u5c31\u662f\u9760\u8fd9\u4e32SpringApplication\u7684\u9759\u6001\u65b9\u6cd5\u4fbf\u542f\u52a8\u4e86web\u670d\u52a1\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SpringApplication.run(DemoApplication.class, args);<\/pre>\n\n\n\n<p>\u5b83\u7684\u5b9e\u73b0\u662f\u5c01\u88c5\u4e86\u4e00\u4e2aSpringApplication\u5bf9\u8c61\uff0c\u5e76\u8c03\u7528run\u65b9\u6cd5\u7684\u91cd\u8f7d\uff1a<code>new SpringApplication(primarySources)).run(args)<\/code>;<\/p>\n\n\n\n<p>\u8fd9\u4e2arun\u65b9\u6cd5\u7684\u6838\u5fc3\u4ee3\u7801\u662f\u5e72\u4e86\u8fd9\u6837\u51e0\u4ef6\u4e8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public ConfigurableApplicationContext run(String... args) {<br> &nbsp; &nbsp;\/\/ \u8ba1\u65f6\u5de5\u5177<br> &nbsp; &nbsp;StopWatch stopWatch = new StopWatch();<br> &nbsp; &nbsp;stopWatch.start();<br> &nbsp; &nbsp;ConfigurableApplicationContext context = null;<br> &nbsp; &nbsp;Collection&lt;SpringBootExceptionReporter&gt; exceptionReporters = new ArrayList&lt;&gt;();<br> &nbsp; &nbsp;configureHeadlessProperty();<br> &nbsp; &nbsp;\/\/ \u7b2c\u4e00\u6b65\uff1a\u83b7\u53d6\u5e76\u542f\u52a8\u76d1\u542c\u5668<br> &nbsp; &nbsp;SpringApplicationRunListeners listeners = getRunListeners(args);<br> &nbsp; &nbsp;listeners.starting();<br> <br> &nbsp; &nbsp;try {<br> &nbsp; &nbsp; &nbsp; &nbsp;ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);<br> &nbsp; &nbsp; &nbsp; &nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u7b2c\u4e8c\u6b65\uff1a\u6839\u636eSpringApplicationRunListeners\u4ee5\u53ca\u53c2\u6570\u6765\u51c6\u5907\u73af\u5883<br> &nbsp; &nbsp; &nbsp; &nbsp;ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);<br> &nbsp; &nbsp; &nbsp; &nbsp;configureIgnoreBeanInfo(environment);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u51c6\u5907Banner\u6253\u5370\u5668 - \u5c31\u662f\u542f\u52a8Spring Boot\u7684\u65f6\u5019\u6253\u5370\u5728console\u4e0a\u7684ASCII\u827a\u672f\u5b57\u4f53<br> &nbsp; &nbsp; &nbsp; &nbsp;Banner printedBanner = printBanner(environment);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u7b2c\u4e09\u6b65\uff1a\u521b\u5efaSpring\u5bb9\u5668<br> &nbsp; &nbsp; &nbsp; &nbsp;context = createApplicationContext();<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;exceptionReporters = getSpringFactoriesInstances(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SpringBootExceptionReporter.class,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new Class[] { ConfigurableApplicationContext.class }, context);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u7b2c\u56db\u6b65\uff1aSpring\u5bb9\u5668\u524d\u7f6e\u5904\u7406<br> &nbsp; &nbsp; &nbsp; &nbsp;prepareContext(context, environment, listeners, applicationArguments,printedBanner);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u7b2c\u4e94\u6b65\uff1a\u5237\u65b0\u5bb9\u5668<br> &nbsp; &nbsp; &nbsp; &nbsp;refreshContext(context);<br>\u200b<br>\u3000\u3000\u3000\u3000 \/\/ \u7b2c\u516d\u6b65\uff1aSpring\u5bb9\u5668\u540e\u7f6e\u5904\u7406<br> &nbsp; &nbsp; &nbsp; &nbsp;afterRefresh(context, applicationArguments);<br>\u200b<br>  \u3000\u3000\u3000 \/\/ \u7b2c\u4e03\u6b65\uff1a\u53d1\u51fa\u7ed3\u675f\u6267\u884c\u7684\u4e8b\u4ef6<br> &nbsp; &nbsp; &nbsp; &nbsp;listeners.started(context);<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u7b2c\u516b\u6b65\uff1a\u6267\u884cRunners<br> &nbsp; &nbsp; &nbsp; &nbsp;this.callRunners(context, applicationArguments);<br> &nbsp; &nbsp; &nbsp; &nbsp;stopWatch.stop();<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u8fd4\u56de\u5bb9\u5668<br> &nbsp; &nbsp; &nbsp; &nbsp;return context;<br> &nbsp;  }<br> &nbsp; &nbsp;catch (Throwable ex) {<br> &nbsp; &nbsp; &nbsp; &nbsp;handleRunFailure(context, listeners, exceptionReporters, ex);<br> &nbsp; &nbsp; &nbsp; &nbsp;throw new IllegalStateException(ex);<br> &nbsp;  }<br>}<\/pre>\n\n\n\n<p>\u8fd9\u91cc\u8fc7\u4e00\u4e0b\u5176\u4e2d\u7684\u6838\u5fc3\u6b65\u9aa4\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u83b7\u53d6\u5e76\u542f\u52a8\u76d1\u542c\u5668(SpringApplicationRunListener)<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">SpringApplicationRunListeners listeners = getRunListeners(args);<br>listeners.starting();<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Springboot\u542f\u52a8\u4e2d\u6d89\u53ca\u5230\u7684\u4e8b\u4ef6\u673a\u5236\u7ec4\u4ef6<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e8b\u4ef6\u6e90 &#8211; <code>SpringApplication<\/code><\/strong><\/li>\n\n\n\n<li><strong>\u4e8b\u4ef6 &#8211; <code>ApplicationEvent<\/code><\/strong><\/li>\n<\/ul>\n\n\n\n<p>SpringApplication\u7684\u751f\u547d\u5468\u671f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u4e8b\u4ef6\u540d<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td>ApplicationStartingEvent<\/td><td>\u6846\u67b6\u542f\u52a8\u4e8b\u4ef6<\/td><\/tr><tr><td>ApplicationEnvironmentPreparedEvent<\/td><td>\u73af\u5883\u51c6\u5907\u5b8c\u6bd5\u4e8b\u4ef6<\/td><\/tr><tr><td>ApplicationContextInitializedEvent<\/td><td>\u4e0a\u4e0b\u6587\u521d\u59cb\u5316<\/td><\/tr><tr><td>ApplicationPreparedEvent<\/td><td>\u4e0a\u4e0b\u6587\u521b\u5efa\u5b8c\u6bd5\uff0c\u4f46\u662fBean\u8fd8\u6ca1\u6709\u52a0\u8f7d\u5b8c\u6bd5<\/td><\/tr><tr><td>ApplicationStartedEvent<\/td><td>bean \u5b9e\u4f8b\u5316\u5b8c\u6210\uff0c\u4f46\u662f\u672a\u8c03\u7528 Runners\u63a5\u53e3<\/td><\/tr><tr><td>ApplicationReadyEvent<\/td><td>\u8c03\u7528 Runners \u63a5\u53e3\u5b8c\u6bd5<\/td><\/tr><tr><td>ApplicationFailedEvent<\/td><td>\u542f\u52a8\u5931\u8d25\u4e8b\u4ef6<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e8b\u4ef6\u53d1\u5e03\u8005 &#8211; <code>ApplicationEventPublisher<\/code> \u53ca <code>ApplicationEventMulticaster<\/code><\/strong><\/li>\n\n\n\n<li><strong>\u76d1\u542c\u5668 &#8211; <code>java.util.EventListener<\/code>\u5b50\u7c7b\u63a5\u53e3<code>ApplicationListener<\/code><\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-preformatted\">public interface ApplicationListener&lt;E extends ApplicationEvent&gt; extends EventListener {<br> &nbsp; &nbsp;void onApplicationEvent(E event);<br> &nbsp; &nbsp;static &lt;T&gt; ApplicationListener&lt;PayloadApplicationEvent&lt;T&gt;&gt; forPayload(Consumer&lt;T&gt; consumer) {<br> &nbsp; &nbsp; &nbsp; &nbsp;return (event) -&gt; {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;consumer.accept(event.getPayload());<br> &nbsp; &nbsp; &nbsp;  };<br> &nbsp;  }<br>}<\/pre>\n\n\n\n<p><strong>\u5b9e\u9645\u4e0aSpringboot\u4e2d\u7528\u5230\u7684Listener\u9ed8\u8ba4\u53ea\u6709<code>EventPublishingRunListener<\/code><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> public class EventPublishingRunListener implements SpringApplicationRunListener, Ordered {<br> &nbsp; &nbsp; &nbsp;private final SpringApplication application;<br> &nbsp; &nbsp; &nbsp;private final String[] args;<br> &nbsp; &nbsp; &nbsp;\/\/\u5e7f\u64ad\u5668<br> &nbsp; &nbsp; &nbsp;private final SimpleApplicationEventMulticaster initialMulticaster;<br> &nbsp;<br> &nbsp; &nbsp; &nbsp;public EventPublishingRunListener(SpringApplication application, String[] args) {<br> &nbsp; &nbsp; &nbsp; &nbsp; this.application = application;<br> &nbsp; &nbsp; &nbsp; &nbsp; this.args = args;<br> &nbsp; &nbsp; &nbsp; &nbsp; this.initialMulticaster = new SimpleApplicationEventMulticaster();<br> &nbsp; &nbsp; &nbsp; &nbsp; Iterator var3 = application.getListeners().iterator();<br> &nbsp; &nbsp; &nbsp; &nbsp; while(var3.hasNext()) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ApplicationListener&lt;?&gt; listener = (ApplicationListener)var3.next();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/\u5c06\u4e0a\u9762\u8bbe\u7f6e\u5230SpringApplication\u7684\u5341\u4e00\u4e2a\u76d1\u542c\u5668\u5168\u90e8\u6dfb\u52a0\u5230SimpleApplicationEventMulticaster\u8fd9\u4e2a\u5e7f\u64ad\u5668\u4e2d<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.initialMulticaster.addApplicationListener(listener);<br> &nbsp; &nbsp; &nbsp; &nbsp; }<br> &nbsp; &nbsp; }<br> &nbsp; &nbsp; \/\/\u7565...<br> }<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u83b7\u53d6SpringApplicationRunListener<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"> private SpringApplicationRunListeners getRunListeners(String[] args) {<br> &nbsp; &nbsp; &nbsp; &nbsp;Class&lt;?&gt;[] types = new Class[]{SpringApplication.class, String[].class};<br> &nbsp; &nbsp; &nbsp; &nbsp;return new SpringApplicationRunListeners(logger,            this.getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args), this.applicationStartup);<br>}<\/pre>\n\n\n\n<p>getRunListeners\u8fd4\u56de\u7684\u662f\u4e00\u4e2a<strong><code>SpringApplicationRunListeners<\/code><\/strong>\uff0c\u5c31\u662f\u5bf9\u83b7\u53d6\u5230\u76d1\u542c\u5668\u7684\u5305\u88c5\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u60f3\u8981\u83b7\u53d6\u7684\u76d1\u542c\u5668\u662fSpringApplicationRunListener.class\u7c7b\u578b\u7684\u76d1\u542c\u5668\uff0c\u770b\u5f97\u51fa\u6765\u5c31\u662f\u5bf9SpringApplication\u6574\u4e2a\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u76d1\u542c\u5668\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u8c03\u7528\u4e86<strong><code>SpringApplication#getSpringFactoriesInstances<\/code><\/strong>\uff0c\u6700\u540e\u83b7\u53d6\u7684\u5c31\u662f\u4e00\u4e2a<strong><code>EventPublishingRunListener<\/code><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250622135046636.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250622135046636.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20250622135046636\"\/><\/div><\/figure>\n\n\n\n<p>\u8fd9\u4e2a\u662fSpringboot\u83b7\u53d6bean\u7684\u4e00\u4e2a\u91cd\u8981\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">private &lt;T&gt; Collection&lt;T&gt; getSpringFactoriesInstances(Class&lt;T&gt; type, Class&lt;?&gt;[] parameterTypes, Object... args) {<br> &nbsp; &nbsp;ClassLoader classLoader = this.getClassLoader();<br> &nbsp; &nbsp;Set&lt;String&gt; names = new LinkedHashSet(SpringFactoriesLoader.loadFactoryNames(type, classLoader));<br> &nbsp; &nbsp;List&lt;T&gt; instances = this.createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);<br> &nbsp; &nbsp;AnnotationAwareOrderComparator.sort(instances);<br> &nbsp; &nbsp;return instances;<br>}<\/pre>\n\n\n\n<p><strong><code>SpringFactoriesLoader.loadFactoryNames(type, classLoader)<\/code>\u8fd9\u4e00\u6b65\uff0c\u5c31\u662f\u6211\u4eec\u719f\u77e5\u7684\uff0c\u4eceMETA-INF\/spring.factories\u8bfb\u53d6bean\u7684\u5168\u9650\u5b9a\u7c7b\u540d<\/strong>\uff1b\u7ed3\u675f\u540e\u518d\u4f20\u7ed9\u4e0b\u9762\u7684\u65b9\u6cd5\u53cd\u5c04\u5b9e\u4f8b\u5316\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u5c31\u662f\u4ece<strong>META-INF\/spring.factories<\/strong>\u4e2d\u8bfb\u53d6Key\u4e3aorg.springframework.boot.<strong>SpringApplicationRunListener<\/strong>\u7684Values\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">org.springframework.boot.SpringApplicationRunListener=\\<br>org.springframework.boot.context.event.EventPublishingRunListener<\/pre>\n\n\n\n<p>\u6240\u4ee5\u83b7\u53d6\u7684<strong><code>SpringApplicationRunListeners<\/code><\/strong>\u4e2d\u53ea\u6709\u8fd9\u4e2a<strong><code>EventPublishingRunListener<\/code><\/strong>\uff0c\u5f53\u7136\u4f60\u4e5f\u53ef\u4ee5\u81ea\u5df1\u6269\u5c55\uff0c\u5e76\u5728<code>META-INF\/spring.factories<\/code>\u4e2d\u81ea\u5df1\u5199\u5165\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u53c8\u51fa\u73b0\u4e86\u4e00\u4e2a\u95ee\u9898\uff1a<strong>\u6211\u4eec\u8fd9\u4e2a\u5982\u4ee5maven\u6784\u5efa\u7684\u9879\u76ee\u76ee\u5f55\u4e2d\u5e76\u6ca1\u6709\u663e\u5f0f\u51fa\u73b0META-INF\u8fd9\u4e2a\u76ee\u5f55\uff0c\u54ea\u8fd9\u4e2a<code>META-INF\/spring.factories<\/code>\u53ca\u5176\u91cc\u9762\u7684\u5185\u5bb9\u662f\u54ea\u6765\u7684\u5462\uff1f<\/strong><\/p>\n\n\n\n<p>\u5176\u5b9e\uff0c\u5728pom.xml\u5728\u5f15\u5165\u7684<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &lt;dependency&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;<br> &nbsp; &nbsp;&lt;\/dependency&gt;<\/pre>\n\n\n\n<p>\u4f9d\u8d56\u7ec4\u4e2d\uff0c\u5305\u542b\u4e86\u4e00\u9879\u540d\u4e3a<strong><code>spring-boot-autoconfigure-x.x.x<\/code><\/strong>\u7684\u4f9d\u8d56\uff0c<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250622140553205.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250622140553205.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20250622140553205\"\/><\/div><\/figure>\n\n\n\n<p>\u8fd9\u4e2ajar\u5305\u4e2d\u5c31\u5305\u542b\u4e86Springboot\u542f\u52a8\u6240\u9700\u7684<strong><code>META-INF\/spring.factories<\/code><\/strong>\u7b49\u4e00\u7cfb\u5217\u914d\u7f6e\uff0c\u5f53\u7136\uff0c\u5e76\u4e0d\u4f1a\u4e0e\u9879\u76ee\u76ee\u5f55\u4e2d\u624b\u52a8\u5efa\u7acb\u7684<strong><code>META-INF\/spring.factories<\/code><\/strong>\u51b2\u7a81\uff0c\u4e24\u4e2a\u6587\u4ef6\u4f1a\u5408\u5e76\u89e3\u6790\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u542f\u52a8SpringApplicationRunListener<\/h3>\n\n\n\n<p>\u8981\u4e86\u89e3\u4e00\u4e2a\u903b\u8f91\u590d\u6742\u7684\u7c7b\u5c31\u5148\u53bb\u4e86\u89e3\u5b83\u7684\u63a5\u53e3\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class EventPublishingRunListener implements SpringApplicationRunListener, Ordered {<br> &nbsp; &nbsp;private final SpringApplication application;<br> &nbsp; &nbsp;private final String[] args;<br> &nbsp; &nbsp;private final SimpleApplicationEventMulticaster initialMulticaster;<br> &nbsp;  ...<br>}<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">public interface SpringApplicationRunListener {<br>\u200b<br> &nbsp; &nbsp;\/\/ \u5728run()\u65b9\u6cd5\u5f00\u59cb\u6267\u884c\u65f6\uff0c\u8be5\u65b9\u6cd5\u5c31\u7acb\u5373\u88ab\u8c03\u7528\uff0c\u53ef\u7528\u4e8e\u5728\u521d\u59cb\u5316\u6700\u65e9\u671f\u65f6\u505a\u4e00\u4e9b\u5de5\u4f5c<br> &nbsp; &nbsp;void starting();<br> &nbsp; &nbsp;\/\/ \u5f53environment\u6784\u5efa\u5b8c\u6210\uff0cApplicationContext\u521b\u5efa\u4e4b\u524d\uff0c\u8be5\u65b9\u6cd5\u88ab\u8c03\u7528<br> &nbsp; &nbsp;void environmentPrepared(ConfigurableEnvironment environment);<br> &nbsp; &nbsp;\/\/ \u5f53ApplicationContext\u6784\u5efa\u5b8c\u6210\u65f6\uff0c\u8be5\u65b9\u6cd5\u88ab\u8c03\u7528<br> &nbsp; &nbsp;void contextPrepared(ConfigurableApplicationContext context);<br> &nbsp; &nbsp;\/\/ \u5728ApplicationContext\u5b8c\u6210\u52a0\u8f7d\uff0c\u4f46\u6ca1\u6709\u88ab\u5237\u65b0\u524d\uff0c\u8be5\u65b9\u6cd5\u88ab\u8c03\u7528<br> &nbsp; &nbsp;void contextLoaded(ConfigurableApplicationContext context);<br> &nbsp; &nbsp;\/\/ \u5728ApplicationContext\u5237\u65b0\u5e76\u542f\u52a8\u540e\uff0cCommandLineRunners\u548cApplicationRunner\u672a\u88ab\u8c03\u7528\u524d\uff0c\u8be5\u65b9\u6cd5\u88ab\u8c03\u7528<br> &nbsp; &nbsp;void started(ConfigurableApplicationContext context);<br> &nbsp; &nbsp;\/\/ \u5728run()\u65b9\u6cd5\u6267\u884c\u5b8c\u6210\u524d\u8be5\u65b9\u6cd5\u88ab\u8c03\u7528<br> &nbsp; &nbsp;void running(ConfigurableApplicationContext context);<br> &nbsp; &nbsp;\/\/ \u5f53\u5e94\u7528\u8fd0\u884c\u51fa\u9519\u65f6\u8be5\u65b9\u6cd5\u88ab\u8c03\u7528<br> &nbsp; &nbsp;void failed(ConfigurableApplicationContext context, Throwable exception);<br>}<\/pre>\n\n\n\n<p>\u5148\u6765\u770b\u770bSpringBoot\u542f\u52a8\u65f6\u7b2c\u4e00\u4e2a\u542f\u52a8\u4e8b\u4ef6listeners.starting()\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@Override<br>public void starting() {<br> &nbsp; &nbsp;\/\/\u5173\u952e\u4ee3\u7801\uff0c\u5148\u521b\u5efaapplication\u542f\u52a8\u4e8b\u4ef6`ApplicationStartingEvent`<br> &nbsp; &nbsp;this.initialMulticaster.multicastEvent(new ApplicationStartingEvent(this.application, this.args));<br>}<\/pre>\n\n\n\n<p>\u8c03\u7528\u5c01\u88c5\u597d\u7684<strong><code>SimpleApplicationEventMulticaster<\/code><\/strong>\u5bf9\u8c61\u7684multicastEvent\uff0c\u51c6\u5907\u5229\u7528\u5e7f\u64ad\u5668\u53d1\u5e03\u4e8b\u4ef6\u5e76\u53c8\u542f\u7528\u76d1\u542c\u5668\u3002\u5176\u5927\u90e8\u5206\u5bf9\u4e8b\u4ef6\u7684\u5904\u7406\u90fd\u662f\u8c03\u7528Multicaster\uff0c\u53ef\u89c1\u5f97\u8fd9\u4e2a\u76d1\u542c\u5668\u7684\u804c\u8d23\u66f4\u50cf\u662f\u4f5c\u4e3a<code>Multicaster<\/code>\u7684\u8865\u5145\u3002<\/p>\n\n\n\n<p>\u8ddf\u8fdbSimpleApplicationEventMulticaster\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@Override<br>public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {<br> &nbsp; &nbsp;ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));<br> &nbsp; &nbsp;\/\/\u901a\u8fc7\u4e8b\u4ef6\u7c7b\u578bApplicationStartingEvent\u83b7\u53d6\u5bf9\u5e94\u7684\u76d1\u542c\u5668<br> &nbsp; &nbsp;for (final ApplicationListener&lt;?&gt; listener : getApplicationListeners(event, type)) {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u83b7\u53d6\u7ebf\u7a0b\u6c60\uff0c\u5982\u679c\u4e3a\u7a7a\u5219\u540c\u6b65\u5904\u7406\u3002\u8fd9\u91cc\u7ebf\u7a0b\u6c60\u4e3a\u7a7a\uff0c\u8fd8\u672a\u6ca1\u521d\u59cb\u5316\u3002<br> &nbsp; &nbsp; &nbsp; &nbsp;Executor executor = getTaskExecutor();<br> &nbsp; &nbsp; &nbsp; &nbsp;if (executor != null) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u5f02\u6b65\u53d1\u9001\u4e8b\u4ef6<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;executor.execute(() -&gt; invokeListener(listener, event));<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;else {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u540c\u6b65\u53d1\u9001\u4e8b\u4ef6<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;invokeListener(listener, event);<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>}<\/pre>\n\n\n\n<p>\u8fd9\u4e2a<strong><code>getApplicationListeners<\/code><\/strong>\u65b9\u6cd5\u4f1a\u628a\u5165\u53c2\u4e8b\u4ef6\u7684.class\u53d6\u51fa\u6765\uff0c\u5e76\u770b\u54ea\u4e9b\u76d1\u542c\u5668(\u76d1\u542c\u5668\u4e5f\u662f\u4ece<strong><code>META-INF\/spring.factories<\/code><\/strong>\u4e2d\u53d6\u51fa\u5b9e\u4f8b\u5316)\u5bf9\u8fd9\u4e2a\u4e8b\u4ef6event\u548c\u4e8b\u4ef6\u6e90source&#8221;\u611f\u5174\u8da3&#8221;\uff08\u5728\u6bcf\u4e2aListener\u4e2d\u5b9a\u4e49\u4e86\u5217\u8868\uff0c\u5bf9\u5e94\u5177\u4f53\u4e8b\u4ef6\u3001\u4e8b\u4ef6\u6e90\u7c7b\uff0c\u4f20\u5165\u7684\u4e8b\u4ef6\u5728\u8fd9\u5217\u8868\u91cc\u9762\u5c31\u8868\u793a\u76d1\u542c\u5668\u5bf9\u5176&#8221;\u611f\u5174\u8da3&#8221;\uff09\u5e76\u5c06\u6240\u6709\u5bf9\u8be5\u4e8b\u4ef6&#8221;\u611f\u5174\u8da3&#8221;\u7684\u76d1\u542c\u5668\u8fd4\u56de\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u5c31\u6709\u5982\u4e0b4\u79cd\u76d1\u542c\u5668\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250622144531608.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250622144531608.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20250622144531608\"\/><\/div><\/figure>\n\n\n\n<p>\u4ee5\u65e5\u5fd7\u76d1\u542c\u5668\uff1aLoggingApplicationListener\u4e3a\u4f8b\uff1a<\/p>\n\n\n\n<p>\u8fd9\u5c31\u662f\u63d0\u5230\u7684\u652f\u6301\u7684source\u548ctype\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250622144756408.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250622144756408.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20250622144756408\"\/><\/div><\/figure>\n\n\n\n<p>\u8fd9\u662f\u5176\u6700\u540e\u8c03\u7528\u7684\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public void onApplicationEvent(ApplicationEvent event) {<br> &nbsp; &nbsp;\/\/\u5728springboot\u542f\u52a8\u7684\u65f6\u5019<br> &nbsp; &nbsp;if (event instanceof ApplicationStartedEvent) {<br> &nbsp; &nbsp; &nbsp; &nbsp;onApplicationStartedEvent((ApplicationStartedEvent) event);<br> &nbsp;  }<br> &nbsp; &nbsp;\/\/springboot\u7684Environment\u73af\u5883\u51c6\u5907\u5b8c\u6210\u7684\u65f6\u5019<br> &nbsp; &nbsp;else if (event instanceof ApplicationEnvironmentPreparedEvent) {<br> &nbsp; &nbsp; &nbsp; &nbsp;onApplicationEnvironmentPreparedEvent(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  (ApplicationEnvironmentPreparedEvent) event);<br> &nbsp;  }<br> &nbsp; &nbsp;\/\/\u5728springboot\u5bb9\u5668\u7684\u73af\u5883\u8bbe\u7f6e\u5b8c\u6210\u4ee5\u540e<br> &nbsp; &nbsp;else if (event instanceof ApplicationPreparedEvent) {<br> &nbsp; &nbsp; &nbsp; &nbsp;onApplicationPreparedEvent((ApplicationPreparedEvent) event);<br> &nbsp;  }<br> &nbsp; &nbsp;\/\/\u5bb9\u5668\u5173\u95ed\u7684\u65f6\u5019<br> &nbsp; &nbsp;else if (event instanceof ContextClosedEvent &amp;&amp; ((ContextClosedEvent) event)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  .getApplicationContext().getParent() == null) {<br> &nbsp; &nbsp; &nbsp; &nbsp;onContextClosedEvent();<br> &nbsp;  }<br> &nbsp; &nbsp;\/\/\u5bb9\u5668\u542f\u52a8\u5931\u8d25\u7684\u65f6\u5019<br> &nbsp; &nbsp;else if (event instanceof ApplicationFailedEvent) {<br> &nbsp; &nbsp; &nbsp; &nbsp;onApplicationFailedEvent();<br> &nbsp;  }<br>}<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u7c7b\u5c31\u662f\u5728springboot\u542f\u52a8\u65f6\u65e5\u5fd7\u8f93\u51fa\u7684\u5b9e\u73b0\u7c7b\u3002<\/p>\n\n\n\n<p>\u81f3\u6b64<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp; &nbsp;SpringApplicationRunListeners listeners = getRunListeners(args);<br> &nbsp; &nbsp;listeners.starting();<\/pre>\n\n\n\n<p>\u8fd9\u4e24\u4e32\u5c31\u4ecb\u7ecd\u5b8c\u4e86\u3002\u8fd9\u4e2aEventPublishingRunListener\u4f1a\u8d2f\u7a7f\u5728SpringApplication\u6574\u4e2a\u8fd0\u884c\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u73af\u5883\u51c6\u5907<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp;ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);<\/pre>\n\n\n\n<p>SpringApplication\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">private ConfigurableEnvironment prepareEnvironment(<br> &nbsp; &nbsp; &nbsp; &nbsp;SpringApplicationRunListeners listeners,<br> &nbsp; &nbsp; &nbsp; &nbsp;ApplicationArguments applicationArguments) {<br> &nbsp; &nbsp;\/\/\u83b7\u53d6\u5bf9\u5e94\u7684ConfigurableEnvironment<br> &nbsp; &nbsp;ConfigurableEnvironment environment = getOrCreateEnvironment();<br> &nbsp; &nbsp;configureEnvironment(environment, applicationArguments.getSourceArgs());<br> &nbsp; &nbsp;\/\/\u53d1\u5e03\u73af\u5883\u5df2\u51c6\u5907\u4e8b\u4ef6\uff0c\u8fd9\u662f\u7b2c\u4e8c\u6b21\u53d1\u5e03\u4e8b\u4ef6<br> &nbsp; &nbsp;listeners.environmentPrepared(environment);<br> &nbsp; &nbsp;bindToSpringApplication(environment);<br> &nbsp; &nbsp;ConfigurationPropertySources.attach(environment);<br> &nbsp; &nbsp;return environment;<br>}<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u8fd8\u662f\u5229\u7528EventPublishingRunListener\u53bb\u53d1\u5e03\u4e8b\u4ef6\uff0c\u90a3\u4e48\u5c31\u7ee7\u7eed\u8ddf\u8fdb\u5230\u5176\u5185\u90e8\u7684<strong><code>SimpleApplicationEventMulticaster#multicast<\/code><\/strong>\uff0c\u4f1a\u8c03\u7528\u5230\u7684\u76d1\u542c\u5668\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250623153555549.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250623153555549.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20250623153555549\"\/><\/div><\/figure>\n\n\n\n<p>\u5176\u4e2d\uff0c\u91cd\u70b9\u6765\u770b<strong><code>ConfigFileApplicationListener<\/code><\/strong>\uff0c\u8be5\u76d1\u542c\u5668\u975e\u5e38\u6838\u5fc3\uff0c\u4e3b\u8981\u7528\u6765\u5904\u7406\u9879\u76ee\u914d\u7f6e\u3002\u9879\u76ee\u4e2d\u7684 properties \u548cyml\u6587\u4ef6\u90fd\u662f\u5176\u5185\u90e8\u7c7b\u6240\u52a0\u8f7d\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250623154407103.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250623154407103.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20250623154407103\"\/><\/div><\/figure>\n\n\n\n<p>\u5148\u4ece<code>META-INF\/spring.factories<\/code>\u4e2d\u8bfb\u53d6\u51e0\u4e2aProcessors\uff0c\u518d\u6267\u884c\u5176postProcessEnvironment\uff0c\u6700\u540e\u6267\u884c\u8be5\u76d1\u542c\u5668\u672c\u8eab\u7684\u903b\u8f91\uff0c\u5373\u52a0\u8f7d\u5176\u5b9a\u4e49\u8def\u5f84\u4e0b\u7684\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">private static final String DEFAULT_SEARCH_LOCATIONS = \"classpath:\/,classpath:\/config\/,file:.\/,file:.\/config\/\";<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5728\u53d1\u5e03\u4e8b\u4ef6\u5904\u7406\u7ed3\u675f\u540e\uff0c<strong>environment<\/strong>\u53d8\u91cf\u5b58\u653e\u4e86\u6211\u4eec\u5728<strong><code>resources\\application.properties<\/code><\/strong>\u914d\u7f6e\u7684\u53c2\u6570\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250623170329712.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20250623170329712.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20250623170329712\"\/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u5e94\u7528\u4e0a\u4e0b\u6587\u7684\u521d\u59cb\u5316\u3001\u540e\u5904\u7406<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">    context = createApplicationContext();<br>    context.setApplicationStartup(this.applicationStartup);<br>    prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);<br>    afterRefresh(context, applicationArguments);<\/pre>\n\n\n\n<p>\u8fd9\u91cc\u7684ApplicationContext\u4e5f\u5e38\u88ab\u6210\u4e3aSpring\u5bb9\u5668\uff0c\u5b83\u662f\u57fa\u4e8e Spring \u6846\u67b6\u5b9a\u4e49\u7684 <strong>IoC \u5bb9\u5668<\/strong>\uff0c\u662f\u8d1f\u8d23 Bean \u751f\u547d\u5468\u671f\u7ba1\u7406\u7684\u6838\u5fc3\u7ec4\u4ef6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.createApplicationContext<\/h3>\n\n\n\n<p>\u5148\u6839\u636e\u5bb9\u5668\u7c7b\u578b\u5224\u65ad\uff0c\u6b64\u5904\u662f<code>SERVLET<\/code>\u7c7b\u578b\uff0c\u6240\u4ee5\u4f1a\u901a\u8fc7\u53cd\u5c04\u88c5\u8f7d\u5bf9\u5e94\u7684\u5b57\u8282\u7801\uff0c\u4e5f\u5c31\u662f<strong>AnnotationConfigServletWebServerApplicationContext<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.prepareContext\uff08\u521d\u59cb\u53161\uff09<\/h3>\n\n\n\n<p>\u7136\u540eprepareContext\u5f00\u59cb\u914d\u7f6e\u5bb9\u5668<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">private void prepareContext(ConfigurableApplicationContext context,<br> &nbsp; &nbsp; &nbsp; &nbsp;ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,<br> &nbsp; &nbsp; &nbsp; &nbsp;ApplicationArguments applicationArguments, Banner printedBanner) {<br> &nbsp; &nbsp;\/\/\u8bbe\u7f6e\u5bb9\u5668\u73af\u5883\uff0c\u5305\u62ec\u5404\u79cd\u53d8\u91cf<br> &nbsp; &nbsp;context.setEnvironment(environment);<br> &nbsp; &nbsp;\/\/\u6267\u884c\u5bb9\u5668\u540e\u7f6e\u5904\u7406<br> &nbsp; &nbsp;postProcessApplicationContext(context);<br> &nbsp; &nbsp;\/\/\u6267\u884c\u5bb9\u5668\u4e2d\u7684ApplicationContextInitializer\uff08\u5305\u62ec spring.factories\u548c\u81ea\u5b9a\u4e49\u7684\u5b9e\u4f8b\uff09<br> &nbsp; &nbsp;applyInitializers(context);<br>\u3000\u3000\/\/\u53d1\u9001\u5bb9\u5668\u5df2\u7ecf\u51c6\u5907\u597d\u7684\u4e8b\u4ef6\uff0c\u901a\u77e5\u5404\u76d1\u542c\u5668<br> &nbsp; &nbsp;listeners.contextPrepared(context);<br> &nbsp; &nbsp;\/\/\u6ce8\u518c\u542f\u52a8\u53c2\u6570bean\uff0c\u8fd9\u91cc\u5c06\u5bb9\u5668\u6307\u5b9a\u7684\u53c2\u6570\u5c01\u88c5\u6210bean\uff0c\u6ce8\u5165\u5bb9\u5668<br> &nbsp; &nbsp;context.getBeanFactory().registerSingleton(\"springApplicationArguments\",<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;applicationArguments);<br> &nbsp; &nbsp;\/\/\u8bbe\u7f6ebanner<br> &nbsp; &nbsp;if (printedBanner != null) {<br> &nbsp; &nbsp; &nbsp; &nbsp;context.getBeanFactory().registerSingleton(\"springBootBanner\", printedBanner);<br> &nbsp;  }<br> &nbsp; &nbsp;\/\/\u83b7\u53d6\u6211\u4eec\u7684\u542f\u52a8\u7c7b\u6307\u5b9a\u7684\u53c2\u6570\uff0c\u53ef\u4ee5\u662f\u591a\u4e2a<br> &nbsp; &nbsp;Set&lt;Object&gt; sources = getAllSources();<br> &nbsp; &nbsp;Assert.notEmpty(sources, \"Sources must not be empty\");<br> &nbsp; &nbsp;\/\/\u52a0\u8f7d\u6211\u4eec\u7684\u542f\u52a8\u7c7b\uff0c\u5c06\u542f\u52a8\u7c7b\u6ce8\u5165\u5bb9\u5668<br> &nbsp; &nbsp;load(context, sources.toArray(new Object[0]));<br> &nbsp; &nbsp;\/\/\u53d1\u5e03\u5bb9\u5668\u5df2\u52a0\u8f7d\u4e8b\u4ef6\u3002<br> &nbsp; &nbsp;listeners.contextLoaded(context);<br>}<\/pre>\n\n\n\n<p>\u8c03\u7528postProcess\u3001\u5faa\u73af\u8c03\u7528ApplicationContextInitializer\u4e2d\u7684initialize\u65b9\u6cd5\u3001<strong>\u5c06\u542f\u52a8\u7c7b\u6ce8\u5165\u5bb9\u5668<\/strong>\u3001\u53d1\u5e03\u5bb9\u5668\u5df2\u52a0\u8f7d(\u6253\u5370\u65e5\u5fd7)\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3.refresh\uff08\u521d\u59cb\u53162\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">public void refresh() throws BeansException, IllegalStateException {<br> &nbsp; &nbsp;synchronized (this.startupShutdownMonitor) {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u542f\u52a8\u6b65\u9aa4\u6807\u8bb0<br> &nbsp; &nbsp; &nbsp; &nbsp;StartupStep contextRefresh = this.applicationStartup.start(\"spring.context.refresh\");<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u51c6\u5907\u5237\u65b0 Prepare this context for refreshing.<br> &nbsp; &nbsp; &nbsp; &nbsp;prepareRefresh();<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u901a\u77e5\u5b50\u7c7b\u5237\u65b0\u5185\u90e8bean\u5de5\u5382 Tell the subclass to refresh the internal bean factory.<br> &nbsp; &nbsp; &nbsp; &nbsp;ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u51c6\u5907\u5de5\u5382\u4ee5\u4fbf\u5728\u6b64\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528 Prepare the bean factory for use in this context.<br> &nbsp; &nbsp; &nbsp; &nbsp;prepareBeanFactory(beanFactory);<br> &nbsp; &nbsp; &nbsp; &nbsp;try {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Allows post-processing of the bean factory in context subclasses.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Bean\u5de5\u5382\u540e\u7f6e\u5904\u7406<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;postProcessBeanFactory(beanFactory);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u6b65\u9aa4\u6807\u8bb0<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StartupStep beanPostProcess = this.applicationStartup.start(\"spring.context.beans.post-process\");<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Invoke factory processors registered as beans in the context.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u6267\u884cBean\u5de5\u5382\u540e\u7f6e\u5904\u7406 \u6838\u5fc3\u5927\u54e5\u6709\u4e2a\u662fConfigurationClassPostProcessor @Configuration @ComponentScan\u90fd\u662f\u8fd9\u5927\u54e5<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;invokeBeanFactoryPostProcessors(beanFactory);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u6ce8\u518cBean \u540e\u7f6e\u5904\u7406\u5668 Register bean processors that intercept bean creation.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;registerBeanPostProcessors(beanFactory);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;beanPostProcess.end();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u56fd\u9645\u5316 Initialize message source for this context.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;initMessageSource();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u6ce8\u518c\u4e8b\u4ef6\u53d1\u5e03\u5668 Initialize event multicaster for this context.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;initApplicationEventMulticaster();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Initialize other special beans in specific context subclasses.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u8fd9\u91cc\u5bf9\u4e8eSpringBoot\u4e3b\u8981\u5c31\u662f\u521b\u5efaWebServer<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;onRefresh();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u6ce8\u518c\u76d1\u542c\u5668 Check for listener beans and register them.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;registerListeners();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u521d\u59cb\u5316\u975e\u5ef6\u8fdf\u52a0\u8f7dBean Instantiate all remaining (non-lazy-init) singletons.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;finishBeanFactoryInitialization(beanFactory);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u5b8c\u6210\u5237\u65b0 Last step: publish corresponding event.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;finishRefresh();<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;catch (BeansException ex) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (logger.isWarnEnabled()) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;logger.warn(\"Exception encountered during context initialization - \" +<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"cancelling refresh attempt: \" + ex);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u9500\u6bc1Bean Destroy already created singletons to avoid dangling resources.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;destroyBeans();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u53d6\u6d88\u5237\u65b0 Reset 'active' flag.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;cancelRefresh(ex);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Propagate exception to caller.<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw ex;<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;finally {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Reset common introspection caches in Spring's core, since we<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ might not ever need metadata for singleton beans anymore...<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resetCommonCaches();<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u4e0a\u4e0b\u6587\u5237\u65b0\u7ed3\u675f<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;contextRefresh.end();<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>}<\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u662fspring\u67b6\u6784\u4e2d\u7684\u6838\u5fc3\uff0c<strong>\u662f\u5b9e\u73b0ioc\u548caop\u7684\u5173\u952e<\/strong>\uff1b\u8d1f\u8d23\u5237\u65b0\u5e94\u7528\u7a0b\u5e8f\u4e0a\u4e0b\u6587\uff0c\u8fd9\u91cc\u4e3b\u8981\u6d89\u53ca\u5230\u51c6\u5907\u5237\u65b0\u4e0a\u4e0b\u6587\uff0c\u8c03\u7528\u4e0a\u4e0b\u6587\u6ce8\u518c\u4e3a bean \u7684\u5de5\u5382\u5904\u7406\u5668\uff0c\u521d\u59cb\u5316\u4e0a\u4e0b\u6587\u7684\u6d88\u606f\u6e90\uff0c\u521d\u59cb\u5316\u7279\u5b9a\u4e0a\u4e0b\u6587\u5b50\u7c7b\u4e2d\u7684\u5176\u4ed6\u7279\u6b8a bean\uff0c\u68c0\u67e5\u76d1\u542c\u5668 bean \u5e76\u6ce8\u518c\uff0c\u6700\u540e\u53d1\u5e03\u76f8\u5e94\u7684\u4e8b\u4ef6\u5e76\u9500\u6bc1\u5df2\u7ecf\u521b\u5efa\u7684\u5355\u4f8b\u53ca\u91cd\u7f6e active \u6807\u5fd7\u3002<\/p>\n\n\n\n<p>\u5927\u6982\u505a\u4e86\u8fd9\u4e9b\u4e8b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u51c6\u5907\u5237\u65b0<\/strong>\uff08<code>prepareRefresh()<\/code>\uff09\u2014\u2014 \u521d\u59cb\u5316\u73af\u5883\u53d8\u91cf\u3001\u542f\u52a8\u65f6\u95f4\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u83b7\u53d6 <code>BeanFactory<\/code><\/strong>\uff08<code>obtainFreshBeanFactory()<\/code>\uff09\u2014\u2014 \u53ea\u662f\u5c06<code>DefaultListableBeanFactory<\/code> Bean \u5de5\u5382\u5237\u65b0\u72b6\u6001\u4e3a\u5df2\u5237\u65b0\u3002<\/li>\n\n\n\n<li><strong>\u914d\u7f6e <code>BeanFactory<\/code><\/strong>\uff08<code>prepareBeanFactory()<\/code>\uff09\u2014\u2014 \u83b7\u53d6\u5230Bean \u5de5\u5382\u540e\uff0c\u5f00\u59cb\u51c6\u5907Bean \u5de5\u5382\uff0c\u4e3b\u8981\u662f\u8fdb\u884c\u529f\u80fd\u6269\u5c55\uff0c\u5165\u8bbe\u7f6e\u7c7b\u52a0\u8f7d\u5668\u3001Aware \u63a5\u53e3\u652f\u6301\u7b49\uff0c\u8fd9\u91cc<strong>\u8fd8\u8bbe\u7f6e\u4e86SPEL \u8868\u8fbe\u5f0f\u89e3\u6790\u5668<\/strong>\u3002<\/li>\n\n\n\n<li><strong>\u6267\u884c Bean\u5de5\u5382\u540e\u7f6e\u5904\u7406\u5668 <\/strong>\uff08<code>invokeBeanFactoryPostProcessors()<\/code>\uff09\u2014\u2014 \u4fee\u6539\u6216\u589e\u5f3a Bean \u5b9a\u4e49\uff08\u5982 <code>@Configuration<\/code> \u7c7b\u5904\u7406\uff09\u3002<\/li>\n\n\n\n<li><strong>\u6ce8\u518c Bean \u540e\u7f6e\u5904\u7406\u5668<\/strong>\uff08<code>registerBeanPostProcessors()<\/code>\uff09\u2014\u2014 \u4e3b\u8981\u662f\u5c06\u8fd9\u4e9b\u540e\u7f6e\u5904\u7406\u5668\u8fdb\u884c\u5206\u7c7b\uff0c\u5e76\u6dfb\u52a0\u5230Bean \u5de5\u5382\u4e2d\uff09\u3002<\/li>\n\n\n\n<li><strong>\u521d\u59cb\u5316\u4e8b\u4ef6\u5e7f\u64ad\u5668\u3001\u6d88\u606f\u6e90\u7b49<\/strong>\uff08<code>initMessageSource()<\/code>, <code>initApplicationEventMulticaster()<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u521d\u59cb\u5316\u975e\u61d2\u52a0\u8f7d\u7684\u5355\u4f8b Bean<\/strong>\uff08<code>finishBeanFactoryInitialization()<\/code>\uff09\u2014\u2014 \u89e6\u53d1\u4f9d\u8d56\u6ce8\u5165\u548c\u521d\u59cb\u5316\u3002<\/li>\n\n\n\n<li><strong>\u5b8c\u6210\u5237\u65b0<\/strong>\uff08<code>finishRefresh()<\/code>\uff09\u2014\u2014 \u53d1\u5e03 <code>ContextRefreshedEvent<\/code> \u4e8b\u4ef6\uff0c\u6807\u5fd7\u5bb9\u5668\u5c31\u7eea\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">4.afterRefresh\uff08\u540e\u5904\u7406\uff09<\/h3>\n\n\n\n<p>\u6269\u5c55\u63a5\u53e3\uff0c\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7684\u6a21\u677f\u65b9\u6cd5\uff0c\u9ed8\u8ba4\u4e3a\u7a7a\u5b9e\u73b0\u3002\u5982\u679c\u6709\u81ea\u5b9a\u4e49\u9700\u6c42\uff0c\u53ef\u4ee5\u91cd\u5199\u8be5\u65b9\u6cd5\u3002\u6bd4\u5982\u6253\u5370\u4e00\u4e9b\u542f\u52a8\u7ed3\u675flog\uff0c\u6216\u8005\u4e00\u4e9b\u5176\u5b83\u540e\u7f6e\u5904\u7406\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Bean\u7684\u6ce8\u518c\u3001\u5b9e\u4f8b\u5316\u3001\u521d\u59cb\u5316<\/h3>\n\n\n\n<p>\u5e94\u7528\u4e0a\u4e0b\u6587\u7684\u542f\u52a8\u4e2d\u4e5f\u5305\u62ec\u4e86\u5bf9Bean\u7684\u6ce8\u518c\u4ee5\u53ca\u5b9e\u4f8b\u5316\uff0c\u7531\u4e8e\u6bd4\u8f83\u91cd\u8981\uff0c\u8fd9\u91cc\u5c31\u5355\u72ec\u5c06\u5bf9Bean\u7684Bean\u7684\u6ce8\u518c\u3001\u5b9e\u4f8b\u5316\u3001\u521d\u59cb\u5316\u63d0\u51fa\u6765\u8bf4\u4e0b\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u626b\u63cf\u4e3b\u7c7b<\/strong><\/h4>\n\n\n\n<p>\u5728\u914d\u7f6e\u5bb9\u5668<strong>\uff08prepareContext\uff09<\/strong>\u7684<strong><code>load(context, sources.toArray(new Object[0]));<\/code><\/strong>\u8fd9\u4e00\u6b65\uff0c\u8c03\u7528\u4e86<strong><code>BeanDefinitionLoader#load<\/code><\/strong>\uff0c<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">private void load(Object source) {<br>        Assert.notNull(source, \"Source must not be null\");<br>        if (source instanceof Class&lt;?&gt;) {<br>            load((Class&lt;?&gt;) source);<br>            return;<br>        }<br>        if (source instanceof Resource) {<br>            load((Resource) source);<br>            return;<br>        }<br>        if (source instanceof Package) {<br>            load((Package) source);<br>            return;<br>        }<br>        if (source instanceof CharSequence) {<br>            load((CharSequence) source);<br>            return;<br>        }<br>        throw new IllegalArgumentException(\"Invalid source type \" + source.getClass());<br>    }<\/pre>\n\n\n\n<p>\u8fd9\u91cc\u6839\u636e\u4e0d\u540c\u7684\u4f20\u53c2\u8d70\u4e0d\u540c\u7684\u91cd\u8f7d\uff0c\u4e5f\u662fSpring\u626b\u63cf\u548c\u6784\u5efabean\u7684\u5206\u652f\u70b9\u3002\u8fd9\u91cc\u7684source\u662f\u4e3b\u7c7b.class\uff0c\u6240\u4ee5\u8d70\u7684\u662f\u7b2c\u4e00\u4e2aif\u5206\u652f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">private int load(Class&lt;?&gt; source) {<br> &nbsp; &nbsp;if (isGroovyPresent()<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&amp;&amp; GroovyBeanDefinitionSource.class.isAssignableFrom(source)) {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Any GroovyLoaders added in beans{} DSL can contribute beans here<br> &nbsp; &nbsp; &nbsp; &nbsp;GroovyBeanDefinitionSource loader = BeanUtils.instantiateClass(source,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GroovyBeanDefinitionSource.class);<br> &nbsp; &nbsp; &nbsp; &nbsp;load(loader);<br> &nbsp;  }<br> &nbsp; &nbsp;if (isComponent(source)) {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/\u4ee5\u6ce8\u89e3\u7684\u65b9\u5f0f\uff0c\u5c06\u542f\u52a8\u7c7bbean\u4fe1\u606f\u5b58\u5165beanDefinitionMap\uff0c\u4e5f\u5c31\u662f\u5c06HelloWorldMainApplication.class\u5b58\u5165\u4e86beanDefinitionMap<br> &nbsp; &nbsp; &nbsp; &nbsp;this.annotatedReader.register(source);<br> &nbsp; &nbsp; &nbsp; &nbsp;return 1;<br> &nbsp;  }<br> &nbsp; &nbsp;return 0;<br>}<\/pre>\n\n\n\n<p>\u8fd9\u91cc\u53ea\u5c06\u542f\u52a8\u7c7b\u6ce8\u5165\u5230\u4e86beanDefinitionMap\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u626b\u63cf\u5e76\u6ce8\u518cbean<\/strong><\/h4>\n\n\n\n<p>\u5728<strong>\u5237\u65b0\u5bb9\u5668\uff08refresh\uff09<\/strong>\u8fd9\u4e00\u6b65\uff0c\u548cBean\u7684\u88c5\u914d\u6709\u5173\u7684\u6b65\u9aa4\u4e3b\u8981\u5b9e\u73b0\u662f<strong>invokeBeanFactoryPostProcessors()<\/strong>\u3002\u5b83\u8c03\u7528\u6240\u6709\u5b9e\u73b0\u4e86BeanDefinitionRegistryPostProcessor\u63a5\u53e3\u7684\u7c7b\u7684postProcessBeanDefinitionRegistry()\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5d4c\u5957\u548c\u8c03\u7528\u592a\u591a\uff0c\u662fspring\u7684\u6838\u5fc3ioc\uff0caop\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<p>\u5176\u4e2d\u6700\u6838\u5fc3\u7684\u4e00\u4e2a\u5b9e\u73b0\u7c7b\u662f<strong>ConfigurationClassPostProcessor<\/strong>\u3002\u8fd9\u4e2a\u6d41\u7a0b\u5bf9\u4e8eBean\uff0c\u5927\u6982\u505a\u4e86\u8fd9\u6837\u51e0\u4ef6\u4e8b\uff1a<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u5904\u7406\u6ce8\u89e3<\/strong><\/h5>\n\n\n\n<p><strong>\u6211\u4eec\u7684Controller\u3001Filter\u7b49\u670d\u52a1\u7c7b\u662f\u600e\u4e48\u88ab\u626b\u63cf\u5230\u7684\uff1fSpring\u4e2d\u7684Servlet\u5bb9\u5668\uff08Tomcat\uff09\u662f\u600e\u4e48\u542f\u52a8\u7684\uff1f<\/strong>\u5176\u5b9e\u4e5f\u5c31\u662f\u5728\u8fd9\u4e2a\u6b65\u9aa4\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u4f1a\u53bb\u626b\u63cf\u5e76\u5904\u7406\u4f20\u5165source\u7684\u6ce8\u89e3\uff0c\u5982\u4e3b\u7c7b\u7684<strong>@SpringbootApplication<\/strong>\u3002@SpringbootApplication\u6ce8\u89e3\u5305\u542b\u4e86\u4e09\u4e2a\u91cd\u8981\u7684\u6ce8\u89e3\uff1a<\/p>\n\n\n\n<p><strong>1.@SpringBootConfiguration<\/strong><\/p>\n\n\n\n<p>\u672c\u8d28\u4e0a\u5c31\u662f <code>@Configuration<\/code>\uff0c\u628a\u4e3b\u7c7b\u5f53\u6210\u4e00\u4e2a\u914d\u7f6e\u7c7b\u6ce8\u518c\u5230 Spring \u5bb9\u5668\u91cc\uff08\u5c06\u4e3b\u7c7b\u7684 <code>@Bean<\/code> \u65b9\u6cd5\uff08\u5982\u679c\u6709\uff09\u7eb3\u5165\u5bb9\u5668\u7ba1\u7406\uff09\u3002<\/p>\n\n\n\n<p><strong>2.@EnableAutoConfiguration<\/strong><\/p>\n\n\n\n<p>\u901a\u8fc7 <code>@Import(AutoConfigurationImportSelector.class)<\/code>\uff0c\u8c03\u7528 <code>AutoConfigurationImportSelector.selectImports(...)<\/code>\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5229\u7528 <code>SpringFactoriesLoader.loadFactoryNames(...)<\/code> \u53bb\u8bfb\u53d6\u6240\u6709\u4f9d\u8d56 JAR \u4e2d\u7684 <code>META\u2011INF\/spring.factories<\/code>\uff0c\u62ff\u5230\u81ea\u52a8\u914d\u7f6e\u7c7b\u5217\u8868\u3002<\/li>\n\n\n\n<li>\u8fc7\u6ee4\u3001\u53bb\u91cd\u3001\u6392\u9664\u4e0d\u7b26\u5408\u6761\u4ef6\u7684\u914d\u7f6e\uff0c\u7136\u540e\u628a\u6700\u7ec8\u7684\u914d\u7f6e\u7c7b\u540d\u8fd4\u56de\u7ed9\u5bb9\u5668\u53bb\u6ce8\u518c\uff08@AutoConfigurationPackage` \u53ea\u662f\u628a\u4e3b\u7c7b\u6240\u5728\u5305\u53ca\u5b50\u5305\u4e5f\u4f5c\u4e3a\u201c\u53ef\u81ea\u52a8\u914d\u7f6e\u7684\u57fa\u7840\u5305\u201d\u6ce8\u518c\u4e00\u904d\uff0c\u786e\u4fdd\u81ea\u5b9a\u4e49\u914d\u7f6e\u4e5f\u80fd\u88ab\u626b\u63cf\u5230\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<p><strong>3.@ComponentScan<\/strong><\/p>\n\n\n\n<p>\u76f4\u63a5\u89e6\u53d1\u7ec4\u4ef6\u626b\u63cf\uff0c\u626b\u63cf\u4e3b\u7c7b\u6240\u5728\u5305\u53ca\u4ee5\u4e0b\u6240\u6709 <code>@Controller<\/code>\u3001<code>@Service<\/code>\u3001<code>@Repository<\/code>\u3001<code>@Component<\/code> \u7b49\u6ce8\u89e3\uff0c\u6ce8\u518c\u6210 BeanDefinition\u3002<\/p>\n\n\n\n<p>\u628a\u90a3\u4e9b\u5e26\u6ce8\u89e3\u7684\u7c7b\u5305\u62ec <code>@Controller<\/code>\u2014\u2014\u90fd\u6293\u51fa\u6765\uff0c\u5305\u88c5\u6210BeanDefinition\uff0c\u5b9e\u4f8b\u5316\u5e76\u653e\u5230 IOC \u5bb9\u5668\u91cc\u3002<\/p>\n\n\n\n<p>\u5e76\u4e14\u8fd9\u91cc\u989d\u5916\u63d0\u5230\u4e00\u4e2a\u6ce8\u89e3<strong>EmbeddedServletContainerAutoConfiguration\uff1a\u5b83\u662f\u662f\u5d4c\u5165\u5f0fServlet\u5bb9\u5668\u7684\u81ea\u52a8\u914d\u7f6e\u7c7b\uff0c\u4f1a\u5148\u81ea\u52a8\u626b\u63cf\u5f53\u524d\u73af\u5883\u662f\u5426\u5f15\u5165Tomcat\uff08\u5982\u4f9d\u8d56\u914d\u7f6e\uff09\uff0c\u5982\u679c\u662f\u5219\u83b7\u53d6\u5de5\u7a0b\u7c7b\u3001\u5b9e\u4f8b\u5316Tomcat\u5e76\u8fd0\u884c<code>Tomcat.start()<\/code><\/strong><\/p>\n\n\n\n<p><strong>\u5bf9@SpringBootApplication\u6ce8\u89e3\u5904\u7406\u540e\uff0c\u5927\u90e8\u5206\u7684bean\u4fbf\u90fd\u88ab\u6ce8\u518c\u5230beanDefinitionMap\u4e86\u3002<\/strong>\u5f53\u7136\u6211\u4eec\u77e5\u9053\uff0c\u9664\u4e86\u4e3b\u7c7b\u6709\u8fd9\u79cd&#8221;\u914d\u7f6e\u578b\u6ce8\u89e3&#8221;\uff0c\u5176\u5b83\u7c7b\u5305\u62ec\u7528\u6237\u81ea\u5b9a\u4e49\u7c7b\u3001spring\u5185\u90e8\u7c7b\u4e5f\u4f1a\u6709\u76f8\u5e94\u7684\u6ce8\u89e3\uff0c\u54ea<strong>\u600e\u4e48\u6837\u626b\u63cf\u5230\u5b83\u4eec\u5462<\/strong>\uff1f\u8fd9\u5c31\u8981\u63d0\u5230\u7b2c\u4e8c\u6b65\u4e86\uff1a<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u8c03\u7528<code>BeanDefinitionLoader#load<\/code><\/strong><\/h5>\n\n\n\n<p>\u8fd9\u91cc\u8c03\u7528\u7684\u4e0e\u4e0a\u4e00\u6b65\u4e0d\u7528\uff0c\u8c03\u7528\u7684\u662fpackage\u7684\u91cd\u8f7d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">private void load(Package source) {<br> &nbsp; &nbsp;this.scanner.scan(source.getName());<br>}<\/pre>\n\n\n\n<p>\u4f20\u5165\u4e3b\u7c7b\u7684\u5305\u540d\uff0c\u4f5c\u4e3a<strong><code>ClassPathBeanDefinitionScanner.scan<\/code><\/strong>\u7684\u5165\u53c2\u3002\u8fd9\u4e2a\u7c7b\u53c8\u4f1a\u53bb\u8c03\u7528doScan\uff0c\u53c8\u5c06\u4e3b\u7c7b\u5305\u4e0b\u9012\u5f52\u641c\u7d22\uff0c\u5c06\u626b\u63cf\u7684\u6ce8\u89e3\u8fdb\u884c\u5904\u7406\uff0c\u5982\u5904\u7406\u5e38\u89c1\u6ce8\u89e3\uff08<code>@Lazy<\/code>\u3001<code>@Primary<\/code>\u3001<code>@DependsOn<\/code> \u7b49\uff09\uff1b\u7ed3\u5408 <code>includeFilters<\/code> \u548c <code>excludeFilters<\/code> \u5224\u65ad\u54ea\u4e9b\u7c7b\u662f\u5019\u9009\u5bf9\u8c61\uff08\u4f8b\u5982\u6807\u6ce8\u4e86 <code>@Component<\/code>\u3001<code>@Controller<\/code>\u3001<code>@Service<\/code> \u7b49\uff09\uff1b\u626b\u63cf\u6ce8\u89e3\u7684\u4f5c\u7528\u57df<strong>@Scope<\/strong>\uff0c\u4e3a\u5c01\u88c5\u7684BeanDefinition\u63d0\u4f9b\u4fe1\u606f\uff0c\u6700\u540e\u6ce8\u5165\u5230beanDefinitionMap\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">bean\u7684\u521d\u59cb\u5316\u548c\u5b9e\u4f8b\u5316<\/h4>\n\n\n\n<p>\u8fd9\u4e00\u6b65\u5c31\u4e0d\u7ec6\u8bb2\u4e86\uff0c\u53ef\u4ee5\u53bb\u770b<a href=\"https:\/\/blog.csdn.net\/qq_15037231\/article\/details\/105938673\">\u3010spring\u5bb9\u5668\u542f\u52a8\u3011\u4e4bbean\u7684\u5b9e\u4f8b\u5316\u548c\u521d\u59cb\u5316\uff08\u6587\u672b\u9644:spring\u5faa\u73af\u4f9d\u8d56\u539f\u7406\uff09_\u5b9e\u4f8b\u5316bean\u548c\u521d\u59cb\u5316bean-CSDN\u535a\u5ba2<\/a>\u3002\u4ece\u6574\u4f53\u8fc7\u7a0b\u7b80\u5355\u6765\u8bf4\uff0c\u5c31\u662f<strong>\u4ece\u4e0a\u9762\u6b65\u9aa4\u63d0\u5230\u7684\u628abeanDefinitionMap\u6ce8\u518c\u7684BeanDefinition\u8fdb\u884c\u89e3\u6790\uff0c\u5f97\u5230bean\u7684\u5143\u4fe1\u606f\uff0c\u518d\u7ecf\u8fc7\u4e00\u7cfb\u5217\u6df1\u5316\u5904\u7406\uff0c\u6700\u7ec8\u5b58\u653e\u5230\u4e09\u7ea7\u7f13\u5b58\u4e2d\uff08\u5904\u7406\u597d\u7684bean\u653e\u5728singletonObjects\uff0c\u534a\u6210\u54c1bean\u653e\u5728earlySingletonObjects\uff0c\u534a\u6210\u54c1bean\u5de5\u5382\u653e\u5728singletonFactories\uff09<\/strong><\/p>\n\n\n\n<p>\u5177\u4f53\u5b9e\u73b0\u5927\u6982\u6765\u8bf4\u5206\u4e3a\u56db\u6b65\uff1a<\/p>\n\n\n\n<p><strong>1. \u5b9e\u4f8b\u5316\uff08<code>createBeanInstance()<\/code>\uff09<\/strong><\/p>\n\n\n\n<p>Spring \u6839\u636e BeanDefinition \u4e2d\u7684\u4fe1\u606f\uff08\u6784\u9020\u5668\u3001<code>factory-method<\/code>\u3001<code>Supplier<\/code>\uff09\uff0c\u4f7f\u7528\u53cd\u5c04\u6216 CGLIB \u5b9e\u4f8b\u5316\u5bf9\u8c61\uff0c\u4f46\u6b64\u65f6\u4ec5\u662f\u201c\u7a7a\u58f3\u201d\uff0c\u5c1a\u65e0\u5c5e\u6027\u3002<\/p>\n\n\n\n<p><strong>2. \u4f9d\u8d56\u586b\u5145\uff08<code>populateBean()<\/code>\uff09<\/strong><\/p>\n\n\n\n<p>\u626b\u63cf\u9700\u8981\u6ce8\u5165\u7684\u5c5e\u6027\uff08\u6765\u81ea <code>PropertyValues<\/code>\u3001@Autowired\u3001@Value \u7b49\uff09\uff0c\u5904\u7406\u4f9d\u8d56\u3002\u5982\u679c\u9047\u5230\u672a\u5b8c\u6210\u7684 Bean\uff0c\u4f1a\u5229\u7528\u4e09\u7ea7\u7f13\u5b58\u673a\u5236\u652f\u6301\u5faa\u73af\u4f9d\u8d56\u3002<\/p>\n\n\n\n<p><strong>3. \u521d\u59cb\u5316\uff08<code>initializeBean()<\/code>\uff09<\/strong><\/p>\n\n\n\n<p>\u5904\u7406 Aware \u63a5\u53e3\u56de\u8c03\uff08\u5982 <code>BeanNameAware<\/code>, <code>BeanFactoryAware<\/code>\uff09\u3002<\/p>\n\n\n\n<p>\u6267\u884c <code>BeanPostProcessor.beforeInitialization()<\/code>\u3002<\/p>\n\n\n\n<p>\u6267\u884c\u521d\u59cb\u5316\u65b9\u6cd5\uff1a@PostConstruct\u3001<code>afterPropertiesSet()<\/code>\uff08\u6765\u81ea <code>InitializingBean<\/code>\uff09\u3001\u4ee5\u53ca\u81ea\u5b9a\u4e49 <code>init-method<\/code>\u3002<\/p>\n\n\n\n<p>\u6267\u884c <code>BeanPostProcessor.afterInitialization()<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u8fd9\u91cc\u8fd8\u5bf9AOP\u505a\u4e86\u4e00\u4e9b\u5904\u7406\uff0c\u5982\u679c\u68c0\u6d4b\u5230\u6709\u5207\u9762\u6ce8\u89e3\uff08@Aspect\u3001@Poincut&#8230;\uff09\uff0c\u4f1a\u751f\u6210\u4e00\u4e2a\u4ee3\u7406&#8211;createProxy()\uff0c\u4f5c\u4e3a\u540e\u7eedAOP\u8bbf\u95ee\u7684\u83b7\u53d6\u5230\u7684\u4ee3\u7406\uff0c\u4f5c\u4e3a\u6267\u884c\u51fd\u6570\u7684\u5165\u53e3\uff08\u5176\u5b9e\u7684\u6e90\u7801\u5b9e\u73b0\u8fd9\u91cc\u6709\u70b9\u50cfRMI\uff0c\u53ea\u662f\u672c\u5730\u8c03\u7528\u201d\u672c\u5730\u65b9\u6cd5\u201c\uff09\u3002<\/strong><\/p>\n\n\n\n<p><strong>4. \u6ce8\u518c\u9500\u6bc1\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<p>\u5c06\u5b9e\u73b0 <code>DisposableBean<\/code>\u3001\u914d\u7f6e <code>destroy-method<\/code> \u7684 Bean \u7528 <code>DisposableBeanAdapter<\/code> \u5305\u88c5\u540e\uff0c\u6ce8\u518c\u5230\u9500\u6bc1\u961f\u5217\u4e2d\u3002<\/p>\n\n\n\n<p><strong>\u5faa\u73af\u4f9d\u8d56 &amp; \u4e09\u7ea7\u7f13\u5b58<\/strong><\/p>\n\n\n\n<p>\u4e0a\u9762\u63d0\u5230\u4e86\u4e09\u7ea7\u7f13\u5b58\uff0c\u7b80\u5355\u6765\u8bf4\u8fd9\u4e2a\u4e09\u7ea7\u7f13\u5b58\u5c31\u662f\u540e\u7eed\u4f8b\u5982\u8c03\u7528getBean(String beanName)\u65b9\u6cd5\u4f1a\u53bb\u8bbf\u95ee\u7684\u7f13\u5b58\uff0c\u7528\u4e8e\u5feb\u901f\u5730\u53d6\u51fabean\uff0c\u8fd9\u91cc\u590d\u5236\u6587\u7ae0\u4e2d\u7684\u8bdd\u7b80\u77ed\u4ecb\u7ecd\u4e00\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">singletonObjects:\u7f13\u5b58\u6240\u6709\u6784\u5efa\u597d\u7684bean<br>earlySingletonObjects:\u5728bean\u6784\u5efa\u8fc7\u7a0b\u4e2d\u63d0\u524d\u66b4\u9732\u51fa\u6765\u7684\u201c\u534a\u6210\u54c1\u201dbean<br>singletonFactories:\u7528\u4e8e\u521b\u5efa\u201c\u534a\u6210\u54c1\u201dbean\u7684\u5de5\u5382<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\uff081\uff09singletonObjects\uff1asingletonObjects\u4e2d\u5b58\u50a8\u7684\u90fd\u662f\u5b8c\u5168\u6784\u5efa\u597d\u7684bean\uff0c\u5bb9\u5668\u542f\u52a8\u540e\u5728\u9879\u76ee\u4ee3\u7801\u4e2d\u6211\u4eec\u901a\u8fc7getBean()\u4ece\u5bb9\u5668\u4e2d\u83b7\u53d6bean\u5b9e\u4f8b\u65f6\uff0c\u5c31\u662f\u76f4\u63a5\u4ece\u8be5\u7f13\u5b58\u4e2d\u83b7\u53d6\uff0c\u6240\u4ee5\u6548\u7387\u975e\u5e38\u9ad8\uff0c\u540c\u65f6\u6027\u80fd\u975e\u5e38\u597d\u3002<br>\uff082\uff09earlySingletonObjects\uff1aearlySingletonObjects\u4e3b\u8981\u4fdd\u5b58\u5728\u5bf9\u8c61\u6784\u5efa\u8fc7\u7a0b\u4e2d\u63d0\u524d\u66b4\u9732\u51fa\u6765\u7684\u201c\u534a\u6210\u54c1\u201dbean\uff0c\u662f\u89e3\u51b3\u5927\u90e8\u5206\u5faa\u73af\u4f9d\u8d56\u7684\u5173\u952e\u3002\u5047\u8bbe\u6709\u4e24\u4e2aBean A\u548cB\uff0c\u8fd9\u4e24\u4e2aBean\u95f4\u5b58\u5728\u76f8\u4e92\u4f9d\u8d56\uff0c\u4e14\u90fd\u4e0d\u5b58\u5728\u52a8\u6001\u4ee3\u7406\u3002\u90a3\u4e48\u5f53\u5bb9\u5668\u6784\u5efaA\u65f6\uff0c\u5b9e\u4f8b\u5316\u597dA\u540e\uff0c\u5bb9\u5668\u4f1a\u628a\u5c1a\u672a\u6ce8\u5165\u4f9d\u8d56\u7684A\u6682\u65f6\u653e\u5165earlySingletonObjects\u4e2d\uff0c\u7136\u540e\u53bb\u5e2eA\u6ce8\u5165\u4f9d\u8d56\u3002\u8fd9\u65f6\u5bb9\u5668\u53d1\u73b0A\u4f9d\u8d56\u4e8eB\uff0c\u4f46\u662fB\u8fd8\u6ca1\u6709\u6784\u5efa\u597d\uff0c\u90a3\u4e48\u5c31\u4f1a\u5148\u53bb\u6784\u5efaB\u3002\u5bb9\u5668\u5728\u5b9e\u4f8b\u5316\u597dB\u65f6\uff0c\u540c\u6837\u9700\u8981\u5e2eB\u6ce8\u5165\u4f9d\u8d56\uff0c\u6b64\u65f6B\u53d1\u73b0\u81ea\u5df1\u4f9d\u8d56A\uff0c\u5728\u83b7\u53d6A\u65f6\u5c31\u53ef\u4ee5\u4eceearlySingletonObjects\u4e2d\u83b7\u53d6\u5c1a\u672a\u6ce8\u5165\u4f9d\u8d56\u7684A\u5f15\u7528\uff0c\u8fd9\u6837\u5c31\u4e0d\u4f1a\u963b\u585eB\u7684\u6784\u5efa\uff0cB\u5b8c\u6210\u6784\u5efa\u540e\u5c31\u53c8\u53ef\u4ee5\u6ce8\u5165\u5230A\u4e2d\uff0c\u8fd9\u6837\u5c31\u89e3\u51b3\u4e86\u7b80\u5355\u7684\u5faa\u73af\u4f9d\u8d56\u95ee\u9898\u3002<br>\uff083\uff09singletonFactories\uff1asingletonFactories\u7528\u6765\u4fdd\u5b58\u521b\u5efa\u201c\u534a\u6210\u54c1\u201dbean\u7684\u5de5\u5382\u5b9e\u4f8b\uff0c\u5728Bean\u5b9e\u4f8b\u5316\u597d\u540e\uff0c\u5e76\u4e0d\u4f1a\u76f4\u63a5\u5c06\u5c1a\u672a\u6ce8\u5165\u4f9d\u8d56\u7684bean\u76f4\u63a5\u653e\u5165\u5230earlySingletonObjects\u4e2d\uff0c\u800c\u662f\u5c06\u80fd\u591f\u521b\u5efa\u8be5\u201c\u534a\u6210\u54c1\u201dbean\u7684\u5de5\u5382\u5b9e\u4f8b\u653e\u5165\u5230singletonFactories\u4e2d\u3002\u8fd9\u6837\u6211\u4eec\u5728\u5bf9\u5916\u66b4\u9732\u201c\u534a\u6210\u54c1\u201dbean\u65f6\u5c31\u53ef\u4ee5\u52a0\u5165\u4e00\u4e9b\u81ea\u5b9a\u4e49\u7684\u7279\u6b8a\u5904\u7406\u903b\u8f91\u3002\u4f8b\u5982\u4e0b\u56fe\u4e2d\u666e\u901a\u5207\u9762\u4ee3\u7406\u5c31\u4f1a\u5728\u6b64\u5904\u52a8\u4e9b\u201c\u624b\u811a\u201d\u3002<\/pre>\n\n\n\n<p>\u81f3\u6b64\uff0cSpringboot\u542f\u52a8\u4e2d\u5bf9\u4e8ebean\u7684\u914d\u7f6e\u5230\u6b64\u5c31\u7ed3\u675f\u4e86<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u53d1\u51fa\u7ed3\u675f\u6267\u884c\u7684\u4e8b\u4ef6<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">listeners.started(context);<\/pre>\n\n\n\n<p>\u5229\u7528<strong><code>EventPublishingRunListener<\/code><\/strong>\u76d1\u542c\u5668\uff0c\u5e76\u6267\u884c\u5176started\u65b9\u6cd5\uff0c\u5e76\u4e14\u5c06\u521b\u5efa\u7684<strong>Spring\u5bb9\u5668(ApplicationContext)<\/strong>\u4f20\u8fdb\u53bb\u4e86\uff0c\u521b\u5efa\u4e00\u4e2aApplicationStartedEvent\u4e8b\u4ef6\uff0c\u5e76\u6267\u884cConfigurableApplicationContext \u7684publishEvent\u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f\u8bf4<strong>\u8fd9\u91cc\u662f\u5728Spring\u5bb9\u5668\u4e2d\u53d1\u5e03\u4e8b\u4ef6\uff0c\u5e76\u4e0d\u662f\u5728SpringApplication\u4e2d\u53d1\u5e03\u4e8b\u4ef6\uff0c\u548c\u524d\u9762\u7684starting\u662f\u4e0d\u540c\u7684\uff0c\u524d\u9762\u7684starting\u662f\u76f4\u63a5\u5411SpringApplication\u4e2d\u768411\u4e2a\u76d1\u542c\u5668\u53d1\u5e03\u542f\u52a8\u4e8b\u4ef6\u3002<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6267\u884cRunners<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\"> this.callRunners(context, applicationArguments);<\/pre>\n\n\n\n<p>\u4f1a\u83b7\u53d6\u5bb9\u5668\u4e2d\u6240\u6709\u7684ApplicationRunner\u7684Bean\u5b9e\u4f8b\u3001\u4ee5\u53ca\u6240\u7528CommandLineRunner\u7684Bean\u5b9e\u4f8b\uff0c\u5e76\u5206\u522b\u6267\u884c\u5176\u4e2d\u7684run\u65b9\u6cd5\u3002\u8fd9\u91cc\u4e5f\u591a\u7528\u4e8e\u81ea\u5b9a\u4e49\uff0c\u53ef\u4ee5\u81ea\u5b9a\u4e49Springboot\u5728\u542f\u52a8\u6d41\u7a0b\u65f6\u60f3\u8981\u6267\u884c\u4ec0\u4e48\u4ee3\u7801\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u81f3\u6b64\uff0cSpringboot\u542f\u52a8\u6d41\u7a0b\u7ed3\u675f\uff0cSpringApplication.run\u65b9\u6cd5\u6700\u7ec8\u5c06\u6784\u67b6\u7684ApplicationContext\u8fd4\u56de\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Springboot\u7684MVC\u6d41\u7a0b<\/h1>\n\n\n\n<p>Springboot\u7684mvc\u90e8\u5206\u5c31\u548cSpring\u6ca1\u4ec0\u4e48\u533a\u522b\u4e86\uff0c\u90fd\u662f\u57fa\u4e8e\u540c\u4e00\u4e2a DispatcherServlet\u3001\u540c\u4e00\u5957 HandlerMapping \u2192 HandlerAdapter \u2192 Controller \u2192 ViewResolver \u7684\u67b6\u6784\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Springboot MVC\u670d\u52a1\u7684\u542f\u52a8<\/h2>\n\n\n\n<p>\u4e0a\u6587\u4e2dSpringboot\u7684run\u65b9\u6cd5\u5df2\u7ecf\u542f\u52a8\u5b8c\u6bd5\uff0c\u5e72\u7684\u4e8b\u60c5\u597d\u50cf\u4e5f\u53ea\u662f\u8fd4\u56de\u4e00\u4e2aApplicationContext\uff0c\u90a3Springboot\u7684\u76d1\u542c\u7aef\u53e3\uff0c\u7b49\u5f85\u8bf7\u6c42\u8fde\u63a5\u7b49web\u670d\u52a1\u662f\u4ece\u54ea\u91cc\u542f\u52a8\u7684\u5462\uff1f<\/p>\n\n\n\n<p>\u8981\u56de\u7b54\u8fd9\u4e2a\u95ee\u9898\uff0c\u5c31\u5f97\u5148\u641e\u6e05\u695a Spring\u202fBoot \u548c\u5e95\u5c42 Servlet \u5bb9\u5668\uff08\u5982 Tomcat\uff09\u662f\u600e\u4e48\u534f\u540c\u5de5\u4f5c\u7684\u3002<\/p>\n\n\n\n<p>Tomcat\u5904\u7406web\u8bf7\u6c42\u9760\u7684\u5c31\u662fServlet\uff0c\u5982\u6bcf\u4e00\u4e2a.jsp\u5728\u4ee3\u7801\u4e2d\u90fd\u88ab\u6620\u5c04\u6210\u4e00\u4e2ajsp_Servlet\uff0c\u800cSpring mvc\u5904\u7406web\u8bf7\u6c42\u5e76\u8fd4\u56de\u89c6\u56fe\u7528\u7684\u90fd\u662fController\u3002\u800c\u4f17\u6240\u5468\u77e5\uff0cSpring\u202fBoot \u4e2d\u7684 <code>DispatcherServlet<\/code> \u662f\u5904\u7406 HTTP \u8bf7\u6c42\u3001\u6620\u5c04\u5230\u4e0d\u540c <code>@Controller<\/code> \u5e76\u8fd4\u56de\u89c6\u56fe\uff08\u6216\u6570\u636e\uff09\u7684\u6838\u5fc3\u7c7b\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u4eceSpringboot\u7684\u63d0\u4f9bweb\u670d\u52a1\u7684\u67b6\u6784\u6765\u770b\uff0c<strong>Springboot MVC\uff0cSpring MVC\u5c31\u76f8\u5f53\u4e8e\uff08\u6216\u8005\u8bf4\u96c6\u6210\u5728\u4e86\uff09\u4e00\u4e2a\u7279\u6b8a\u7684Tomcat Servlet\uff08DispatcherServlet\uff09\u3002<\/strong><\/p>\n\n\n\n<p>\u5982\u6b64\u800c\u8a00\uff0c\u5bfb\u627eSpringboot MVC\u7684\u670d\u52a1\u542f\u52a8\u70b9\uff0c\u5c31\u53ef\u4ee5\u628a\u773c\u5149\u653e\u5728\u542f\u52a8\u6d41\u7a0b\u4e2dTomcat\u7684\u542f\u52a8\u4e4b\u5904\u3002<\/p>\n\n\n\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u7ed3\u5408\u4e0a\u6587\uff0cSpring\u202fBoot \u5728\u542f\u52a8\u65f6\u4f1a\u521b\u5efa\u4e00\u4e2a\u5185\u5d4c\u7684 Tomcat \u5bb9\u5668\uff0c\u7136\u540e\u901a\u8fc7\u81ea\u52a8\u914d\u7f6e\u751f\u6210\u4e00\u4e2a <code>ServletRegistrationBean&lt;DispatcherServlet&gt;<\/code>\uff0c\u5e76\u5c06\u5176\u6ce8\u518c\u5230\u8fd9\u4e2a\u5bb9\u5668\u4e2d\u3002Tomcat \u5728\u542f\u52a8\u65f6\u4f1a\u904d\u5386\u6240\u6709\u7684 <code>ServletContextInitializer<\/code>\uff08\u5305\u62ec\u6211\u4eec\u521a\u624d\u8bf4\u7684 <code>ServletRegistrationBean<\/code>\uff09\uff0c\u8c03\u7528\u5b83\u4eec\u7684 <code>onStartup()<\/code> \u65b9\u6cd5\uff0c\u5c06 <code>DispatcherServlet<\/code> \u52a0\u5165\u5230\u5bb9\u5668\u7684 servlet \u6620\u5c04\u8868\u91cc\u3002\u81f3\u6b64\uff0cDispatcherServlet \u4fbf\u6b63\u5f0f\u201c\u7ed1\u5b9a\u201d\u5728\u6307\u5b9a\u7684 URL \u4e0a\uff0c\u5f00\u59cb\u76d1\u542c\u5e76\u7b49\u5f85\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u3002<\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u67b6\u6784\u4e2d\uff0c<strong>DispatcherServlet<\/strong> \u5219\u53d8\u6210\u4e86Tomcat\u7684\u4e00\u4e2a\u7279\u6b8a Servlet\uff0c\u800c\u6240\u6709\u7684 <code>@Controller<\/code> \u548c\u5176\u4ed6 MVC \u7ec4\u4ef6\uff0c\u5219\u7531 DispatcherServlet \u8fdb\u4e00\u6b65\u8c03\u7528\uff0c\u5b8c\u6210\u6700\u7ec8\u7684\u4e1a\u52a1\u903b\u8f91\u5904\u7406\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Springboot MVC\u5904\u7406\u8bf7\u6c42<\/h3>\n\n\n\n<p>\u8bf7\u6c42\u7684\u524d\u534a\u90e8\u5206\u8d70\u7684\u662fTomcat\u5904\u7406\uff0cTomcat\u6700\u540e\u4f1a\u5728ApplicationFilterChain#doFilter\u6700\u540e\u7684internalDoFilter\u65b9\u6cd5\uff0c\u8c03\u7528Servlet#service\uff0c\u8fdb\u5165\u5230<code>DispatcherServlet#service<\/code>\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u5bf9\u4e8e\u6211\u4eec\u5b66\u4e60\u5b89\u5168\u503c\u5f97\u6ce8\u610f\u7684\u4e00\u70b9\u7684\u662f\uff0c<strong>\u539f\u751f\u7684Spring MVC\u53ea\u6709Tomcat\u6d41\u7a0b\u5bf9url\u7684\u5b57\u7b26\u62e6\u622a\u3001\u5904\u7406\u505a\u4e86\u5de5\u4f5c\u3002<\/strong>\u4e5f\u5c31\u662f\u5728Tomcat\u6e90\u7801\u6d41\u7a0b\u4e2d\u8bf4\u7684:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">0\u201331\u3001127&nbsp;\u662f\u6240\u6709 C0 \u63a7\u5236\u5b57\u7b26\uff0c false<br>\u200b<br>! \" # $ &lt; &gt; [ \\ ] ^ \\ { | } ~ \u3002false<br>\u200b<br>\u6240\u6709\u5b57\u6bcd\u3001\u6570\u5b57\uff0c\u4ee5\u53ca - . \/ : = ? @ _ \u5b57\u7b26\u90fd\u4e3a&nbsp;true<\/pre>\n\n\n\n<p><strong>\u800cSpring\u5e76\u6ca1\u6709\u5bf9URL\u7684\u5b89\u5168\u5904\u7406\u591a\u52a0\u903b\u8f91\uff1b\u53ea\u6709Spring Security\u662f\u65b0\u52a0\u4e86\u51e0\u4e2aFilter\uff0c\u5176\u4e2d\u624d\u6709Spring\u5b9e\u73b0\u7684\u5bf9URL\u5b57\u7b26\u7684\u62e6\u622a\u3002<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u91cc\u4f1a\u5148\u8c03\u7528\u5176\u7236\u7c7bFrameworkServlet\u4ee5\u53caFrameworkServlet\u7236\u7c7bHttpServlet\u5148\u8fdb\u884c\u4e00\u4e2a\u7b80\u5355\u7684\u5904\u7406\uff0c\u4f9d\u636e\u8bf7\u6c42\u65b9\u6cd5\uff0c\u5206\u522bdoGet\uff0cdoPost&#8230;\u5148\u8fdb\u884c\u4e00\u4e2a\u7b80\u5355\u7684\u89e3\u6790\u3002<\/p>\n\n\n\n<p>\u6700\u540e\u5c31\u6765\u5230\u4e86<strong><code>DispatcherServlet#doDispatch<\/code><\/strong>\u3002<\/p>\n\n\n\n<p>\u5b83\u505a\u4e86\u8fd9\u6837\u51e0\u4ef6\u4e8b\uff1a<\/p>\n\n\n\n<p><strong>1.\u6784\u9020HandlerExecutionChain\u548c\u5bfb\u627eHandlerAdapter\uff1a<\/strong>\u6839\u636e\u8bf7\u6c42\u65b9\u5f0f\u7684\u4e0d\u540c\u627e\u5230\u76f8\u5e94\u7684Handlermapping\uff1b\u518d\u6839\u636eurl\u8bf7\u6c42\u53c2\u6570\uff0c\u53bb\u83b7\u53d6handlermapping\u4e2d\u7684Handlermethod(\u6b64\u5904\u4e3aController\u65b9\u6cd5\u5bf9\u5e94\u7684bean)\uff0c\u518d\u548c\u7cfb\u7edf\u62e6\u622a\u5668\u5c01\u88c5\u6210\u4e00\u4e2aHandlerChain\uff1b\u518d\u6839\u636e\u8fd9\u4e2aHandlerChain\u53bb\u5bfb\u627e\u9002\u914d\u7684HandlerAdapter\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6709\u5f88\u591aXXXHandler\uff0cHandlerXXX\u3002\u5148\u8981\u7406\u6e05\u695a\u8fd9\u4e9b\u4e1c\u897f\u4ee5\u53caHandler\u548cController\u7684\u5173\u7cfb\u5177\u4f53\u53ef\u4ee5\u53bb\u770b\u770bSpringboot\u6280\u672f\u6587\u6863\u6216\u8005\u8bf4\u6587\u7ae0\u7684<strong>\u9002\u914d\u5668\u6a21\u5f0f\u6709\u5173\u90e8\u5206<\/strong>\uff1a<a href=\"https:\/\/blog.csdn.net\/zxd1435513775\/article\/details\/103000992\">https:\/\/blog.csdn.net\/zxd1435513775\/article\/details\/103000992<\/a><\/p>\n\n\n\n<p>2.<strong>\u8c03\u7528HandlerAdapter\uff1a<\/strong>HandlerAdapter\u8c03\u7528\u5bf9\u5e94\u7684Handler\u53bb\u83b7\u5f97\u4e00\u4e2aModelAndView\u5bf9\u8c61\uff1b\u8fd9\u91cc\u5b9e\u9645\u4e0a\u8c03\u7528\u7684\u5c31\u662fController\u65b9\u6cd5\uff0c\u5f97\u5230\u8fd4\u56de\u503c\uff0c\u6839\u636e\u8fd4\u56de\u503c\u586b\u5199ModelAndView\u5bf9\u8c61\u4e2d\u7684viewname\u53d8\u91cf\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e00\u6b65\u8fd8\u6709\u4e2a\u503c\u5f97\u6ce8\u610f\u7684\u5730\u65b9\uff0cviewname\u8fd9\u4e2a\u53d8\u91cf\u4e0d\u4ec5\u4ec5\u662f\u83b7\u5f97\u8fd9\u4e2areturnvalue\u8fd9\u4e48\u7b80\u5355\u3002\u5b9e\u9645\u4e0aSpringboot\u8fd8\u4f1a\u5728<strong>HandlerAdapter<\/strong>\u7684\u6267\u884c\u94fe\u6761\u4e2d\uff0c\u83b7\u53d6\u8fd4\u56de\u503c\u540e\uff0c\u5230<strong>ServletInvocableHandlerMethod#invokeAndHandle<\/strong>\u4e2d\u53bb<strong>returnValueHandlers.handleReturnValue<\/strong>\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20241015204005764.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20241015204005764.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20241015204005764\"\/><\/div><\/figure>\n\n\n\n<p>\u8fd9\u91cc\u4e5f\u4f1a\u4f53\u73b0\u51fa<strong>Controller<\/strong>\u548c<strong>RestController<\/strong>\u7684\u533a\u522b\u2013\u4ee5\u53ca\u4e3a\u4ec0\u4e48\u6211\u4eec\u5b8f\u89c2\u4e0a\u7528Controller\u4f1a\u8fd4\u56de\u4e00\u4e2a\u89e3\u6790\u540e\u7684\u89c6\u56fe\uff0c\u800cRestController\u53ea\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6765\u8fc7\u4e00\u4e0b\u8fd9\u4e2a\u903b\u8f91\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp;  public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {<br>\u200b<br> &nbsp; &nbsp; &nbsp;  HandlerMethodReturnValueHandler handler = selectHandler(returnValue, returnType);<br> &nbsp; &nbsp; &nbsp;  if (handler == null) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  throw new IllegalArgumentException(\"Unknown return value type: \" + returnType.getParameterType().getName());<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  handler.handleReturnValue(returnValue, returnType, mavContainer, webRequest);<br> &nbsp;  }<\/pre>\n\n\n\n<p>\u524d\u9762\u901a\u8fc7invoke <strong>HandlerMethod<\/strong>(controller\/restcontroller)\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\u4f5c\u4e3areturnvalue\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20241006100004124.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20241006100004124.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20241006100004124\"\/><\/div><\/figure>\n\n\n\n<p>\u4f1a\u6839\u636ereturnType\u53bb\u9009\u62e9\u5904\u7406\u7c7b\u578b\u7684handler\u3002\u8fd4\u56de\u7c7b\u578b\u4e0d\u5c31\u662f\u4e2a\u5b57\u7b26\u4e32\u5417\uff1f\u5176\u5b9e\u4e0d\u7136\uff0creturnType\u8fd8\u5305\u542b\u4e86Handler\u7b49\u7b49\u66f4\u591a\u4fe1\u606f\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20241006100620494.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20241006100620494.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20241006100620494\"\/><\/div><\/figure>\n\n\n\n<p>\u800c\u6839\u636eselectHandler<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"> &nbsp;  private HandlerMethodReturnValueHandler selectHandler(@Nullable Object value, MethodParameter returnType) {<br> &nbsp; &nbsp; &nbsp;  boolean isAsyncValue = isAsyncReturnValue(value, returnType);<br> &nbsp; &nbsp; &nbsp;  for (HandlerMethodReturnValueHandler handler : this.returnValueHandlers) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  if (isAsyncValue &amp;&amp; !(handler instanceof AsyncHandlerMethodReturnValueHandler)) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  continue;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  if (handler.supportsReturnType(returnType)) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  return handler;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp;  return null;<br> &nbsp;  }<\/pre>\n\n\n\n<p>\u8fd9\u91cc\u5982\u679c\u4f60\u7528\u7684<strong>RestControlle<\/strong>r\u90a3\u4e48\u4f60\u4f1aselect\u5230\u4e00\u4e2aRequestResponseBodyMethodProcessor:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20241006101034645.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20241006101034645.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20241006101034645\"\/><\/div><\/figure>\n\n\n\n<p>\u4e14\u7531\u4e8e\u540e\u7eed\u8fd4\u56de\u903b\u8f91\u7528\u7684\u662f\u5b83\uff0c\u6700\u540emv = ha.handle(processedRequest, response, mappedHandler.getHandler());\u4f1a\u8fd4\u56denull\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20241006101301331.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20241006101301331.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20241006101301331\"\/><\/div><\/figure>\n\n\n\n<p>\u800c\u5982\u679c\u7528\u7684\u662fController\u4f1aselect\u5230ViewNameMethodReturnValueHandler<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20241006122826589.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20241006122826589.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20241006122826589\"\/><\/div><\/figure>\n\n\n\n<p>\u6700\u540e\u4f1a\u8fd4\u56de:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20241006123303698.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/byname.oss-cn-chengdu.aliyuncs.com\/image-20241006123303698.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20241006123303698\"\/><\/div><\/figure>\n\n\n\n<p><strong>3.applyDefaultViewName()\uff1a<\/strong>\u5bf9\u5f53\u524dModelAndView\u505a\u5224\u65ad\uff0c\u5982\u679c\u4e3anull\u5219\u8fdb\u5165defalutViewName\u90e8\u5206\u5904\u7406\uff0c\u5c06URI path\u4f5c\u4e3amav\u7684viewname\u503c\u3002\u8fd9\u91cc\u5b9e\u9645\u4e0a\u4e5f\u662f\u7b2c\u4e09\u79cdpayload\u7684\u539f\u56e0\u3002<\/p>\n\n\n\n<p>4.<strong>processDispatchResult()\uff1a<\/strong>\u6e32\u67d3\u89c6\u56fe\u7684\u6700\u7ec8\u70b9\u3002<\/p>\n\n\n\n<p>\u5148\u5904\u7406ModelAndView\uff0c\u901a\u8fc7\u9002\u914d\u7684ViewResolver\u5c06viewname\u89e3\u6790\uff0c\u628amav\u91cc\u7684viewname\u7b49\u7b49\u5c01\u88c5\u5230\u4e00\u4e2a\u5177\u4f53\u7684view\u91cc\u9762\uff0c\u6bd4\u5982\u6211\u4eec\u8fd9\u91cc\u7684Thymeleafview\uff0c\u5176viewTemplateName\u91cc\u9762\u5c31\u5c01\u88c5\u4e86\u6211\u4eec\u7684viewname\u3002\u518d\u8c03\u7528Thymeleaf#render\u6765\u89e3\u6790\u89c6\u56fe\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u4f20\u8fdb\u53bb\u7684viewname\u503c\u5305\u542b::\u7684\u8bdd\uff0c\u5373\u8bc6\u522b\u5230\u8fd9\u662f\u7247\u6bb5\u8868\u8fbe\u5f0f\uff0c\u4f1a\u5148\u8fdb\u884c<strong>\u9884\u5904\u7406<\/strong>-\u5373\u89e3\u6790\u5728\u53d8\u91cf\u8868\u8fbe\u5f0f\u4e2d\u7684SPEL\u8bed\u53e5\u3002\u6700\u540e\u6839\u636e\u6587\u4ef6\u540d\u8fd4\u56de\u76f8\u5e94\u7684\u6a21\u677f\u6587\u4ef6<strong>\uff08\u8fd8\u4f1a\u5bf9\u6a21\u677f\u6587\u4ef6\u91cc\u9762\u7684SPEL\u8868\u8fbe\u5f0f\u8fdb\u884c\u89e3\u6790&#8211;Springboot\u8def\u5f84\u7a7f\u8d8a\u6587\u4ef6\u4e0a\u4f20\u62ffshell\u7684\u4e00\u4e2a\u70b9\uff09<\/strong><\/p>\n\n\n\n<p>\u6700\u540e\u5c06html\u5199\u5165response\u57df\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u5355\u6982\u62ec\u4e00\u4e0bSpringboot\u7684\u6e90\u7801\u6d41\u7a0b\u67b6\u6784\uff0c\u4e00\u4e9b\u6bd4\u8f83\u91cd\u8981\u7684\u7c7b\u548c\u7ec6\u8282\u4f1a\u8be6\u7ec6\u5199\u51fa\u6765\u3002 Springboot\u7684\u542f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-28","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/posts\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/comments?post=28"}],"version-history":[{"count":1,"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":29,"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/posts\/28\/revisions\/29"}],"wp:attachment":[{"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/categories?post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/byname6.cn\/index.php\/wp-json\/wp\/v2\/tags?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}