Spring: creating a bean of type java.util.Properties

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>

13 thoughts on “Spring: creating a bean of type java.util.Properties

  1. 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 😉

  2. 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)

  3. 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

  4. 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 ?

  5. You can create a single bean from type java.util.Properties with the constructor. Example for presetting Quartz properties:

    <bean id="quartzProperties" class="java.util.Properties">
        <constructor-arg>
            <props>
                <prop key="org.quartz.jobStore.tablePrefix">QRTZ_</prop>
                <prop key="org.quartz.jobStore.selectWithLockSQL">
                    SELECT * FROM {0}LOCKS UPDLOCK WHERE LOCK_NAME = ?
                </prop>
            </props>
        </constructor-arg>
    </bean>
    

    bye*stephan

Leave a comment