Vivek Pandian has posted 1 posts at DZone. View Full User Profile

Workaround for using the Spring DataSource in Play Model Beans

06.29.2011
| 6887 views |
  • submit to reddit

Recently, I came to know about the Play framework, and I wanted to integrate Play with Spring. There is a Spring module in the Play framework, through which I could access the Spring beans in Play. Everything was fine, until I encountered the following scenario: I wanted to use the datasource bean defined in Spring as the datasource for the Play model classes. Play, by default, uses the following properties defined in "application.conf" file for creating the datasource or connection objects.

#####application.conf file####
db.url=jdbc:oracle:thin:@host:port:sid
db.driver=oracle.jdbc.OracleDriver 
db.user=userName
db.pass=password


The Spring module does not provide a way to tell Play to use the datasource already defined in Spring.

Solution

After struggling a lot, I found a hack for this problem. I created a Play plugin, and in "onApplicationStart", I modify the datasource property of the DB object to point to the Spring DataSource bean. Also, this plugin must be loaded before the JPA Plugin, otherwise the JPA plugin will complain that db propertes (db.driver,db.user etc ) are not set properly. Also, we must make sure that the Spring plugin is loaded before our custom plugin, otherwise it could not find the Spring beans.

Code Snippet

Plugin
//LoadSpringDsPlugin.java

public class LoadSpringDsPlugin extends PlayPlugin {
   @Override
    public void onApplicationStart() {
    	DataSource dataSource = Spring.getBeanOfType(DataSource.class);
    	DB.datasource = dataSource;
    }
}
DataSource Bean Defintion in application-context.xml file
   

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

  
 	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<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>
 


Connection settings in jdbc.properties file
####jdbc.properties file####
jdbc.url=jdbc:oracle:thin:@host:port:sid
jdbc.driverClassName=oracle.jdbc.OracleDriver
jdbc.username=userName
jdbc.password=password

Plugin Configuration Create a file named "play.plugins" and place it under the "app" folder of your project.

//play.plugins file


388:play.modules.spring.SpringPlugin 399:com.ebay.pricing.catgyautomn.plugin.LoadSpringDsPlugin


Since the priority for JPA Plugin is 400, we assigned 399 for our custom Plugin. Also, spring plugin must be loaded before our custom plugin. so, we assigned priority 388 for spring plugin.

Note: Don't forget to comment the db.driver, db.url etc properties from applicaiton.conf, as it's no longer required.
#####application.conf file modified#### #####The db properties are not requried, since we are using the datasource bean from spring
# db.url=jdbc:oracle:thin:@host:port:sid
# db.driver=oracle.jdbc.OracleDriver
# db.user=userName
# db.pass=password

Published at DZone with permission of its author, Vivek Pandian Subburam.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)