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>
org.springframework.beans.factory.config.PropertiesFactoryBean
should do the trick. Never tried it with local defined properties but its documented that it should work as well, so
try this:
${hibernate.dialect}
${hibernate.query.substitutions}
${hibernate.show_sql}
${hibernate.cglib.use_reflection_optimizer}
${hibernate.cache.provider_class}
${hibernate.generate_statistics}
${hibernate.connection.username}
${hibernate.connection.password}
Gimme feedback if it worked 😉
JRoller is crazy, sometimes it dont submit my comments and when it does it, then it still doesnt like XML. Check my blog for the (hopfully right) answer.
http://www.logemann.org
Nice that it worked. Can you change my name in your blog entry to “Logemann” 😉 Thanks. (call me pedantic, but google will find this wrong name in a 100 years)
Thanks! that was exactly what I was looking for, and I had read the PropertiesFactoryBean API but for some reason I haven’t seen the “properties” property
Why create a separate, named bean to hold the properties? Why not use an anonymous inner bean definition like the one shown in the Spring documentation?
*.hbm.xml
org.hibernate.dialect.Oracle9Dialect
false
true
true
Some times you need to set them that way so you can set the bean in different files or use it in two places.
Is there a way to update values in the properties file through a spring bean and get to spring to re-load it so that all referring beans can make use of the new property value, eg change the password value for a user in the properties file based on input values and re-load the properties so that on the next authentication the new password is used ?
The only way I know is reloading the full application context, because the properties are loaded at that time.
http://osdir.com/ml/java.springframework.user/2004-08/msg00115.html
Thank u, very useful information, I choose the elegant solution and helped me a lot.
Mauricio
Quito, Ecuador
You can create a single bean from type java.util.Properties with the constructor. Example for presetting Quartz properties:
bye*stephan
Ups – i can’t see my code snippet for the bean definition. I put the example on our blog:
http://blog.systemfeld.de/2011/10/25/konfiguration-einer-properties-bean-mit-spring/
bye*stephan
I have fixed your comment code, but have into account that this post is from 2005!