I don’t know if there’s a simple way to crate a Spring bean of type java.util.Properties . This is what I needed to do.
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" depends-on="hibernateProperties.load"> <property name="dataSource"><ref local="dataSource"/></property> <property name="configLocation"><value>classpath:net/lmb/resources/hibernate.cfg.xml</value></property> <property name="configurationClass"><value>org.hibernate.cfg.AnnotationConfiguration</value></property> <property name="hibernateProperties"><ref local="hibernateProperties"/></property> </bean> <bean id="hibernateProperties" class="java.util.Properties"/> <bean id="hibernateProperties.load" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject"><ref local="hibernateProperties"/></property> <property name="targetMethod"><value>putAll</value></property> <property name="arguments"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.cglib.use_reflection_optimizer">${hibernate.cglib.use_reflection_optimizer}</prop> <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop> <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop> <prop key="hibernate.connection.username">${hibernate.connection.username}</prop> <prop key="hibernate.connection.password">${hibernate.connection.password}</prop> </props> </property> </bean>
Basically you need to create an empty bean of type java.util.Properties, and after you add the properties using Spring <props> syntax calling the method putAll. One of the tricky parts is that you need to explicitly state the depends-on attribute in the LocalSessionFactoryBean so properties get loaded before the hibernate session is created.
Update:
Marc Logemann has posted a much more elegant solution using Spring’s PropertiesFactoryBean
<bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="properties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.query.substitutions">${hibernate.query.substitutions}</prop> <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> <prop key="hibernate.cglib.use_reflection_optimizer">${hibernate.cglib.use_refl}</prop> <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop> <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop> <prop key="hibernate.connection.username">${hibernate.connection.username}</prop> <prop key="hibernate.connection.password">${hibernate.connection.password}</prop> </props> </property> </bean>