作者:京東科技 李君
書(shū)接上文,前面在 Spring 應(yīng)用合并之路(一):摸石頭過(guò)河 介紹了幾種不成功的經(jīng)驗(yàn),下面繼續(xù)折騰…
四、倉(cāng)庫(kù)合并,獨(dú)立容器
在經(jīng)歷了上面的嘗試,在同事為啥不搞兩個(gè)獨(dú)立的容器提醒下,決定拋開(kāi) Spring Boot 內(nèi)置的父子容器方案,完全自己實(shí)現(xiàn)父子容器。
如何加載 web 項(xiàng)目?
現(xiàn)在的難題只有一個(gè):如何加載 web 項(xiàng)目?加載完成后,如何持續(xù)持有 web 項(xiàng)目?經(jīng)過(guò)思考后,可以創(chuàng)建一個(gè) boot 項(xiàng)目的 Spring Bean,在該 Bean 中加載并持有 web 項(xiàng)目的容器。由于 Spring Bean 默認(rèn)是單例的,并且會(huì)伴隨 Spring 容器長(zhǎng)期存活,就可以保證 web 容器持久存活。結(jié)合 Spring 擴(kuò)展點(diǎn)概覽及實(shí)踐 中介紹的 Spring 擴(kuò)展點(diǎn),有兩個(gè)地方可以利用:
1.可以利用 ApplicationContextAware 獲取 boot 容器的 ApplicationContext 實(shí)例,這樣就可以實(shí)現(xiàn)自己實(shí)現(xiàn)的父子容器;
2.可以利用 ApplicationListener 獲取 ContextRefreshedEvent 事件,該事件表示容器已經(jīng)完成初始化,可以提供服務(wù)。在監(jiān)聽(tīng)到該事件后,來(lái)進(jìn)行 web 容器的加載。
思路確定后,代碼實(shí)現(xiàn)就很簡(jiǎn)單了:
package com.diguage.demo.boot.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; /** * @author D瓜哥 · https://www.diguage.com */ @Component public class WebLoaderListener implements ApplicationContextAware, ApplicationListener { private static final Logger logger = LoggerFactory.getLogger(WebLoaderListener.class); /** * 父容器,加載 boot 項(xiàng)目 */ private static ApplicationContext parentContext; /** * 子容器,加載 web 項(xiàng)目 */ private static ApplicationContext childContext; @Override public void setApplicationContext(ApplicationContext ctx) throws BeansException { WebLoaderListener.parentContext = ctx; } @Override public void onApplicationEvent(ApplicationEvent event) { logger.info("receive application event: {}", event); if (event instanceof ContextRefreshedEvent) { WebLoaderListener.childContext = new ClassPathXmlApplicationContext( new String[]{"classpath:web/spring-cfg.xml"}, WebLoaderListener.parentContext); } } }
容器重復(fù)加載的問(wèn)題
這次自己實(shí)現(xiàn)的父子容器,如同設(shè)想的那樣,沒(méi)有同名 Bean 的檢查,省去了很多麻煩。但是,觀察日志,會(huì)發(fā)現(xiàn) com.diguage.demo.boot.config.WebLoaderListener#onApplicationEvent 方法被兩次執(zhí)行,也就是監(jiān)聽(tīng)到了兩次 ContextRefreshedEvent 事件,導(dǎo)致 web 容器會(huì)被加載兩次。由于項(xiàng)目的 RPC 服務(wù)不能重復(fù)注冊(cè),第二次加載拋出異常,導(dǎo)致啟動(dòng)失敗。
最初,懷疑是 web 容器,加載了 WebLoaderListener,但是跟蹤代碼,沒(méi)有發(fā)現(xiàn) childContext 容器中有 WebLoaderListener 的相關(guān) Bean。
昨天做了個(gè)小實(shí)驗(yàn),又調(diào)試了一下 Spring 的源代碼,發(fā)現(xiàn)了其中的奧秘。直接貼代碼吧:
SPRING/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java
/** * Publish the given event to all listeners. * This is the internal delegate that all other {@code publishEvent} * methods refer to. It is not meant to be called directly but rather serves * as a propagation mechanism between application contexts in a hierarchy, * potentially overridden in subclasses for a custom propagation arrangement. * @param event the event to publish (may be an {@link ApplicationEvent} * or a payload object to be turned into a {@link PayloadApplicationEvent}) * @param typeHint the resolved event type, if known. * The implementation of this method also tolerates a payload type hint for * a payload object to be turned into a {@link PayloadApplicationEvent}. * However, the recommended way is to construct an actual event object via * {@link PayloadApplicationEvent#PayloadApplicationEvent(Object, Object, ResolvableType)} * instead for such scenarios. * @since 4.2 * @see ApplicationEventMulticaster#multicastEvent(ApplicationEvent, ResolvableType) */ protected void publishEvent(Object event, @Nullable ResolvableType typeHint) { Assert.notNull(event, "Event must not be null"); ResolvableType eventType = null; // Decorate event as an ApplicationEvent if necessary ApplicationEvent applicationEvent; if (event instanceof ApplicationEvent applEvent) { applicationEvent = applEvent; eventType = typeHint; } else { ResolvableType payloadType = null; if (typeHint != null && ApplicationEvent.class.isAssignableFrom(typeHint.toClass())) { eventType = typeHint; } else { payloadType = typeHint; } applicationEvent = new PayloadApplicationEvent?>(this, event, payloadType); } // Determine event type only once (for multicast and parent publish) if (eventType == null) { eventType = ResolvableType.forInstance(applicationEvent); if (typeHint == null) { typeHint = eventType; } } // Multicast right now if possible - or lazily once the multicaster is initialized if (this.earlyApplicationEvents != null) { this.earlyApplicationEvents.add(applicationEvent); } else if (this.applicationEventMulticaster != null) { this.applicationEventMulticaster.multicastEvent(applicationEvent, eventType); } // Publish event via parent context as well... // 如果有父容器,則也將事件發(fā)布給父容器。 if (this.parent != null) { if (this.parent instanceof AbstractApplicationContext abstractApplicationContext) { abstractApplicationContext.publishEvent(event, typeHint); } else { this.parent.publishEvent(event); } } }
在 publishEvent 方法的最后,如果父容器不為 null 的情況下,則也會(huì)向父容器廣播容器的相關(guān)事件。
看到這里就清楚了,不是 web 容器持有了 WebLoaderListener 這個(gè) Bean,而是 web 容器主動(dòng)向父容器廣播了 ContextRefreshedEvent 事件。
容器銷毀
除了上述問(wèn)題,還有一個(gè)問(wèn)題需要思考:如何銷毀 web 容器?如果不能銷毀容器,會(huì)有一些意想不到的問(wèn)題。比如,注冊(cè)中心的 RPC 提供方不能及時(shí)銷毀等等。
這里的解決方案也比較簡(jiǎn)單:同樣基于事件監(jiān)聽(tīng),Spring 容器銷毀會(huì)有 ContextClosedEvent 事件,在 WebLoaderListener 中監(jiān)聽(tīng)該事件,然后調(diào)用 AbstractApplicationContext#close 方法就可以完成 Spring 容器的銷毀工作。
父子容器加載及銷毀
結(jié)合上面的所有論述,完整的代碼如下:
package com.diguage.demo.boot.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; import java.util.Objects; /** * 基于事件監(jiān)聽(tīng)的 web 項(xiàng)目加載器 * * @author D瓜哥 · https://www.diguage.com */ @Component public class WebLoaderListener implements ApplicationContextAware, ApplicationListener { private static final Logger logger = LoggerFactory.getLogger(WebLoaderListener.class); /** * 父容器,加載 boot 項(xiàng)目 */ private static ApplicationContext parentContext; /** * 子容器,加載 web 項(xiàng)目 */ private static ClassPathXmlApplicationContext childContext; @Override public void setApplicationContext(ApplicationContext ctx) throws BeansException { WebLoaderListener.parentContext = ctx; } /** * 事件監(jiān)聽(tīng) * * @author D瓜哥 · https://www.diguage.com */ @Override public void onApplicationEvent(ApplicationEvent event) { logger.info("receive application event: {}", event); if (event instanceof ContextRefreshedEvent refreshedEvent) { ApplicationContext context = refreshedEvent.getApplicationContext(); if (Objects.equals(WebLoaderListener.parentContext, context)) { // 加載 web 容器 WebLoaderListener.childContext = new ClassPathXmlApplicationContext( new String[]{"classpath:web/spring-cfg.xml"}, WebLoaderListener.parentContext); } } else if (event instanceof ContextClosedEvent) { // 處理容器銷毀事件 if (Objects.nonNull(WebLoaderListener.childContext)) { synchronized (WebLoaderListener.class) { if (Objects.nonNull(WebLoaderListener.childContext)) { AbstractApplicationContext ctx = WebLoaderListener.childContext; WebLoaderListener.childContext = null; ctx.close(); } } } } } }
五、參考資料
1.?Spring 擴(kuò)展點(diǎn)概覽及實(shí)踐 - "地瓜哥"博客網(wǎng)?
2.?Context Hierarchy with the Spring Boot Fluent Builder API?
3.?How to revert initial git commit?
審核編輯 黃宇
-
spring
+關(guān)注
關(guān)注
0文章
340瀏覽量
14338
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論