当前位置: 首页 > news >正文

西安注册公司在哪个网站系统wordpress怎么添加友链

西安注册公司在哪个网站系统,wordpress怎么添加友链,good work wordpress,北京哪个网站建设最好书接上文#xff0c;前面在 [Spring 应用合并之路#xff08;一#xff09;#xff1a;摸石头过河]介绍了几种不成功的经验#xff0c;下面继续折腾… 四、仓库合并#xff0c;独立容器 在经历了上面的尝试#xff0c;在同事为啥不搞两个独立的容器提醒下#xff0c;…书接上文前面在 [Spring 应用合并之路一摸石头过河]介绍了几种不成功的经验下面继续折腾… 四、仓库合并独立容器 在经历了上面的尝试在同事为啥不搞两个独立的容器提醒下决定抛开 Spring Boot 内置的父子容器方案完全自己实现父子容器。 如何加载 web 项目 现在的难题只有一个如何加载 web 项目加载完成后如何持续持有 web 项目经过思考后可以创建一个 boot 项目的 Spring Bean在该 Bean 中加载并持有 web 项目的容器。由于 Spring Bean 默认是单例的并且会伴随 Spring 容器长期存活就可以保证 web 容器持久存活。结合 Spring 扩展点概览及实践 中介绍的 Spring 扩展点有两个地方可以利用 1.可以利用 ApplicationContextAware 获取 boot 容器的 ApplicationContext 实例这样就可以实现自己实现的父子容器 2.可以利用 ApplicationListener 获取 ContextRefreshedEvent 事件该事件表示容器已经完成初始化可以提供服务。在监听到该事件后来进行 web 容器的加载。 思路确定后代码实现就很简单了 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,ApplicationListenerApplicationEvent {private static final Logger logger LoggerFactory.getLogger(WebLoaderListener.class);/*** 父容器加载 boot 项目*/private static ApplicationContext parentContext;/*** 子容器加载 web 项目*/private static ApplicationContext childContext;Overridepublic void setApplicationContext(ApplicationContext ctx) throws BeansException {WebLoaderListener.parentContext ctx;}Overridepublic 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);}} } 容器重复加载的问题 这次自己实现的父子容器如同设想的那样没有同名 Bean 的检查省去了很多麻烦。但是观察日志会发现 com.diguage.demo.boot.config.WebLoaderListener#onApplicationEvent 方法被两次执行也就是监听到了两次 ContextRefreshedEvent 事件导致 web 容器会被加载两次。由于项目的 RPC 服务不能重复注册第二次加载抛出异常导致启动失败。 最初怀疑是 web 容器加载了 WebLoaderListener但是跟踪代码没有发现 childContext 容器中有 WebLoaderListener 的相关 Bean。 昨天做了个小实验又调试了一下 Spring 的源代码发现了其中的奥秘。直接贴代码吧 SPRING/spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java /*** Publish the given event to all listeners.* pThis 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 necessaryApplicationEvent 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 initializedif (this.earlyApplicationEvents ! null) {this.earlyApplicationEvents.add(applicationEvent);}else if (this.applicationEventMulticaster ! null) {this.applicationEventMulticaster.multicastEvent(applicationEvent, eventType);}// Publish event via parent context as well...// 如果有父容器则也将事件发布给父容器。if (this.parent ! null) {if (this.parent instanceof AbstractApplicationContext abstractApplicationContext) {abstractApplicationContext.publishEvent(event, typeHint);}else {this.parent.publishEvent(event);}} } 在 publishEvent 方法的最后如果父容器不为 null 的情况下则也会向父容器广播容器的相关事件。 看到这里就清楚了不是 web 容器持有了 WebLoaderListener 这个 Bean而是 web 容器主动向父容器广播了 ContextRefreshedEvent 事件。 容器销毁 除了上述问题还有一个问题需要思考如何销毁 web 容器如果不能销毁容器会有一些意想不到的问题。比如注册中心的 RPC 提供方不能及时销毁等等。 这里的解决方案也比较简单同样基于事件监听Spring 容器销毁会有 ContextClosedEvent 事件在 WebLoaderListener 中监听该事件然后调用 AbstractApplicationContext#close 方法就可以完成 Spring 容器的销毁工作。 父子容器加载及销毁 结合上面的所有论述完整的代码如下 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;/*** 基于事件监听的 web 项目加载器** author D瓜哥 · https://www.diguage.com*/ Component public class WebLoaderListener implements ApplicationContextAware,ApplicationListenerApplicationEvent {private static final Logger logger LoggerFactory.getLogger(WebLoaderListener.class);/*** 父容器加载 boot 项目*/private static ApplicationContext parentContext;/*** 子容器加载 web 项目*/private static ClassPathXmlApplicationContext childContext;Overridepublic void setApplicationContext(ApplicationContext ctx) throws BeansException {WebLoaderListener.parentContext ctx;}/*** 事件监听** author D瓜哥 · https://www.diguage.com*/Overridepublic 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 扩展点概览及实践 - 地瓜哥博客网 2.Context Hierarchy with the Spring Boot Fluent Builder API 3.How to revert initial git commit? 作者京东科技 李君 来源京东云开发者社区 转载请注明来源
http://www.zqtcl.cn/news/950681/

相关文章:

  • 黄河道网站建设网站设计标语
  • 企业网站建设范文wordpress 5.1
  • 网站 河北 备案 慢设计一个营销方案
  • 网站建设培训合肥品牌设计案例
  • 建网站注册免费云服务器
  • 可以做网站的公司有哪些聊天软件开发厂家有哪些
  • 正规网站建设公司一般要多少钱婚纱网站有哪些
  • 企业网站开发目的和意义住房和城乡建设厅官网查询
  • 直播一级a做爰片免费网站wordpress 模板 使用
  • 网站开发中期检查优质的菏泽网站建设
  • 建设网站号码在线html编辑
  • 品牌型网站制作有哪些公司石家庄广告制作公司
  • 做网站赚几百万网站效果图怎么做的
  • 哪些网站做企业招聘不要花钱wordpress底部导航代码
  • 怎么用链接进自己做的网站企业组织架构
  • 建设新网站征求意见网站设计佛山
  • 重庆建设造价工程信息网站东莞电商页面设计公司
  • 乔拓云智能建站官网登录入口怎么样做网站卖农产品
  • 怎么维护好网站网站的域名每年都要续费
  • 运动网站模板佛山三水区有没有网站建设公司
  • 申请微官网的网站国外域名注册商网站
  • 集团公司网站建设建设中学校园网站的来源
  • 产品展示网站含后台网站模板下载网站开发什么语言好
  • 做知乎网站的图片如何设计好网站
  • 广州企业网站推广织梦学校网站模板
  • 国内响应式网站案例深圳住房和城乡建设局网站
  • 网页制作网站首页中国建筑论坛网
  • 众创空间网站建设少年宫网站建设模块
  • 企业营销型网站的内容科技公司取名大全
  • 哈尔滨云建站模板投资公司的钱从哪里来