Now that we have defined a plugin, let's see how the Play Framework identifies and enables it for the application. ApplicationProvider
for the production and development mode (static and reloadable applications, respectively) both rely on DefaultApplication
, which is defined as follows:
class DefaultApplication( override val path: File, override val classloader: ClassLoader, override val sources: Option[SourceMapper], override val mode: Mode.Mode) extends Application with WithDefaultConfiguration with WithDefaultGlobal with WithDefaultPlugins
The trait WithDefaultPlugins
line is responsible for binding the plugins to application's life cycle. It is defined as follows:
trait WithDefaultPlugins {
self: Application =>
private[api] def pluginClasses: Seq[String] = {
import scala.collection.JavaConverters._
val PluginDeclaration = """([0-9_]+):(.*)""".r
val pluginFiles = self.classloader.getResources("play.plugins").asScala.toList ++ self.classloader.getResources("conf/play.plugins").asScala.toList
pluginFiles.distinct.map { plugins =>
PlayIO.readUrlAsString(plugins).split("
").map(_.replaceAll("#.*$", "").trim).filterNot(_.isEmpty).map {
case PluginDeclaration(priority, className) => (priority.toInt, className)
}
}.flatten.sortBy(_._1).map(_._2)
}
...
}
So, we should declare our plugin class in a file with the play.plugins
name. All the plugin declarations obtained from one or more play.plugins
files are combined and sorted. Each declared plugin has a priority assigned to it, which is used for sorting. Once sorted, the plugins are loaded in order prior to the application's startup.
The priorities should be set based on the dependencies of a plugin. The suggested priorities are as follows:
100
: This priority is set when a plugin has no dependencies, such as the messages plugin (used for i18n
)200
: This priority is set for the plugins that create and manage the DB connection pools300-500
: This priority is set for the plugins that depend on a database, such as JPA, Ebean, and evolutionsThe default play.plugins
file just has a basic plugin declaration:
1:play.core.system.MigrationHelper 100:play.api.i18n.DefaultMessagesPlugin 1000:play.api.libs.concurrent.AkkaPlugin 10000:play.api.GlobalPlugin
A few more plugin declarations from the Play modules are as follows:
200:play.api.db.BoneCPPlugin 500:play.api.db.evolutions.EvolutionsPlugin 600:play.api.cache.EhCachePlugin 700:play.api.libs.ws.ning.NingWSPlugin
3.147.74.211