【问题标题】:Run and close child context from parent app从父应用程序运行和关闭子上下文
【发布时间】:2018-06-16 12:02:45
【问题描述】:

我正在使用 spring 应用程序构建器从父级运行子上下文应用程序(spring Cloud 任务)。我还将正在运行的父上下文传递给子应用程序的构建器 - 它定义了 DataSource bean,我希望它也被任务使用。

我的问题是当子上下文关闭时,所有父 bean 也被销毁。 难道我做错了什么?

如何与子上下文共享来自父上下文的 bean 并在子上下文退出时仍然保持它们的活动。

@Component
public class ClassInParentContext{

 @Autowired
 private ConfigurableApplicationContext parentAppCtx;

 @Autowired
 private DataSource ds; //this bean is defined in parent context and I want it to be passed to child but not to be disposed when the child context closes.

 public void onSomeEventInParentContext(){
    new SpringApplicationBuilder(com.app.child.Child.class)
            .parent(parentAppCtx)  
            .run(args); //this context is autoclosed by spring cloud task 
 }
}

@EnableTask
public class Child{
}

谢谢

【问题讨论】:

  • 不要描述你的代码。发布它。
  • @JBNizet,我已经添加了代码示例

标签: spring spring-cloud-task


【解决方案1】:

在与spring-cloud-task 团队交谈后发现,父上下文在任务结束时隐式、强制和有意关闭。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多