Play 2.4 ã® Module ã®ä½ãæ¹ã¨ Plugin ããã®ç§»è¡ã«ã¤ãã¦
Play 2.4 ã§ã¯ä»ã¾ã§ã® Plugin ã®ä»çµã¿ã deprecated ã¨ãªãã æ°ãã« Module ã¨ããä»çµã¿ãå°å ¥ããã¦ãã¾ãã
Module ã¯ããã¾ãæ°ãã«å°å ¥ããã Runtime Dependency Injection ã®ä¸ã«ä¹ã£ãã£ã¦ãã¾ãã Play ã§ã¯ Guice ãããã©ã«ãã® DI å®è£ ã¨ãã¦å©ç¨ãã¾ããã Guice 以å¤ã®å®è£ ãå©ç¨ããã¦ã¼ã¶ã¼ãããã ããã¨ãããã¨ã§ã ã©ã¤ãã©ãªã¨ãã¦æä¾ããã¢ã¸ã¥ã¼ã«ã«ã¤ãã¦ã¯ããã ã Guice ã§ã¯ãªããPlay ç¬èªã® Module ã¨ããä»çµã¿ã«æ²¿ã£ãå½¢ã§å®è£ ãã¾ãã
ã¢ããªã±ã¼ã·ã§ã³ã®èµ·åæã¨çµäºæã«ã¡ãã»ã¼ã¸ã表示ãã Play Module ãå®è£ ãã¦ã¿ã¾ãããã
ã¾ãã¯å¦çã®æ¬ä½ã§ãã
package example import play.api._ import javax.inject._ import play.api.inject._ import scala.concurrent._ import scala.concurrent.ExecutionContext.Implicits.global @Singleton class Hello @Inject() (lifecycle: ApplicationLifecycle) { lifecycle.addStopHook(() => Future.successful { println("Goodbye") }) println("Hello") }
ä»ã¾ã§ã® Plugin ã¤ã³ã¿ãã§ã¼ã¹ã®ã¡ã½ãããå®è£ ããå½¢ã§å®è£ ãã¦ããã Play ã¢ããªã±ã¼ã·ã§ã³ã®èµ·åæã®å¦çã¯ãã·ã³ã°ã«ãã³ã¯ã©ã¹ã®åæåå¦çã®ä¸ã§è¡ãã¾ãã çµäºæã®å¦ç㯠injection ããã lifecycle ãªãã¸ã§ã¯ãã® addStopHook ã¡ã½ãããå©ç¨ãã¦ç»é²ãã¾ãã
ããã Play Module ã¨ãã¦èªã¿è¾¼ãã«ã¯
play.api.inject.Module
ãç¶æ¿ãã¦ãbinding ã¡ã½ãããå®è£
ãã¾ãã
package example import play.api._ import javax.inject._ import play.api.inject._ import scala.concurrent._ import scala.concurrent.ExecutionContext.Implicits.global class HelloModule extends Module { def bindings(environment: Environment, configuration: Configuration) = { Seq( bind[Hello].toSelf.eagerly ) } }
bind ã®ä»æ¹ã DSL ã§è¨è¿°ãã¾ãã
Play ã¢ããªã±ã¼ã·ã§ã³èµ·åæã®å¦çãè¡ãããã¯ãèµ·åæã« bind ããå¿
è¦ãããã®ã§ã
.eagerly
ã使ç¨ãã¾ãã
ä½ã£ã Module ãå©ç¨ããã¨ã㯠application.conf ã§ãã® Module ãæå¹ã«ãã¾ãã
play.modules.enabled += "example.HelloModule"
ã·ã³ã°ã«ãã³ãªãã¸ã§ã¯ããªã®ã§ toSelf
ã§èªåèªèº«ã« bind ãã¦ãã¾ãã¾ãããã
ããä¸è¬çã«ã¯ã¤ã³ã¿ãã§ã¼ã¹ãä½ã£ã¦ããã« bind ãã¾ãã
package example import play.api._ import javax.inject._ import play.api.inject._ import scala.concurrent._ import scala.concurrent.ExecutionContext.Implicits.global class HelloModule extends Module { def bindings(environment: Environment, configuration: Configuration) = { Seq( bind[HelloInterface].to[Hello].eagerly ) } } @Singleton class Hello @Inject() (lifecycle: ApplicationLifecycle) extends HelloInterface { lifecycle.addStopHook(() => Future.successful { println("Goodbye") }) println("Hello") } trait HelloInterface
ããã«ãã¤ã³ã¹ã¿ã³ã¹ã®çæãè¤éãªå ´å㯠Provider ãå©ç¨ãããã¨ãããããã§ãã
package example import play.api._ import javax.inject._ import play.api.inject._ import scala.concurrent._ import scala.concurrent.ExecutionContext.Implicits.global class HelloModule extends Module { def bindings(environment: Environment, configuration: Configuration) = { Seq( bind[HelloInterface].toProvider[HelloProvider].eagerly ) } } @Singleton class HelloProvider @Inject() (lifecycle: ApplicationLifecycle) extends Provider[HelloInterface] { lazy val get = new Hello(lifecycle) } class Hello (lifecycle: ApplicationLifecycle) extends HelloInterface { lifecycle.addStopHook(() => Future.successful { println("Goodbye") }) println("Hello") } trait HelloInterface
Java ã£ã½ããå¢ããã®ã§ãç¹ã« Java ã® DI ã«ãªãã¿ã®ãªã LL ããã®ã¦ã¼ã¶ã¼ã¯æ¸æãããããã¾ããã Runtime Dependency Injection ã®å°å ¥çµç·¯ããã¹ãã¸ã®å½±é¿ãªã©ã¯ playframework-dev ã®ã¡ã¼ãªã³ã°ãªã¹ããèªãã¨é¢ç½ããã¨æãã¾ãã