Announcement Announcement Module
Collapse
No announcement yet.
Configuring multiple similar beans Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Configuring multiple similar beans

    I need to configure quite a lot of data access object (DAO) beans in my applicationContext.xml file:

    <bean id="addressDAO" class="mypackage.dao.AddressDAOImpl">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>
    <bean id="userDAO" class="mypackage.dao.UserDAOImpl">
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    </bean>

    etc.

    I was wondering whether there were any shortcuts for configuring multiple beans with the same properties? For example, it might be done like this:

    <property-applicator>
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
    <property name="otherProperty"><ref local="otherBean"/></property>
    <bean id="userDAO" class="mypackage.dao.UserDAOImpl"/>
    <bean id="otherDAO" class="mypackage.dao.OtherDAOImpl"/>
    <bean id="anotherDAO" class="mypackage.dao.AnotherDAOImpl"/>
    </property-applicator>

    It might come in handy if it doesn't already exist.

  • #2
    There is already such a feature.
    Creates a bean with attribute abstract="true". It is not an instanciable bean but it can be referenced by new beans to inherit its properties. You need not set a class attribute in it (but I don't know if you can add one). Then you write your real bean like <bean id="dao1" parent="myAbstractBean" class="...">.

    HTH

    Olivier

    Comment


    • #3
      So, I presume that I can just put the property tag in the parent bean declaration and it will be used by all the child beans? I knew I'd seen something like this - I think it was to do with transactions, but the same applies, of course.

      Comment


      • #4
        Originally posted by jonmor
        So, I presume that I can just put the property tag in the parent bean declaration and it will be used by all the child beans?
        exactly.
        About the class stuff, you may or may not precise it in the abstract/parent bean,a s you wish.

        Olivier

        Comment

        Working...
        X