Announcement Announcement Module
Collapse
No announcement yet.
Injecting Hibernate Configuration into another bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment

    Working...
    X