Results 1 to 4 of 4

Thread: Configuring multiple similar beans

  1. #1
    Join Date
    Aug 2004
    Posts
    123

    Default 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. #2
    Join Date
    Aug 2004
    Location
    Toulouse, France
    Posts
    148

    Default

    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

  3. #3
    Join Date
    Aug 2004
    Posts
    123

    Default

    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.

  4. #4
    Join Date
    Aug 2004
    Location
    Toulouse, France
    Posts
    148

    Default

    Quote 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

Similar Threads

  1. Replies: 4
    Last Post: Jul 22nd, 2008, 05:20 PM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM
  4. Beans from multiple projects
    By sbhatti in forum Data
    Replies: 1
    Last Post: Mar 31st, 2005, 02:50 PM
  5. Replies: 2
    Last Post: Jan 14th, 2005, 11:04 AM

Posting Permissions

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