May 28th, 2012, 04:52 AM
Dynamic Bean Instances?
This seems like it should be a fairly common logical requirement, but I haven't found any examples. I'm not sure I'm even thinking about the problem in the right "Spring way" to search for the answer properly.
Is there a way to dynamically create n number of bean instances via configuration?
Here is an outline of my problem and what I want to accomplish:
- A web service proxy that makes external web service api requests and transforms and caches data to be returned to my client app according to my own service schema.
- I need to execute logic to reorder parsed/transformed data that my web service proxy has collected from other sources. For this task I have created a bean and have injected it into my controller with autowiring. I need to execute this same logic, but for n number of different data sources. I do not need to persist the data in the long term.
- I need to manage some state somewhere in my application. I can simply employ a collection of some sort, a standard Hash for example in my controller. I could then execute the bean logic and have it return some data that I store in my Hash. Alternatively, I could manage all the separate state data inside my singleton bean. But perhaps there is a better way in Spring?
It would be handy if I could store the state in individual bean instances and then have Spring manage multiple instances of my bean. I think there would need to be some kind autowiring configuration that allowed me to manage a collection of beans. I suppose this would probably need to provide read/write access with a key.
This can't be a one time bean factory, but would need to allow for new instances to be generated programmatically. I've come across mention of stateful. For example,
<bean id="foo" class="com.foo" singleton="false"> and
<bean id="foo" class="com.foo" scope="prototype">
but I'm not sure I'm onto the right path.
Hope that makes sense. Thanks in advance.
Last edited by giantsteps; May 28th, 2012 at 04:59 AM.
May 28th, 2012, 08:42 AM
I'm trying to follow your statement, but I'm missing a couple of details:
- do you have a single bean implementation with transforming logic for every possible datasource?
- have you already considered using spring integration?
May 29th, 2012, 05:30 AM
Hi Carlos -
Data transformation occurs in an earlier step where an interface allows for varied transform beans to be used. I simply mentioned it for overview purposes.
After transformation is complete I have another bean that orders a collection of objects based on a weighting algorithm. Each collection is cached in a cacheing layer. Because responses are cached, paged and available across all client sessions - I need to manage state somewhere.
Yesterday I opted to simply manage state within a single bean which meant refactoring it to manage multiple collections with a Hashtable. In the future I might do it differently. I will have a look at Spring integration. Thanks!