春季 - 在运行时寄存器作用域的 Bean
我正在开发一个基于Spring的应用程序,该应用程序注册了一个自定义范围“任务”。这个想法是,当一个新任务启动时,Spring应该提供任务范围的对象。
任务在运行时中实例化。它以对象的形式提供一些配置。我想将该对象注册到任务范围内的 but 中,以便该范围内的所有 Bean 都可以引用该特定任务的配置。Properties
ApplicationContext
下面是代码中的粗略想法:
public class MyTask extends SourceTask {
@Override
public void start(Map<String, String> props) {
context = ContextProvider.getApplicationContext();
// Initialize the scope
ConnectorTaskScope scope = context.getBean(ConnectorTaskScope.class);
scope.startNewTask();
// TODO register the props object in the context
// get an object which requires the properties and work with it
context.getBean(SomeScopedBean.class);
}
}
我不知道如何在适当作用域内的bean中注册bean。ApplicationContext
谢谢
更新:
这里有一些更多的代码来更好地解释这个问题。 应该使用它提供的bean提供的配置做一些事情,看起来像这样:SomeScopedBean
public class SomeScopedBean {
@Autowire
public SomeScopedBean (Properties configuration) {
// do some work with the configuration
}
}
应用程序的想法是,它应该具有多个使用不同配置运行的实例,并且每个任务都是其自己的范围。在每个任务的范围内,应该有 1 个使用任务的配置初始化的实例。MyTask
SomeScopedBean
public class MyApplication {
public static void main (String[] args) {
// ...
Properties config1 = loadConfiguration1();
Properties config2 = loadConfiguration2();
MyTask task1 = new MyTask();
MyTask task2 = new MyTask();
task1.start(config1);
task2.start(config2);
// ...
}
}