Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - register scoped bean at runtime

Tags:

java

spring

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);
        // ...
    }
}
like image 745
artemb Avatar asked Apr 12 '17 09:04

artemb


People also ask

How do I register a bean in Spring?

Spring 5 comes with support for functional bean registration in the application context. Simply put, this can be done through overloaded versions of a new registerBean() method defined in the GenericApplicationContext class.

How can we make JVM scoped bean available for Spring container?

Single pattern in java mean you can create the only one instance of a that class in JVM. But In spring singleton bean scope means every container can create only single bean in the Spring IoC Container but a JVM can have multiple Spring IoC Container so JVM can multiple beans rather than bean singleton bean scope.


2 Answers

If I take your last comment:

What I want is to have 1 instance of SomeScopedBean within each scope (within each MyTask), but each configured with different configuration properties (which are provided by the deployment framework when it instantiates each Task,

And especially within each MyTask and if it is limited to MyTask.

You can:

  • define SomeScopedBean as a prototype bean
  • create a factory @Configuration which will instantiate SomeScopedBean with the provided properties configuration

First the configuration:

@Configuration
public class SomeScopedBeanFactoryConfiguration {

    @Bean
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    public SomeScopedBean create(Properties configuration) {
        return new SomeScopedBean(configuration);
    }

}

Then autowired the SomeScopedBeanFactoryConfiguration into MyTask and creates SomeScopedBean:

public class MyTask extends SourceTask {

    @Autowired
    private SomeScopedBeanFactoryConfiguration  someScopedBeanFactoryConfiguration;

    @Override
    public void start(Map<String, String> props) {
        SomeScopedBean scopedBean = someScopedBeanFactoryConfiguration.create(props);    
    }
}

Note: if SomeScopedBean must be injected in more than one bean with the task/thread scope, you could change its scope to your thread scope one eg.:

    @Bean
    @Scope("thread")
    public SomeScopedBean create(Properties configuration) {
        return new SomeScopedBean(configuration);
    }
like image 55
Nicolas Labrot Avatar answered Oct 13 '22 06:10

Nicolas Labrot


AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();

MyTask instance = new MyTask();
beanFactory.autowireBean(instance);
beanFactory.initializeBean(instance, MyTask.class.getCanonicalName());

//for singleton I used
((ConfigurableListableBeanFactory)beanFactory).registerSingleton(MyTask.class.getCanonicalName(), instance);

In your case I would register singleton of MyTask Proxy. The Proxy can keep all your scope dependent instances (e.g. in a Map or in ThreadLocal storage) and on call delegate logic to correct one from the Map.

UPDATE: Actually you autowire not MyTask bean but a Proxy. The proxy wraps all the MyTask methods. Proxy has the same interface as MyTask. Suppose you call ProxyMyTask.do() method. The proxy intercepts the call, get somehow scope e.g. current thread in case of Tread Scope example and get from the Map (or for Thread Scope from ThreadLocal storage) proper instance of MyTask. Finally calls the do(0 method of the found MyTask instance.

UPDATE 2: See the example http://tutorials.jenkov.com/java-reflection/dynamic-proxies.html You can easily wrap an interface. Your logic to determine scope and return proper instance should be in the method

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    return method.invoke(target, args);
}
like image 25
StanislavL Avatar answered Oct 13 '22 05:10

StanislavL