I am working on a Spring-based application which registers a custom scope "task". The idea is that when a new Task is started, Spring should supply task-scoped objects.
The task is instantiated in the runtime. It is supplied with some configuration in the form of a Properties object. I want to register that object with the ApplicationContext but within the task scope so that all beans within that scope can reference the configuration of that particular task.
Here is the rough idea in code:
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);
}
}
I can't figure out how can I register a bean in the ApplicationContext that is scoped appropriately.
Thank you
Update:
Here is some more code to explain the question a bit better. SomeScopedBean should be doing something with the configuration it has bean provided with and looks something like this:
public class SomeScopedBean {
@Autowire
public SomeScopedBean (Properties configuration) {
// do some work with the configuration
}
}
The idea of the application is that it should have multiple instances of MyTask running with different configuration and each task is its own scope. Within the scope of each task, there should be 1 instance of SomeScopedBean initialized with the task's configuration.
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);
// ...
}
}