Announcement Announcement Module
Collapse
No announcement yet.
Override a bean with a bean inheriting from the overriden bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Override a bean with a bean inheriting from the overriden bean

    Hello,

    We are developping an application which can be customized by overriding some beans.

    For instance, we have a bean "myBean" which has two properties A and B

    Code:
    <bean id="myBean">
    	<property name="A" value="Foo" />
    	<property name="B" value="Bar" />
    </bean>
    In the customized application, we would like to override this bean as below in order to modify its property B
    but we do not want to override its A property so we declare this overriding bean as inheriting from the overriden bean.

    Code:
    <bean id="myBean" parent="myBean">
    	<property name="B" value="777" />
    </bean>
    Unfortunately, it does not work. It seems that when this new bean is being created, the parent bean is already unavailable.

    We have tried to get around this issue by using bean aliases. It seems that is not possible.

    We have thought of two other solutions but we don't know how to implement them :
    - Add a mechanism to automatically change the name of the overriden bean and the one referred in the parent attribute of the overriding bean.
    - Make a copy of the parent bean with different name so that the overriding bean can inherit from this copy

    Is there anyone who knows how to achieve those solutions or has an other solution for this problem?

    Many thanks for your help

  • #2
    Well as you noticed you cannot do this, BeanDefinitions are stored by name and one overrides the other, they aren't merged together. This also means that you cannot have a bean with itself as a parent (because that is basically the result of the replacement).

    You could create your own BeanDefinitionReader or BeanFactory implementation but that would also mean creating your own ApplicationContexts etc. Easiest way would be to define a parent which contains the settings and when overriding also use this as a parent that way overriding the properties work, drawback is that everyone needs to know that they have to extend a parent bean.

    Comment


    • #3
      Thanks Marten for this clever solution

      So if understand well, we would have in our application:
      Code:
      <bean id="myBeanParent">
      	<property name="A" value="Foo" />
      	<property name="B" value="Bar" />
      </bean>
      <bean id="myBean" parent="myBeanParent" />
      And in the customized application, we would have:
      Code:
      <bean id="myBean" parent="myBeanParent">
      	<property name="B" value="777" />
      </bean>

      Comment


      • #4
        That should indeed work. It will inherit the properties from the parent and override it with the properties as set on the bean itself (this is also explained in more detail in the reference guide).

        Comment

        Working...
        X