Results 1 to 4 of 4

Thread: Override a bean with a bean inheriting from the overriden bean

  1. #1
    Join Date
    Mar 2012
    Posts
    2

    Question 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Mar 2012
    Posts
    2

    Talking

    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>

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Tags for this Thread

Posting Permissions

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