Results 1 to 6 of 6

Thread: consolidating spring beans

  1. #1

    Default consolidating spring beans

    Hi,

    I would like to know how I can consolidate many of my beans that use the same property. For example, I have about 10 DAOs that are defined that use the same sessionFactory property, e.g.

    ###############
    <bean id="customerDAO" class="com.test.persistence.orm.hibernate.Hibernat eCustomerDAO">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean id="accountDAO" class="com.test.persistence.orm.hibernate.Hibernat eAccountDAO">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    ....
    ..
    ... // 8 more DAOs just like above
    ################

    I know consolidation is possible but have not seen an example on the Web on how this would be done.

    Also, can I consolidate transaction proxy beans that use the same transaction manager + transaction attributes on different service beans? For example, can I consolidate the below 2 beans into 1?:

    ################
    <bean id="customerServiceTX" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager" ref="hibernateTxManager"/>
    <property name="target" ref="customerService"/>
    <property name="transactionAttributes">
    <props>
    <prop key="*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <bean id="accountServiceTX" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
    <property name="transactionManager" ref="hibernateTxManager"/>
    <property name="target" ref="accountService"/>
    <property name="transactionAttributes">
    <props>
    <prop key="*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>
    ###############################


    I actually have 6 definitions of these beans in my app so consolidation would be nice.

    Thanks in advance!

    -los

  2. #2
    Join Date
    Oct 2004
    Location
    Fareham, England
    Posts
    313

    Default

    Hi los

    Using parent and child bean definitions is certainly a recommended and best practice, esp. in the case of TransactionProxyFactoryBean; to wit...

    Code:
    <bean id="abstractTxProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
         abstract="true">
       <property name="transactionManager" ref="hibernateTxManager"/>
       <property name="transactionAttributes">
          <value>
             *=PROPAGATION_REQUIRED
          </value>
       </property>
    </bean>
    
    <bean id="customerServiceTX" parent="abstractTxProxy">
       <property name="target" ref="customerService"/>
    </bean>
    
    <bean id="accountServiceTX" parent="abstractTxProxy">
       <property name="target" ref="accountService"/>
    </bean>
    See http://static.springframework.org/sp...an-definitions for details.

    Cheers
    Rick

  3. #3
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    You can use parent beans like this:

    Code:
    <bean id="abstractDAO" abstract="true">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
    <bean id="customerDAO" class="com.test.persistence.orm.hibernate.HibernateCustomerDAO" parent="abstractDAO">
    </bean>

    Regards,
    Andreas

  4. #4

    Default

    Thanks Rick and Andreas,

    For the TransactionProxyFactory example, I see where this would simplify a lot of text. However, for the DAO example, you still need to define a bean for each DAO (although it is a bit simpler than what I have). Is there a way to define a session factory and then list all the beans that would be involved in it, e.g. (the code below is pseudo...)

    ###########
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSes sionFactoryBean">
    <!-- list all of the DAOs that will participate -->
    <list>
    <ref bean="customerDAO"/>
    <ref bean="accountDAO"/>
    ....
    </list>
    </bean>
    ###########

    Thanks in advance!

    -los

  5. #5

    Default

    Hmm...

    Then again, I would still need to define DAO beans for the refs.... oh well!

    -los

  6. #6
    Join Date
    Oct 2004
    Location
    Fareham, England
    Posts
    313

    Default

    Hi los

    There is no way to do it the way that you suggest.

    I would recommend you to use the parent/child bean deifnition approach as suggested by Andreas.

    If you want to go there, you might want to consider using autowiring for your DAOs. If you are only ever going to have one SessionFactory, then turn autowiring on for your DAOs and the Spring container will do the donkey work of injecting the SessionFactory reference into your DAOs for you.

    http://static.springframework.org/sp...ctory-autowire

    Cheers
    Rick

Posting Permissions

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