Results 1 to 4 of 4

Thread: Creating arbitrary number of instances of the same class

  1. #1
    Join Date
    Sep 2008
    Posts
    2

    Unhappy 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.

  2. #2
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    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.

  3. #3
    Join Date
    Sep 2008
    Posts
    2

    Default

    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.

  4. #4
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    You can create a FactoryBean that takes target names as a property and produces a list of the corresponding handlers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •