VIVEK PANDIAN S
VIVEK PANDIAN S

Reputation: 839

Play Framework + Spring - strange problem

I am using Play Framework along with Spring JbbcTemplates. I am using spring DriverManagerDataSource as the datasource for JdbcTemplate. Now, for some tables, I would like to use the Play Model bean. Is this possible? If so, how to make use of the same datasource (used by spring) to load the Play Model Beans. Please advise.

Upvotes: 0

Views: 815

Answers (1)

VIVEK PANDIAN S
VIVEK PANDIAN S

Reputation: 839

I found a hack for the above mentioned problem. Just created a plugin, and in the onApplicationStartUp(), assing the spring datasource to DB.datasource. Also, we need change the priority of the plugins so tat, spring plugin gets loaded before the JPA plugin.

Please find the code snippet,

play.plugins (place it under the "app" folder of your play project.

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

//LoadSpringDsPlugin.java public class LoadSpringDsPlugin extends PlayPlugin {

@Override
public void onApplicationStart() {
    Logger.debug("***********LoadSpringDsPlugin:onApplicationStart begins************");

    DataSource dataSource = Spring.getBeanOfType(DataSource.class);
    DB.datasource = dataSource;


    Logger.debug("***********LoadSpringDsPlugin:onApplicationStart ends************");
}

application-context.xml

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

Upvotes: 3

Related Questions