Announcement Announcement Module
No announcement yet.
Injecting FactoryBean properties Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Injecting FactoryBean properties

    Hello all,

    Consider this example:

    <bean id="bean1" class="bean1FactoryBean">
    <property name="prop1" value="value1"/>

    bean1FactoryBean is a bean which, obviously, implements FactoryBean.

    I have two questions. Well, an observation and a question:

    1) When Spring invokes the getObject() method it acquires a new bean. However, if this bean implements InitializingBean, Spring does _not_ call afterPropertiesSet(). I kind of consider this to be an omission, Spring could easily do this (and it would make sense).

    2) In the example, a property named "prop1" is set to "value1". These properties are invoked on the _factory bean_ rather than the _instance_ it creates. This raises some questions I think. Firstly, if you are creating a bean which has some complexity in it that you would like to implement in java, and a bunch of properties that are basically just bean references, you end up having to delegate all those bean references to the bean you are instantiating. How to fix this?

    Well. Spring already supports the "&" notation in the getBean method:

    FactoryBean fb = spring.getBean("&myFactoryBean");

    So why not support the same notation for properties?

    <bean id="bean1" class="bean1FactoryBean">
    <property name="&factoryProp1" value="value1"/>
    <property name="instanceProp1" value="value2"/>

    Ok, I guess this really amounts to two observations and no questions :-) Except to ask for some feedback and how others are handling this situation. Also I haven't checked in Spring 3.x if there are newer and more suitable approaches to handling the scenario above.

    Michael Berg

  • #2

    I guess I'm the only one having these problems :-)