【发布时间】: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,我已经添加了代码示例