views:

120

answers:

2

Instead of instantiating a PersistenceManagerFactory within my app like this:

Properties properties = new Properties();
properties.setProperty("javax.jdo.PersistenceManagerFactoryClass",
                "org.datanucleus.jdo.JDOPersistenceManagerFactory");
properties.setProperty("javax.jdo.option.ConnectionDriverName","com.mysql.jdbc.Driver");
properties.setProperty("javax.jdo.option.ConnectionURL","jdbc:mysql://localhost/myDB");
properties.setProperty("javax.jdo.option.ConnectionUserName","login");
properties.setProperty("javax.jdo.option.ConnectionPassword","password");
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(properties);

I want to configure it for dependency injection in Spring something like this:

<bean id="persistenceManagerFactory" class="org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean" lazy-init="true">
    <property name="persistenceManagerFactoryName" value="transactions-optional" />
</bean>

But I'm not sure how to pass the Properties in the application-context.xml (without using a jdoconfig.xml).

Is it possible in the application-context.xml to pass all of these Properties values for Autowiring?

+3  A: 

The simple way to do this is to use the <props> element to specify the Properties object and its entries. This is described in Section 3.4.2.4 of the Spring Reference Manual.

There are other alternatives for more complicated use-cases; e.g. there's a properties factory class that can assemble a Properties object from multiple sources.

Stephen C
+1  A: 

Either use an inline <props> block as Stephen C suggested, or use a properties file together with a PropertiesPlaceHolderConfigurer as suggested in the Spring Online Reference:

First register the PropertyPlaceholderConfigurer:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations"
        value="classpath:com/foo/jdbc.properties"/>
</bean>

Or use the new-school equivalent shortcut:

<context:property-placeholder
    location="classpath:com/foo/jdbc.properties"/>

This is sample content for the properties file:

jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root

And here's how you assign the properties:

<bean id="dataSource" destroy-method="close"
    class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="${jdbc.driverClassName}"/>
  <property name="url" value="${jdbc.url}"/>
  <property name="username" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>
</bean>

Examples are taken from the Spring Reference. The nice thing about this approach is that a) you can reuse the same properties file for different application contexts, b) for unit tests, you just put a different version of the properties on the classpath (in a maven scenario in src/test/resources instead of src/main/resources) and you don't have to change anything else.

You can also configure the LocalPersistenceManagerFactoryBean directly with a properties file (see PersistenceManagerFactory setup):

<beans>

  <bean id="myPmf" class="org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean">
    <property name="configLocation" value="classpath:kodo.properties"/>
  </bean>

</beans>
seanizer