Sep 19th, 2008, 01:50 PM
Creating arbitrary number of instances of the same class
We have a system using spring framework to create some beans. One bean is a handler that used to be a singleton. Now we need to change it to be arbitrary number of instances, each one takes a different constructor argument. This needs to be configurable because this system is used in different environments with different requirement.
I know I can write code to read the arguments from some placeholder and pass them one by one to create the beans, but is there any way to achieve this without writing code? The system will be maintained by some people without any knowledge of spring framework, so we can't ask them to change the application context xml file. Ideally, this should be configurable in a property file, say, "site=yahoo,google,msn", and the application context can create 3 instances of the handler with different arguments.
Sep 22nd, 2008, 12:55 AM
Spring doesn't offer the functionality you described. But how would you reference the created objects if it did? I don't see any problem with solving your issue via declaring three different instances of the target handler class with different constructor args.
Sep 22nd, 2008, 01:38 PM
I don't really need to refer to any individual handler explicitly. They can be added to a handler list to pass to the underlying network layer to hook up. Each handler simply listens to the update from the network and passes the messages to the central processing engine.
Because I don't know how many instances I need to create before hand, I can't declare them in the application context.
Sep 23rd, 2008, 12:53 AM
You can create a FactoryBean that takes target names as a property and produces a list of the corresponding handlers.