Workaround for using the Spring DataSource in Play Model Beans
Join the DZone community and get the full member experience.
Join For FreeRecently, 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
Opinions expressed by DZone contributors are their own.
Trending
-
File Upload Security and Malware Protection
-
Top 10 Pillars of Zero Trust Networks
-
Merge GraphQL Schemas Using Apollo Server and Koa
-
Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
Comments