Airframeã§Playãåãããã¨ãã
taroleoãããéçºãã¦ããAirframeãæè¿Twitterã®ã¿ã¤ã ã©ã¤ã³ã§è©±é¡ã«ãªã£ã¦ãããã¨ãå¤ãã®ã§ããããã触ã£ã¦ã¿ãããã¨æããã¨ããããPlay frameworkã®DIã³ã³ãããGuiceããAirframeã«ç½®ãæããã¨ããã®ããã£ã¦è¦ã¾ãããçµæã¯å¤±æã«çµãã£ããã§ãããGuiceã¨Airframeã®éããç¥ããã¨ã¯ã§ãã¾ããã
Playã§ã¯ããã©ã«ãã§ã¯Runtime DIã«ã¯Guiceãå©ç¨ããã¾ãããPlay2.6ã§ã¯DIã³ã³ããã®ããã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ãä½ãããã®ã§Guice以å¤ã®DIã³ã³ãããå©ç¨ã§ãã¾ããApplicationLoaderã¨ããã¯ã©ã¹ãPlayã®Applicationãªãã¸ã§ã¯ãããã¼ãããå½¹å²ãæ ã£ã¦ããã®ã§ããã好ããªDIã³ã³ããã§å®è£ ãã¾ãã
class AirframeApplicationLoader extends ApplicationLoader { override def load(context: ApplicationLoader.Context): Application = { import wvlet.airframe._ // ããã§Airframeã§Applicationãªãã¸ã§ã¯ããæ§ç¯ãã } }
å®éã«ã¯Playã¨ã®æ©æ¸¡ãçãªã³ã¼ãããããªãã«æ¸ãå¿ è¦ãããããããªã«ç°¡åã§ã¯ãªãã§ããä¾ãã°Playãå®ç¾©ãã¦ããInjectorã¤ã³ã¿ã¼ãã§ã¤ã¹ãå®è£ ããå¿ è¦ããã£ãããPlayã®ãã«ãã¤ã³ã¢ã¸ã¥ã¼ã«ãå©ç¨ãããDIã³ã³ããã§ä½¿ããããã«å¤æãããããªã³ã¼ããæ¸ãããã§ããå®ç¨çãªãã¨ãèããã¨Playãæä¾ãã¦ããGuiceApplicationBuilderã¿ãããªã³ã¼ããæ¸ãå¿ è¦ãããããªããªã骨ãæãã¾ãã
Airframeã®å ´åãå®éã«Playã®åã¢ã¸ã¥ã¼ã«ããã¼ããã段éã§ãã¤ã¾ã¥ãã¾ããPlayã®åã¢ã¸ã¥ã¼ã«ã¯ @Inject
ã¢ããã¼ã·ã§ã³ã§DIã³ã³ããã«å¯¾ãã¦ã¯ã©ã¹ã®çææ¹æ³ã®ãã³ããä¸ãã¦ãã¾ãããAirframe㯠@Inject
ã¢ããã¼ã·ã§ã³ã«ã¯å¯¾å¿ãã¦ããªãæ¹éã®ããã§ãããæ©è½ãã¾ãããèªåã§ã¯ã©ã¹ã®çææ¹æ³ãè¨è¿°ããå¿
è¦ãããã®ã§ãããPlayã®åã¢ã¸ã¥ã¼ã«ã®æ§ç¯ãæåã§ããã®ã¯ããªãé¢åãã¨ãããã¨ã§æ«æãã¾ããã
ãã®ä»Guiceã¨ã®éãã§éè¦ãããªã®ã¯sessionã¨ããæ¦å¿µã§ãããããAirframeã§ã¯sessionãæå¹ãªéã ãæ©è½ãå©ç¨ã§ããã¾ã@PostConstruct
, @PreDestroy
ã¢ããã¼ã·ã§ã³ãå©ç¨ããã¨sessionã®éå§æãçµäºæãããã¯ãã¦å¦çãæã¿ããã¾ããä¾ãã°ã¹ã¬ããã®éå§ãçµäºãªã©ãAirframeã«ä»»ããã¨ä¾¿å©ã§ãããGuiceã«ã¯ãã®æ©è½ã¯ãªãã®ã§ãPlayã§ã¯ @PostConstruct
ã @PreDestroy
ã®ä»£ããã«åã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã¨ApplicationLifecycleã¨ããã¯ã©ã¹ã§ã©ã¤ããµã¤ã¯ã«ããã¯ãå®è£
ããã¦ãã¾ãã
åãDIã³ã³ããã¨ãã種é¡ã®ã©ã¤ãã©ãªã§ãæ¯è¼ãã¦è¦ãã¨ãããªãã«å·®ç°ãããã¾ããããã«DIã³ã³ããã¯ã¢ããªã±ã¼ã·ã§ã³ã®åºç¤é¨åãæ ããããªãã®ãªã®ã§ãæ°è»½ã«ç½®ãæãããããããªãã®ã§ã¯ãªãã§ãããã§ãæ°ãåãããåãã£ã¬ã³ã¸ãã¦ã¿ããã¨æãã¾ãï¼å¤åãããªãï¼ã