The active Playframework contributors consider this repository has reached End of Life and archived it.
This repository is not being used anymore and won't get any further updates.
Thank you to all contributors that worked on this repository!
This is an application loader for Play applications that runs with Spring as the DI. It binds and allows injecting all Play-provided components in addition to any components provided by third-party Play modules (defined as a play.api.inject.Module)
The current version targets Play 2.6.x and Spring 4.3.x. It may work but has not been tested on other versions.
The application loader was originally authored by Remi Thieblin based on the original proof of concept by James Roper. It is now being maintained by the Play team. There are currently no plans to add new features, but we're happy to accept contributions from the community. This project still needs tests and also a Java API for the SpringApplicationBuilder (though the Scala API can be used from Java).
To use in your Play SBT project, add the dependency to your build.sbt:
libraryDependencies += "com.lightbend.play" %% "play-spring-loader" % "0.0.2"Then configure the loader in your application.conf:
play.application.loader = "com.lightbend.play.spring.SpringApplicationLoader"
play.application.loader = "com.lightbend.play.spring.SpringApplicationLoader"
# This works assuming the class is a play.api.inject.Module
#play.modules.enabled += "com.demo.spring.MyModule"
play.spring.configs += "config.AppConfig"with the following configuration class:
package config
import org.springframework.context.annotation.{ComponentScan, Configuration}
@Configuration
@ComponentScan(Array("com.demo.spring", "controllers"))
class AppConfig {
}play.application.loader = "com.lightbend.play.spring.SpringApplicationLoader"
# This works assuming the class is a play.api.inject.Module
#play.modules.enabled += "com.demo.spring.MyModule"
play.spring.configs = ["com.example.PlaySpringDIConfiguration"]with the following configuration class:
package com.example;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class PlaySpringDIConfiguration {
}If you want to migrate your existing project from guice you should follow these steps
- Remove
guicefrom yourlibraryDependenciesinbuild.sbtfile - Make sure
PlaySpringDIConfigurationis placed in your root package or you have to name specific packages in@ComponentScanannotation - Annotate all of your controllers as
@Componentand services as@Service - Replace all
com.google.inject.Injectorwithorg.springframework.context.ApplicationContextandinjector.getInstance(MyClass.class)withcontext.getBean(MyClass.class)
Besides all of the above steps you must:
- Remove
javaJpa(jpa) from yourlibraryDependenciesinbuild.sbtfile - Add
"org.springframework" % "spring-orm" % "4.3.12.RELEASE","org.springframework" % "spring-aop" % "4.3.12.RELEASE"and"org.springframework" % "spring-expression" % "4.3.12.RELEASE"to yourlibraryDependenciesinbuild.sbtfile - Place this class next to the
PlaySpringDIConfiguration(or inside it):
@Configuration
@EnableTransactionManagement
// @EnableJpaRepositories //For SpringData
public class PersistenceContext {
@Bean
DataSource dataSource(play.db.DBApi dbapi) {
return dbapi.getDatabase("default").getDataSource();
}
@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, Config config) {
Config hibernateConfig = config.getConfig("db.default.hibernate");
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactoryBean.setPackagesToScan("com.example.domain.model");
Properties jpaProperties = new Properties();
hibernateConfig.entrySet().forEach(entry -> {
jpaProperties.put("hibernate."+entry.getKey(), entry.getValue().unwrapped());
});
entityManagerFactoryBean.setJpaProperties(jpaProperties);
entityManagerFactoryBean.setPersistenceUnitName(config.getString("jpa.default"));
return entityManagerFactoryBean;
}
@Bean
JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
}
- Add these properties from your
persisttence.xmlindb.defaultsection of yourapplication.conf:
db {
default {
[... your current db config ...]
hibernate.dialect="org.hibernate.dialect.X"
hibernate.hbm2ddl.auto="validate"
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.connection.autocommit=false
}
- Delete
persisttence.xmlfile - Annotate all of your repositories as
@Repository - Replace all
@Inject JPAApi jpaApiwith@PersistenceContext EntityManager entityManagerandjpaApi.em()withentityManager - Replace all
play.db.jpa.Transactionalwithjavax.transaction.Transactional
If you want to use SpringData, replace "org.springframework" % "spring-orm" % "4.3.12.RELEASE", "org.springframework" % "spring-aop" % "4.3.12.RELEASE" and "org.springframework" % "spring-expression" % "4.3.12.RELEASE" with "org.springframework.data" % "spring-data-jpa" % "1.11.8.RELEASE" and uncomment @EnableJpaRepositories over PersistenceContext class
The play-spring-loader library is Community Driven.