Results 1 to 2 of 2

Thread: Injecting Hibernate Configuration into another bean

  1. #1
    Join Date
    Mar 2006
    Posts
    1

    Post Injecting Hibernate Configuration into another bean

    In Spring 1.2.7, I want to inject the hibernate configuration (currently that was used to create a SessionFactory into another bean, and I'd like to do it all in the applicationContext.xml file.

    I see that the configuration is available on the LocalSessionFactoryBean via getConfiguration, so I figure there is some way to get to that object.

    I have the typical sessionFactory configuration:


    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSess ionFactoryBean">
    <property name="mappingResources">
    ...
    </property>

    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.query.substitutions">true=1 false=0</prop>
    <prop key="hibernate.show_sql">false</prop>
    ...
    </props>
    </property>
    </bean>


    Is there someway to access that hibernate configuration so I can inject it into another bean like the following?

    <bean id="exampleNeedsHibernateConfiguration"
    class="...">
    <property name="configuration"><ref bean="configuration"/></property>
    </bean>


    I was thinking maybe something trick with the MethodInvokingFactoryBean might do it.

    Any help is appreaciated.

    Thanks,
    Skip Walker

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

    Default

    Try this:

    Code:
    <bean id="hibernateConfig" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject"><ref bean="&amp;sessionFactory"/></property>
        <property name="targetMethod"><value>getConfiguration</value></property>
    </bean>
    This bean should then hold the configuration extracted from the LocalSessionFactoryBean named "sessionFactory". The ampersand prefix tells spring to refer to the factory bean itself, rather than its product (which is SessionFactory).

    Regards,
    Andreas

Posting Permissions

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