{"id":10006926,"date":"2022-11-28T16:48:20","date_gmt":"2022-11-28T08:48:20","guid":{"rendered":"https:\/\/ityet.com\/?p=10006926"},"modified":"2022-11-28T16:48:20","modified_gmt":"2022-11-28T08:48:20","slug":"%e8%a7%82%e5%af%9f%e8%80%85%e6%a8%a1%e5%bc%8f%e5%9c%a8spring%e4%b8%ad%e7%9a%84%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006926","title":{"rendered":"\u89c2\u5bdf\u8005\u6a21\u5f0f\u5728spring\u4e2d\u7684\u5e94\u7528"},"content":{"rendered":"<p>\u4f5c\u8005\uff1a\u738b\u5b50\u6e90<\/p>\n<h3>1 \u89c2\u5bdf\u8005\u6a21\u5f0f\u7b80\u4ecb<\/h3>\n<h4>1.1 \u5b9a\u4e49<\/h4>\n<p>\u6307\u591a\u4e2a\u5bf9\u8c61\u95f4\u5b58\u5728\u4e00\u5bf9\u591a\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u5f53\u4e00\u4e2a\u5bf9\u8c61\u7684\u72b6\u6001\u53d1\u751f\u6539\u53d8\u65f6\uff0c\u6240\u6709\u4f9d\u8d56\u4e8e\u5b83\u7684\u5bf9\u8c61\u90fd\u5f97\u5230\u901a\u77e5\u5e76\u88ab\u81ea\u52a8\u66f4\u65b0\u3002\u8fd9\u79cd\u6a21\u5f0f\u6709\u65f6\u53c8\u79f0\u4f5c\u53d1\u5e03-\u8ba2\u9605\u6a21\u5f0f\u3001\u6a21\u578b-\u89c6\u56fe\u6a21\u5f0f\uff0c\u5b83\u662f\u5bf9\u8c61\u884c\u4e3a\u578b\u6a21\u5f0f\u3002<\/p>\n<h4>1.2 \u89d2\u8272\u4ecb\u7ecd<\/h4>\n<p>\u5728\u89c2\u5bdf\u8005\u6a21\u5f0f\u4e2d\uff0c\u6709\u4ee5\u4e0b\u51e0\u4e2a\u89d2\u8272\u3002<\/p>\n<p>\u4e3b\u9898\u4e5f\u53eb\u88ab\u89c2\u5bdf\u8005\uff08Subject\uff09\uff1a<\/p>\n<ol>\n<li>\u5b9a\u4e49\u88ab\u89c2\u5bdf\u8005\u5fc5\u987b\u5b9e\u73b0\u7684\u804c\u8d23\uff0c<\/li>\n<li>\u5b83\u80fd\u52a8\u6001\u7684\u589e\u52a0\u53d6\u6d88\u89c2\u5bdf\u8005\uff0c\u5b83\u4e00\u822c\u662f\u62bd\u8c61\u7c7b\u6216\u8005\u662f\u5b9e\u73b0\u7c7b\uff0c\u4ec5\u4ec5\u5b8c\u6210\u4f5c\u4e3a\u88ab\u89c2\u5bdf\u8005\u5fc5\u987b\u5b9e\u73b0\u7684\u804c\u8d23\uff1a<\/li>\n<li>\u7ba1\u7406\u89c2\u5bdf\u8005\u5e76\u901a\u77e5\u89c2\u5bdf\u8005\u3002<\/li>\n<\/ol>\n<p>\u89c2\u5bdf\u8005\uff08Observer\uff09\uff1a  <br \/>\u89c2\u5bdf\u8005\u63a5\u53d7\u5230\u6d88\u606f\u540e\uff0c\u5373\u8fdb\u884c\u66f4\u65b0\u64cd\u4f5c\uff0c\u5bf9\u63a5\u6536\u5230\u7684\u4fe1\u606f\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>\u5177\u4f53\u7684\u88ab\u89c2\u5bdf\u8005\uff08ConcreteSubject\uff09\uff1a  <br \/>\u5b9a\u4e49\u88ab\u89c2\u5bdf\u8005\u81ea\u5df1\u7684\u4e1a\u52a1\u903b\u8f91\uff0c\u540c\u65f6\u5b9a\u4e49\u5bf9\u54ea\u4e9b\u4e8b\u4ef6\u8fdb\u884c\u901a\u77e5\u3002<\/p>\n<p>\u5177\u4f53\u7684\u89c2\u5bdf\u8005\uff08ConcreteObserver\uff09\uff1a  <br \/>\u5177\u4f53\u7684\u89c2\u5bdf\u8005\uff0c\u6bcf\u4e2a\u89c2\u5bdf\u8005\u63a5\u6536\u5230\u6d88\u606f\u540e\u7684\u5904\u7406\u53cd\u5e94\u662f\u4e0d\u540c\u7684\uff0c\u6bcf\u4e2a\u89c2\u5bdf\u8005\u90fd\u6709\u81ea\u5df1\u7684\u5904\u7406\u903b\u8f91\u3002<\/p>\n<h4>1.3 \u89c2\u5bdf\u8005\u6a21\u5f0f\u7684\u9002\u7528\u573a\u666f<\/h4>\n<ol>\n<li>\u5bf9\u8c61\u95f4\u5b58\u5728\u4e00\u5bf9\u591a\u5173\u7cfb\uff0c\u4e00\u4e2a\u5bf9\u8c61\u7684\u72b6\u6001\u53d1\u751f\u6539\u53d8\u4f1a\u5f71\u54cd\u5176\u4ed6\u5bf9\u8c61\u3002<\/li>\n<li>\u5f53\u4e00\u4e2a\u62bd\u8c61\u6a21\u578b\u6709\u4e24\u4e2a\u65b9\u9762\uff0c\u5176\u4e2d\u4e00\u4e2a\u65b9\u9762\u4f9d\u8d56\u4e8e\u53e6\u4e00\u65b9\u9762\u65f6\uff0c\u53ef\u5c06\u8fd9\u4e8c\u8005\u5c01\u88c5\u5728\u72ec\u7acb\u7684\u5bf9\u8c61\u4e2d\u4ee5\u4f7f\u5b83\u4eec\u53ef\u4ee5\u5404\u81ea\u72ec\u7acb\u5730\u6539\u53d8\u548c\u590d\u7528\u3002<\/li>\n<li>\u5b9e\u73b0\u7c7b\u4f3c\u5e7f\u64ad\u673a\u5236\u7684\u529f\u80fd\uff0c\u4e0d\u9700\u8981\u77e5\u9053\u5177\u4f53\u6536\u542c\u8005\uff0c\u53ea\u9700\u5206\u53d1\u5e7f\u64ad\uff0c\u7cfb\u7edf\u4e2d\u611f\u5174\u8da3\u7684\u5bf9\u8c61\u4f1a\u81ea\u52a8\u63a5\u6536\u8be5\u5e7f\u64ad\u3002<\/li>\n<li>\u591a\u5c42\u7ea7\u5d4c\u5957\u4f7f\u7528\uff0c\u5f62\u6210\u4e00\u79cd\u94fe\u5f0f\u89e6\u53d1\u673a\u5236\uff0c\u4f7f\u5f97\u4e8b\u4ef6\u5177\u5907\u8de8\u57df\uff08\u8de8\u8d8a\u4e24\u79cd\u89c2\u5bdf\u8005\u7c7b\u578b\uff09\u901a\u77e5\u3002<\/li>\n<\/ol>\n<h3>2 \u89c2\u5bdf\u8005\u6a21\u5f0f\u5728Spring\u4e2d\u7684\u5e94\u7528<\/h3>\n<h4>2.1 spring\u7684\u4e8b\u4ef6\u76d1\u542c\u673a\u5236<\/h4>\n<p>Spring\u4e8b\u4ef6\u673a\u5236\u662f\u89c2\u5bdf\u8005\u6a21\u5f0f\u7684\u5b9e\u73b0\u3002ApplicationContext\u4e2d\u4e8b\u4ef6\u5904\u7406\u662f\u7531ApplicationEvent\u7c7b\u548cApplicationListener\u63a5\u53e3\u6765\u63d0\u4f9b\u7684\u3002\u5982\u679c\u4e00\u4e2aBean\u5b9e\u73b0\u4e86ApplicationListener\u63a5\u53e3\uff0c\u5e76\u4e14\u5df2\u7ecf\u53d1\u5e03\u5230\u5bb9\u5668\u4e2d\u53bb\uff0c\u6bcf\u6b21ApplicationContext\u53d1\u5e03\u4e00\u4e2aApplicationEvent\u4e8b\u4ef6\uff0c\u8fd9\u4e2aBean\u5c31\u4f1a\u63a5\u5230\u901a\u77e5\u3002ApplicationEvent \u4e8b\u4ef6\u7684\u53d1\u5e03\u9700\u8981\u663e\u793a\u89e6\u53d1\uff0c\u8981\u4e48 Spring \u89e6\u53d1\uff0c\u8981\u4e48\u6211\u4eec\u7f16\u7801\u89e6\u53d1\u3002spring\u5185\u7f6e\u4e8b\u4ef6\u7531spring\u89e6\u53d1\u3002\u6211\u4eec\u5148\u6765\u770b\u4e00\u4e0b\uff0c\u5982\u4f55\u81ea\u5b9a\u4e49spring\u4e8b\u4ef6\uff0c\u5e76\u4f7f\u5176\u88ab\u76d1\u542c\u548c\u53d1\u5e03\u3002<\/p>\n<p><strong>2.1.1 \u4e8b\u4ef6<\/strong><\/p>\n<p>\u4e8b\u4ef6\uff0cApplicationEvent\uff0c\u8be5\u62bd\u8c61\u7c7b\u7ee7\u627f\u4e86EventObject\uff0cEventObject\u662fJDK\u4e2d\u7684\u7c7b\uff0c\u5e76\u5efa\u8bae\u6240\u6709\u7684\u4e8b\u4ef6\u90fd\u5e94\u8be5\u7ee7\u627f\u81eaEventObject\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042916359\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<pre><code>public abstract class ApplicationEvent extends EventObject {\nprivate static final long serialVersionUID = 7099057708183571937L;\nprivate final long timestamp = System.currentTimeMillis();\n\npublic ApplicationEvent(Object source) {\nsuper(source);\n}\n\npublic final long getTimestamp() {\nreturn this.timestamp;\n}\n}\n<\/code><\/pre>\n<p><strong>2.1.2 \u76d1\u542c\u5668<\/strong><\/p>\n<p>ApplicationListener\uff0c\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u8be5\u63a5\u53e3\u7ee7\u627f\u4e86EventListener\u63a5\u53e3\u3002EventListener\u63a5\u53e3\u662fJDK\u4e2d\u7684\uff0c\u5efa\u8bae\u6240\u6709\u7684\u4e8b\u4ef6\u76d1\u542c\u5668\u90fd\u5e94\u8be5\u7ee7\u627fEventListener\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042916360\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<pre><code>@FunctionalInterface\npublic interface ApplicationListener&lt;E extends ApplicationEvent&gt; extends EventListener {\nvoid onApplicationEvent(E var1);\n}\n<\/code><\/pre>\n<p><strong>2.1.3 \u4e8b\u4ef6\u53d1\u5e03\u5668<\/strong><\/p>\n<p>ApplicationEventPublisher\uff0cApplicationContext\u7ee7\u627f\u4e86\u8be5\u63a5\u53e3\uff0c\u5728ApplicationContext\u7684\u62bd\u8c61\u5b9e\u73b0\u7c7bAbstractApplicationContext\u4e2d\u505a\u4e86\u5b9e\u73b0\u4e0b\u9762\u6211\u4eec\u6765\u770b\u4e00\u4e0b<\/p>\n<p>org.springframework.context.support.AbstractApplicationContext#publishEvent(java.lang.Object, org.springframework.core.ResolvableType)<\/p>\n<pre><code>protected void publishEvent(Object event, @Nullable ResolvableType eventType) {\nAssert.notNull(event, &quot;Event must not be null&quot;);\n\nApplicationEvent applicationEvent;\nif (event instanceof ApplicationEvent) {\napplicationEvent = (ApplicationEvent) event;\n}\nelse {\napplicationEvent = new PayloadApplicationEvent&lt;&gt;(this, event);\nif (eventType == null) {\neventType = ((PayloadApplicationEvent&lt;?&gt;) applicationEvent).getResolvableType();\n}\n}\nif (this.earlyApplicationEvents != null) {\nthis.earlyApplicationEvents.add(applicationEvent);\n}\nelse {\n\/\/\u83b7\u53d6\u5f53\u524d\u6ce8\u5165\u7684\u53d1\u5e03\u5668,\u6267\u884c\u53d1\u5e03\u65b9\u6cd5\ngetApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);\n}\nif (this.parent != null) {\nif (this.parent instanceof AbstractApplicationContext) {\n((AbstractApplicationContext) this.parent).publishEvent(event, eventType);\n}\nelse {\nthis.parent.publishEvent(event);\n}\n}\n}\n<\/code><\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0cAbstractApplicationContext\u4e2dpublishEvent\u65b9\u6cd5\u6700\u7ec8\u6267\u884c\u53d1\u5e03\u4e8b\u4ef6\u7684\u662fApplicationEventMulticaster#multicastEvent\u65b9\u6cd5\uff0c\u4e0b\u9762\u6211\u4eec\u518d\u6765\u4e00\u8d77\u770b\u4e00\u4e0bmulticastEvent\u65b9\u6cd5<\/p>\n<pre><code>public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {\nResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));\nExecutor executor = getTaskExecutor();\n\/\/\u62ff\u5230\u6240\u6709\u7684\u76d1\u542c\u5668,\u5982\u679c\u5f02\u6b65\u6267\u884c\u5668\u4e0d\u4e3a\u7a7a,\u5f02\u6b65\u6267\u884c\nfor (ApplicationListener&lt;?&gt; listener : getApplicationListeners(event, type)) {\nif (executor != null) {\nexecutor.execute(() -&gt; invokeListener(listener, event));\n}\nelse {\n\/\/\u6267\u884c\u76d1\u542c\u65b9\u6cd5\ninvokeListener(listener, event);\n}\n}\n}\nprotected void invokeListener(ApplicationListener&lt;?&gt; listener, ApplicationEvent event) {\nErrorHandler errorHandler = getErrorHandler();\nif (errorHandler != null) {\ntry {\ndoInvokeListener(listener, event);\n}\ncatch (Throwable err) {\nerrorHandler.handleError(err);\n}\n}\nelse {\ndoInvokeListener(listener, event);\n}\n}\n\n\nprivate void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {\ntry {\nlistener.onApplicationEvent(event);\n}\ncatch (ClassCastException ex) {\nString msg = ex.getMessage();\nif (msg == null || matchesClassCastMessage(msg, event.getClass())) {\nLog logger = LogFactory.getLog(getClass());\nif (logger.isTraceEnabled()) {\nlogger.trace(&quot;Non-matching event type for listener: &quot; + listener, ex);\n}\n}\nelse {\nthrow ex;\n}\n}\n}\n<\/code><\/pre>\n<p>\u4e0a\u9762\u4ecb\u7ecd\u4e86\u975espring\u5185\u7f6e\u7684\u4e8b\u4ef6\u53d1\u5e03\u548c\u76d1\u542c\u6267\u884c\u6d41\u7a0b\u3002\u603b\u7ed3\u4e00\u4e0b<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042916361\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<ol>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u4e8b\u4ef6,\u8be5\u4e8b\u4ef6\u7ee7\u627fApplicationEvent<\/li>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u76d1\u542c\u5668,\u5b9e\u73b0ApplicationListener\u63a5\u53e3<\/li>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u4e8b\u4ef6\u53d1\u5e03\u5668,\u5b9e\u73b0ApplicationEventPublisherAware\u63a5\u53e3<\/li>\n<li>\u8c03\u7528ApplicationEventPublisher#publishEvent\u65b9\u6cd5.<\/li>\n<\/ol>\n<h4>2.2 spring\u4e8b\u4ef6\u5b9e\u73b0\u539f\u7406<\/h4>\n<p>\u4e0a\u9762\u6211\u4eec\u8bb2\u5230\u4e86spring\u4e8b\u4ef6\u7684\u53d1\u5e03\uff0c\u90a3\u4e48spring\u4e8b\u4ef6\u53d1\u5e03\u4e4b\u540e\uff0cspring\u662f\u5982\u4f55\u6839\u636e\u4e8b\u4ef6\u627e\u5230\u4e8b\u4ef6\u5bf9\u5e94\u7684\u76d1\u542c\u5668\u5462\uff1f\u6211\u4eec\u4e00\u8d77\u6765\u63a2\u7a76\u4e00\u4e0b\u3002<\/p>\n<p>spring\u7684\u5bb9\u5668\u521d\u59cb\u5316\u8fc7\u7a0b\u60f3\u5fc5\u5927\u5bb6\u90fd\u5df2\u5341\u5206\u4e86\u89e3\uff0c\u8fd9\u91cc\u5c31\u4e0d\u8fc7\u591a\u8d58\u8ff0\uff0c\u6211\u4eec\u76f4\u63a5\u770brefresh\u65b9\u6cd5\u5728refresh\u65b9\u6cd5\u4e2d\uff0c\u6709\u8fd9\u6837\u4e24\u4e2a\u65b9\u6cd5\uff0cinitApplicationEventMulticaster()\u548cregisterListeners()<\/p>\n<p>\u6211\u4eec\u5148\u6765\u770binitApplicationEventMulticaster()\u65b9\u6cd5<\/p>\n<p><strong>2.2.1 initApplicationEventMulticaster()<\/strong><\/p>\n<p>org.springframework.context.support.AbstractApplicationContext#initApplicationEventMulticaster<\/p>\n<pre><code>public static final String APPLICATION_EVENT_MULTICASTER_BEAN_NAME = &quot;applicationEventMulticaster&quot;;\nprotected void initApplicationEventMulticaster() {\n\/\/\u83b7\u5f97beanFactory\nConfigurableListableBeanFactory beanFactory = getBeanFactory();\n\/\/BeanFactory\u4e2d\u662f\u5426\u6709ApplicationEventMulticaster\nif (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {\nthis.applicationEventMulticaster =\nbeanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);\nif (logger.isTraceEnabled()) {\nlogger.trace(&quot;Using ApplicationEventMulticaster [&quot; + this.applicationEventMulticaster + &quot;]&quot;);\n}\n}\nelse {\n\/\/\u5982\u679cBeanFactory\u4e2d\u4e0d\u5b58\u5728\uff0c\u5c31\u521b\u5efa\u4e00\u4e2aSimpleApplicationEventMulticaster\nthis.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);\nbeanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);\nif (logger.isTraceEnabled()) {\nlogger.trace(&quot;No '&quot; + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + &quot;' bean, using &quot; +\n&quot;[&quot; + this.applicationEventMulticaster.getClass().getSimpleName() + &quot;]&quot;);\n}\n}\n}\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0cspring\u5148\u4eceBeanFactory\u4e2d\u83b7\u53d6applicationEventMulticaster\u5982\u679c\u4e3a\u7a7a\uff0c\u5219\u76f4\u63a5\u521b\u5efaSimpleApplicationEventMulticaster<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042916362\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>2.2.2 registerListeners()<\/strong><\/p>\n<p>org.springframework.context.support.AbstractApplicationContext#registerListeners<\/p>\n<p>registerListeners \u662f\u5c06\u5404\u79cd\u5b9e\u73b0\u4e86 ApplicationListener \u7684\u76d1\u542c\u5668\u6ce8\u518c\u5230 ApplicationEventMulticaster \u4e8b\u4ef6\u5e7f\u64ad\u5668\u4e2d<\/p>\n<pre><code>\nprotected void registerListeners() {\nfor (ApplicationListener&lt;?&gt; listener : getApplicationListeners()) {\ngetApplicationEventMulticaster().addApplicationListener(listener);\n}\n\nString[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);\nfor (String listenerBeanName : listenerBeanNames) {\n\/\/\u628a\u76d1\u542c\u5668\u6ce8\u518c\u5230\u4e8b\u4ef6\u53d1\u5e03\u5668\u4e0a\ngetApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);\n}\n\nSet&lt;ApplicationEvent&gt; earlyEventsToProcess = this.earlyApplicationEvents;\nthis.earlyApplicationEvents = null;\nif (!CollectionUtils.isEmpty(earlyEventsToProcess)) {\n\/\/\u5982\u679c\u5185\u7f6e\u76d1\u542c\u4e8b\u4ef6\u96c6\u5408\u4e0d\u4e3a\u7a7a\nfor (ApplicationEvent earlyEvent : earlyEventsToProcess) {\n\/\/\u6267\u884cspring\u5185\u7f6e\u7684\u76d1\u542c\u65b9\u6cd5\ngetApplicationEventMulticaster().multicastEvent(earlyEvent);\n}\n}\n}\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u89e3\u91ca\u4e00\u4e0bearlyApplicationListeners<\/p>\n<p>earlyApplicationListeners\u7684\u672c\u8d28\u8fd8\u662fApplicationListener\u3002Spring\u5355\u4f8bBan\u7684\u5b9e\u4f8b\u5316\u662f\u5728Refresh\u9636\u6bb5\u5b9e\u4f8b\u5316\u7684\uff0c\u90a3\u4e48\u7528\u6237\u81ea\u5b9a\u4e49\u7684\u4e00\u4e9bApplicationListener\u7ec4\u4ef6\u81ea\u7136\u4e5f\u662f\u5728\u8fd9\u4e2a\u9636\u6bb5\u624d\u521d\u59cb\u5316\uff0c\u4f46\u662fSpring\u5bb9\u5668\u542f\u52a8\u8fc7\u7a0b\u4e2d\uff0c\u5728Refresh\u5b8c\u6210\u4e4b\u524d\u8fd8\u6709\u5f88\u591a\u4e8b\u4ef6\uff1a\u5982Spring\u4e0a\u4e0b\u6587\u73af\u5883\u51c6\u5907\u7b49\u4e8b\u4ef6\uff0c\u8fd9\u4e9b\u4e8b\u4ef6\u53c8\u662fSpring\u5bb9\u5668\u542f\u52a8\u5fc5\u987b\u8981\u76d1\u542c\u7684\u3002\u6240\u4ee5Spring\u5b9a\u4e49\u4e86\u4e00\u4e2aearlyApplicationListeners\u96c6\u5408\uff0c\u8fd9\u4e2a\u96c6\u5408\u4e2d\u7684Listener\u5728factories\u6587\u4ef6\u4e2d\u5b9a\u4e49\u597d\uff0c\u5728\u5bb9\u5668Refresh\u4e4b\u524d\u9884\u5148\u5b9e\u4f8b\u5316\u597d\uff0c\u7136\u540e\u5c31\u53ef\u4ee5\u76d1\u542cSpring\u5bb9\u5668\u542f\u52a8\u8fc7\u7a0b\u4e2d\u7684\u6240\u6709\u4e8b\u4ef6\u3002<\/p>\n<p>\u5f53registerListeners\u65b9\u6cd5\u6267\u884c\u5b8c\u6210,\u6211\u4eec\u7684\u76d1\u542c\u5668\u5df2\u7ecf\u6dfb\u52a0\u5230\u591a\u64ad\u5668SimpleApplicationEventMulticaster\u4e2d\u4e86\uff0c\u5e76\u4e14earlyEvent \u65e9\u671f\u4e8b\u4ef6\u4e5f\u5df2\u7ecf\u6267\u884c\u5b8c\u6bd5\u3002\u4f46\u662f\u6211\u4eec\u53d1\u73b0,\u5982\u679c\u81ea\u5b9a\u4e49\u4e86\u4e00\u4e2a\u76d1\u542c\u5668\u53bb\u76d1\u542cspring\u5185\u7f6e\u7684\u4e8b\u4ef6,\u6b64\u65f6\u5e76\u6ca1\u6709\u88ab\u6267\u884c\uff0c\u90a3\u6211\u4eec\u6ce8\u518c\u7684\u76d1\u542c\u5668\u662f\u5982\u4f55\u88ab\u6267\u884c\u7684\u5462\uff1f\u7b54\u6848\u5728finishRefresh\u65b9\u6cd5\u4e2d\u3002<\/p>\n<p><strong>2.2.3 finishRefresh<\/strong><\/p>\n<p>org.springframework.context.support.AbstractApplicationContext#finishRefresh<\/p>\n<pre><code>protected void finishRefresh() {\nclearResourceCaches();\n\ninitLifecycleProcessor();\n\ngetLifecycleProcessor().onRefresh();\n\/\/\u5bb9\u5668\u4e2d\u7684\u7c7b\u5168\u90e8\u521d\u59cb\u5316\u5b8c\u6bd5\u540e\uff0c\u89e6\u53d1\u5237\u65b0\u4e8b\u4ef6\npublishEvent(new ContextRefreshedEvent(this));\n\nLiveBeansView.registerApplicationContext(this);\n}\n<\/code><\/pre>\n<p>\u5982\u679c\u6211\u4eec\u60f3\u8981\u5b9e\u73b0\u5728spring\u5bb9\u5668\u4e2d\u6240\u6709bean\u521b\u5efa\u5b8c\u6210\u540e\u505a\u4e00\u4e9b\u6269\u5c55\u529f\u80fd\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5b9e\u73b0ApplicationListener\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5b9e\u73b0\u5176\u529f\u80fd\u4e86\u3002\u81f3\u6b64\uff0cSpring\u4e2d\u540c\u6b65\u7684\u4e8b\u4ef6\u76d1\u542c\u53d1\u5e03\u6a21\u5f0f\u6211\u4eec\u5c31\u8bb2\u89e3\u5b8c\u4e86\uff0c\u5f53\u7136Spring\u8fd8\u652f\u6301\u5f02\u6b65\u7684\u6d88\u606f\u76d1\u542c\u6267\u884c\u673a\u5236\u3002<\/p>\n<p><strong>2.2.4 spring\u4e2d\u5f02\u6b65\u7684\u76d1\u542c\u6267\u884c\u673a\u5236<\/strong><\/p>\n<p>\u6211\u4eec\u56de\u8fc7\u5934\u6765\u770b\u4e00\u4e0bApplicationEventMulticaster#pushEvent\u65b9\u6cd5<\/p>\n<pre><code>protected void publishEvent(Object event, @Nullable ResolvableType eventType) {\nAssert.notNull(event, &quot;Event must not be null&quot;);\n\n\nApplicationEvent applicationEvent;\nif (event instanceof ApplicationEvent) {\napplicationEvent = (ApplicationEvent) event;\n}\nelse {\napplicationEvent = new PayloadApplicationEvent&lt;&gt;(this, event);\nif (eventType == null) {\neventType = ((PayloadApplicationEvent&lt;?&gt;) applicationEvent).getResolvableType();\n}\n}\nif (this.earlyApplicationEvents != null) {\nthis.earlyApplicationEvents.add(applicationEvent);\n}\nelse {\n\/\/\u83b7\u53d6\u5f53\u524d\u6ce8\u5165\u7684\u53d1\u5e03\u5668,\u6267\u884c\u53d1\u5e03\u65b9\u6cd5\ngetApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);\n}\nif (this.parent != null) {\nif (this.parent instanceof AbstractApplicationContext) {\n((AbstractApplicationContext) this.parent).publishEvent(event, eventType);\n}\nelse {\nthis.parent.publishEvent(event);\n}\n}\n<\/code><\/pre>\n<p>\u6700\u7ec8\u6267\u884c\u53d1\u5e03\u4e8b\u4ef6\u7684\u662fApplicationEventMulticaster#multicastEvent\u65b9\u6cd5\uff0c\u4e0b\u9762\u6211\u4eec\u518d\u6765\u4e00\u8d77\u770b\u4e00\u4e0bmulticastEvent\u65b9\u6cd5<\/p>\n<pre><code>public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {\n   ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));\n   Executor executor = getTaskExecutor();\n\/\/\u62ff\u5230\u6240\u6709\u7684\u76d1\u542c\u5668,\u5982\u679c\u5f02\u6b65\u6267\u884c\u5668\u4e0d\u4e3a\u7a7a,\u5f02\u6b65\u6267\u884c\n   for (ApplicationListener&lt;?&gt; listener : getApplicationListeners(event, type)) {\n      if (executor != null) {\n         executor.execute(() -&gt; invokeListener(listener, event));\n      }\n      else {\n\/\/\u6267\u884c\u76d1\u542c\u65b9\u6cd5\n         invokeListener(listener, event);\n      }\n   }\n}\n<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5f02\u6b65\u4e8b\u4ef6\u901a\u77e5\u4e3b\u8981\u4f9d\u9760SimpleApplicationEventMulticaster \u7c7b\u4e2d\u7684Executor\u53bb\u5b9e\u73b0\u7684\uff0c\u5982\u679c\u8fd9\u4e2a\u53d8\u91cf\u4e0d\u914d\u7f6e\u7684\u8bdd\u9ed8\u8ba4\u4e8b\u4ef6\u901a\u77e5\u662f\u540c\u6b65\u7684\uff0c \u5426\u5219\u5c31\u662f\u5f02\u6b65\u901a\u77e5\u4e86\uff0c\u8981\u5b9e\u73b0\u540c\u65f6\u652f\u6301\u540c\u6b65\u901a\u77e5\u548c\u5f02\u6b65\u901a\u77e5\u5c31\u5f97\u4ece\u8fd9\u91cc\u4e0b\u624b\uff1b\u6211\u4eec\u4e0a\u6587\u5df2\u7ecf\u5206\u6790\u8fc7\u4e86\u5728initApplicationEventMulticaster\u65b9\u6cd5\u4e2d\u6709\u8fd9\u6837\u4e00\u6bb5\u4ee3\u7801<\/p>\n<pre><code>if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {\nthis.applicationEventMulticaster =\nbeanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);\nif (logger.isTraceEnabled()) {\nlogger.trace(&quot;Using ApplicationEventMulticaster [&quot; + this.applicationEventMulticaster + &quot;]&quot;);\n}\n}\n<\/code><\/pre>\n<p>\u5982\u679cBeanFactory\u4e2d\u5df2\u7ecf\u6709\u4e86SimpleApplicationEventMulticaster\u5219\u4e0d\u4f1a\u91cd\u65b0\u521b\u5efa\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u518dspring\u4e2d\u6ce8\u518c\u4e00\u4e2aSimpleApplicationEventMulticaster\u5e76\u4e14\u5411\u5176\u4e2d\u6ce8\u5165\u5bf9\u5e94\u7684Executor\u8fd9\u6837\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u5f02\u6b65\u6267\u884c\u76d1\u542c\u7684SimpleApplicationEventMulticaster\u4e86\uff0c\u6211\u4eec\u7684\u901a\u77e5\u5c31\u4f1a\u901a\u8fc7Executor\u5f02\u6b65\u6267\u884c\u3002\u8fd9\u6837\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u4e8b\u4ef6\u53d1\u5e03\u7684\u6548\u7387\u3002<\/p>\n<p>\u5728springboot\u9879\u76ee\u4e2d\u6211\u4eec\u53ef\u4ee5\u589e\u52a0\u4e00\u4e2a\u914d\u7f6e\u7c7b\u6765\u5b9e\u73b0<\/p>\n<pre><code>@Configuration\n@EnableAsync\npublic class Config {\n@Bean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME)\npublic SimpleApplicationEventMulticaster simpleApplicationEventMulticaster(){\nSimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster();\nsimpleApplicationEventMulticaster.setTaskExecutor(taskExecutor());\nreturn simpleApplicationEventMulticaster;\n}\n\n@Bean\npublic TaskExecutor taskExecutor() {\nThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();\nexecutor.setCorePoolSize(5);\nexecutor.setMaxPoolSize(20);\nexecutor.setQueueCapacity(100);\nexecutor.setKeepAliveSeconds(300);\nexecutor.setThreadNamePrefix(&quot;thread-&quot;);\nexecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());\nexecutor.setWaitForTasksToCompleteOnShutdown(true);\nreturn executor;\n}\n}\n<\/code><\/pre>\n<p>spring\u9879\u76ee\u4e2d\u6211\u4eec\u4e5f\u53ef\u4ee5\u589e\u52a0\u5982\u4e0bxml\u914d\u7f6e<\/p>\n<pre><code>&lt;!--\u5b9a\u4e49\u4e8b\u4ef6\u5f02\u6b65\u5904\u7406--&gt;\n&lt;bean id=&quot;commonTaskExecutor&quot;\nclass=&quot;org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor&quot;&gt;\n&lt;!-- \u7ebf\u7a0b\u6c60\u7ef4\u6301\u5904\u4e8eKeep-alive\u72b6\u6001\u7684\u7ebf\u7a0b\u6570\u91cf\u3002\u5982\u679c\u8bbe\u7f6e\u4e86allowCoreThreadTimeOut\u4e3atrue\uff0c\u8be5\u503c\u53ef\u80fd\u4e3a0\u3002\n\u5e76\u53d1\u7ebf\u7a0b\u6570,\u60f3\u8fbe\u5230\u771f\u6b63\u7684\u5e76\u53d1\u6548\u679c,\u6700\u597d\u5bf9\u5e94CPU\u7684\u7ebf\u7a0b\u6570\u53ca\u6838\u5fc3\u6570 --&gt;\n&lt;property name=&quot;corePoolSize&quot; value=&quot;5&quot; \/&gt;\n&lt;!-- \u6700\u5927\u7ebf\u7a0b\u6c60\u5bb9\u91cf --&gt;\n&lt;property name=&quot;maxPoolSize&quot; value=&quot;20&quot; \/&gt;\n&lt;!-- \u8d85\u8fc7\u6700\u5927\u7ebf\u7a0b\u6c60\u5bb9\u91cf\u540e,\u5141\u8bb8\u7684\u7ebf\u7a0b\u961f\u5217\u6570 --&gt;\n&lt;property name=&quot;queueCapacity&quot; value=&quot;100&quot; \/&gt;\n&lt;!-- \u7ebf\u7a0b\u6c60\u7ef4\u62a4\u7ebf\u7a0b\u6240\u5141\u8bb8\u7684\u7a7a\u95f2\u65f6\u95f4 .\u5355\u4f4d\u6beb\u79d2\uff0c\u9ed8\u8ba4\u4e3a60s\uff0c\u8d85\u8fc7\u8fd9\u4e2a\u65f6\u95f4\u540e\u4f1a\u5c06\u5927\u4e8ecorePoolSize\u7684\u7ebf\u7a0b\u5173\u95ed\uff0c\u4fdd\u6301corePoolSize\u7684\u4e2a\u6570 --&gt;\n&lt;property name=&quot;keepAliveSeconds&quot; value=&quot;300&quot; \/&gt;\n&lt;!-- \u5141\u8bb8\u6838\u5fc3\u7ebf\u7a0b\u8d85\u65f6: false\uff08\u9ed8\u8ba4\u503c\uff09\u4e0d\u5141\u8bb8\u8d85\u65f6\uff0c\u54ea\u6015\u7a7a\u95f2\uff1btrue\u5219\u4f7f\u7528keepAliveSeconds\u6765\u63a7\u5236\u7b49\u5f85\u8d85\u65f6\u65f6\u95f4\uff0c\u6700\u7ec8corePoolSize\u7684\u4e2a\u6570\u53ef\u80fd\u4e3a0 --&gt;\n&lt;property name=&quot;allowCoreThreadTimeOut&quot; value=&quot;true&quot; \/&gt;\n\n&lt;!-- \u7ebf\u7a0b\u6c60\u5bf9\u62d2\u7edd\u4efb\u52a1(\u65e0\u7ebf\u7a0b\u53ef\u7528)\u7684\u5904\u7406\u7b56\u7565 --&gt;\n&lt;property name=&quot;rejectedExecutionHandler&quot;&gt;\n&lt;bean class=&quot;java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy&quot; \/&gt;\n&lt;!-- java.util.concurrent.ThreadPoolExecutor$CallerRunsPolicy:\u4e3b\u7ebf\u7a0b\u76f4\u63a5\u6267\u884c\u8be5\u4efb\u52a1\uff0c\u6267\u884c\u5b8c\u4e4b\u540e\u5c1d\u8bd5\u6dfb\u52a0\u4e0b\u4e00\u4e2a\u4efb\u52a1\u5230\u7ebf\u7a0b\u6c60\u4e2d --&gt;\n&lt;!-- java.util.concurrent.ThreadPoolExecutor$AbortPolicy:\u76f4\u63a5\u629b\u51fajava.util.concurrent.RejectedExecutionException\u5f02\u5e38 --&gt;\n&lt;\/property&gt;\n&lt;\/bean&gt;\n\n&lt;!--\u540d\u5b57\u5fc5\u987b\u662fapplicationEventMulticaster\uff0c\u56e0\u4e3aAbstractApplicationContext\u9ed8\u8ba4\u627e\u4e2a--&gt;\n&lt;bean id=&quot;applicationEventMulticaster&quot; class=&quot;org.springframework.context.event.SimpleApplicationEventMulticaster&quot;&gt;\n&lt;!--\u6ce8\u5165\u4efb\u52a1\u6267\u884c\u5668 \u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u5f02\u6b65\u8c03\u7528--&gt;\n&lt;property name=&quot;taskExecutor&quot; ref=&quot;commonTaskExecutor&quot;&gt;&lt;\/property&gt;\n&lt;\/bean&gt;\n<\/code><\/pre>\n<h3>3 \u5c0f\u7ed3<\/h3>\n<p>\u672c\u6587\u4e3b\u8981\u8bb2\u89e3\u4e86\u89c2\u5bdf\u8005\u6a21\u5f0f\u5728spring\u4e2d\u7684\u5e94\u7528\u53ca\u4e8b\u4ef6\u76d1\u542c\u673a\u5236\uff0cJDK \u4e5f\u6709\u5b9e\u73b0\u63d0\u4f9b\u4e8b\u4ef6\u76d1\u542c\u673a\u5236Spring \u7684\u4e8b\u4ef6\u673a\u5236\u4e5f\u662f\u57fa\u4e8eJDK \u6765\u6269\u5c55\u7684\u3002Spring \u7684\u4e8b\u4ef6\u673a\u5236\u9ed8\u8ba4\u662f\u540c\u6b65\u963b\u585e\u7684\uff0c\u60f3\u8981\u63d0\u5347\u5bf9\u5e94\u7684\u6548\u7387\u8981\u8003\u8651\u5f02\u6b65\u4e8b\u4ef6\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042916357\">https:\/\/segmentfault.com\/a\/1190000042916357<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u738b\u5b50\u6e90 1 \u89c2\u5bdf\u8005\u6a21\u5f0f\u7b80\u4ecb 1.1 \u5b9a\u4e49 \u6307\u591a\u4e2a\u5bf9\u8c61\u95f4\u5b58\u5728\u4e00\u5bf9\u591a\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u5f53\u4e00\u4e2a\u5bf9\u8c61\u7684\u72b6\u6001\u53d1\u751f\u6539\u53d8\u65f6\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006926","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006926"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006926\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}