DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Workaround for using the Spring DataSource in Play Model Beans

Workaround for using the Spring DataSource in Play Model Beans

Vivek Pandian Subburam user avatar by
Vivek Pandian Subburam
·
Jun. 29, 11 · Java Zone · Interview
Like (0)
Save
Tweet
8.94K Views

Join the DZone community and get the full member experience.

Join For Free

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

Spring Framework Datasource Workaround

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Five Tips to Fasten Your Skewed Joins in Apache Spark
  • Testing Your Infrastructure as Code Using Terratest
  • Python Class Attribute: Class Attribute vs. Instance Attribute
  • How to Translate Value to Executives Using an Outcome-Driven Mindset

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo