从父应用程序运行和关闭子上下文

时间:2018-06-16 12:02:46

标签: spring spring-cloud-task

我使用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{
}

由于

1 个答案:

答案 0 :(得分:0)

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

相关问题