Search code examples
springspring-bootjavabeansboilerplate

How to avoid boilerplate properties in XML Spring beans?


I have several Beans in my applicationContext.xml file that share similar properties (they implement the same interface).

I'd like to avoid boilerplate properties for each bean, and instead have one bean with these properties that can be referred to within each bean.

Currently what I have is this:

<bean id="Animal"
    class="com.test.Dog">
    <constructor-arg ref="Coat"/>
    <constructor-arg value="Bark" index="1"
          type="java.lang.String"/>
    <constructor-arg value="Spot" index="2"
          type="java.lang.String"/>
</bean>


<bean id="Dog"
    class="com.test.Dog">
    <constructor-arg ref="Coat"/>
    <constructor-arg value="Bark" index="1"
          type="java.lang.String"/>
    <constructor-arg value="Spot" index="2"
          type="java.lang.String"/>
</bean>

    <bean id="Chicken"
        class="com.test.Chicken">
        <constructor-arg ref="Coat"/>
        <constructor-arg value="Cluck" index="1"
              type="java.lang.String"/>
        <constructor-arg value="Janice" index="2"
              type="java.lang.String"/>
    </bean>

    <bean id="Rhino"
        class="com.test.Rhino">
        <constructor-arg ref="Coat"/>
        <constructor-arg value="huff" index="1"
              type="java.lang.String"/>
        <constructor-arg value="Tank" index="2"
              type="java.lang.String"/>

    </bean>

    <bean id="Coat"
        class="com.test.Coat"></bean>

Notice the repeating properties within each bean configuration.

What I want to do is something like this:

    <bean id="Animal"
        class="com.cat.test.Dog">
        <constructor-arg ref="properties"/>
    </bean>


    <bean id="Dog"
        class="com.cat.test.Dog">
        <constructor-arg ref="properties"/>
    </bean>

    <bean id="Chicken"
        class="com.cat.test.Chicken">
        <constructor-arg ref="properties"/>
    </bean>

    <bean id="Rhino"
        class="com.cat.test.Rhino">
        <constructor-arg ref="properties"/>
    </bean>

    <bean id="properties">
        <constructor-arg ref="Coat"/>
        <constructor-arg value="huff" index="1"
              type="java.lang.String"/>
        <constructor-arg value="Tank" index="2"
              type="java.lang.String"/>
    </bean> 

    <bean id="Coat"
        class="com.cat.test.Coat"></bean>

Is something like this possible?


Solution

  • You can use bean definition inheritance

    e.g

    <bean id="inheritedTestBean" abstract="true"
        class="org.springframework.beans.TestBean">
      <property name="name" value="parent"/>
      <property name="age" value="1"/>
    </bean>
    
    <bean id="inheritsWithDifferentClass"
          class="org.springframework.beans.DerivedTestBean"
          parent="inheritedTestBean" init-method="initialize">
    
      <property name="name" value="override"/>
      <!-- the age property value of 1 will be inherited from  parent -->
    
    </bean>