Announcement Announcement Module
No announcement yet.
IOC and parameterized factory Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • IOC and parameterized factory


    Just a question on how to use IOC with a parameterized factory

    Let's say you want to have a parameterized factory that takes runtime parameter(s) obtained from a user request (for example) and these parameters identify to the factory the kind of object it should create. With IOC should the consumer of the object know how to combine the runtime parameters to form an id of an object that it needs. That doesen't seem clean or intuitively the right solution. How does Spring facilitate using runtime values to construct objects as might have been done in a parameterized factory without using Spring.


  • #2
    Out of the box Spring doesn't support a parameterized factory as a standard concept. But people have talked about solutions to problems of this kind. Check out this thread

    Could you describe in more detail your particular problem? Do you want to specify all of the various classes as prototype beans in the application context? Do you want to add properties after the object is created by Spring, but before its initalized with afterPropertiesSet or the init-method?


    • #3

      The thread you gave as a reference was pretty useful. I am not looking at a specific problem. Just looking at Spring from a framework perspective to see how it would meet scenarios where runtime parameters might be needed -- for example:
      to either populate properties with runtime info
      or for a factory to return different objects (different implementations of an interface) based on runtime info.

      I see how your solution with ThreadLocal could be a workaround.

      However, I think a getBean(String name, Object []args) would be the best solution. Also, it seems to me that a FactoryBean should be able to take runtime parameters (e.g., Object[]) in it's getObject to be able to return different implementations of an interface based on some runtime information if needed.