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

  • 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
    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

    Comment


    • #3
      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

      Comment


      • #4
        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

        Comment


        • #5
          Hmm...

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

          -los

          Comment


          • #6
            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

            Comment

            Working...
            X