DIã³ã³ããã使ãæã«ã©ã®ã¤ã³ã¸ã§ã¯ã·ã§ã³ã使ããã£ã¦è©±ã§ãã ãã¶ã誰ããã©ããã§åããããªãã¨ãæ¸ãã¦ããã ããããã©ãæ°ã«ããæ¸ããã ãä»ã®èª°ããæ¸ãã¦ããããªãã¦ã®ãæ¸ããªãçç±ã«ãã¦ãã¨ä½ãæ¸ããªããªããã
- ã³ã³ãã
- DIã³ã³ããã®ãã¨ã
- ã³ã³ãã管ç
- ã¤ã³ã¹ã¿ã³ã¹ã®ã©ã¤ããµã¤ã¯ã«ãã³ã³ããã管çãã¦ãããã¨ãéã«è¨ãã°ã使ãå´ã§ new ããªãã£ã¦ãã¨ã
- ã¤ã³ã¸ã§ã¯ã·ã§ã³
- Dependency Injectionã®ãã¨ã
Short Answer
ã³ã³ã¹ãã©ã¯ã¿ã¤ã³ã¸ã§ã¯ã·ã§ã³ã使ãã¾ãããã使ãåããªãã¦ããã§ãã
3種é¡ã®ã¤ã³ã¸ã§ã¯ã·ã§ã³
ã¤ã³ã¸ã§ã¯ã·ã§ã³ã«ã¯3種é¡ããã¾ãããä»ãã£ã¦ãç¥ããªãã
- ãã£ã¼ã«ãã¤ã³ã¸ã§ã¯ã·ã§ã³
- ã»ãã¿ã¼ã¤ã³ã¸ã§ã¯ã·ã§ã³
- ã³ã³ã¹ãã©ã¯ã¿ã¤ã³ã¸ã§ã¯ã·ã§ã³
ãã£ã¼ã«ãã¤ã³ã¸ã§ã¯ã·ã§ã³
ä¸çªããè¦ãããªã
class Hoge { @Inject Piyo piyo; }
ãã®ç¶æ
ã§Hoge
ãã³ã³ããã«ã¤ã³ã¹ã¿ã³ã¹çæãã¦ãããã¨ãpiyo
ãã£ã¼ã«ãã«ã³ã³ãã管çããã¦ããPiyo
ã¤ã³ã¹ã¿ã³ã¹ãã¤ã³ã¸ã§ã¯ã·ã§ã³ãããã
è¨è¿°éãå°ãªãã¦æ¸ãã®ã¯å¬ããã§ããã
ãã®ã¤ã³ã¹ã¿ã³ã¹ãã³ã³ããå¤ã§çæãã¦ä½¿ããã¨ããã¨ãpiyo
ãã£ã¼ã«ãã«ä½ãããã®æ¹æ³ã§ã¤ã³ã¹ã¿ã³ã¹ãè¨å®ãã¦ãããªãããããªãã
ä½ãããã®æ¹æ³ã£ã¦ã®ã¯åä¸å
«ä¹ãªãã¬ã¯ã·ã§ã³ã§ããããã£ã¼ã«ããpublic
ã«ããæãããããã©ããã¾ãããªãããªã
ã³ã³ããã§ã©ã¤ããµã¤ã¯ã«ã管çããããªãã¸ã§ã¯ãã¯ããããã¯ãã³ã¼ãã§ã¯ã³ã³ããå¤ã§ä½¿ãã¾ããï¼ãã£ã¡ããããªãï¼ãããã¹ãã³ã¼ãã§ã¯ãããã¾ãã
åé¡ç¹ã¯ãï¼ãªãã¬ã¯ã·ã§ã³ã使ããªãå ´åï¼ãã¹ãã§ã³ã³ããã使ããªãã¨ãããªããªãã¾ãã ã³ã³ããã使ãã¨éããªãã¾ãã ã³ã³ããã®éãã¯ãã£ãã·ã¥ãã³ã³ããã®é«éåãªã©ã§å¯¾å¿ã§ããªãã¯ãªãã§ãã ã¨ã¯ããããã¹ãã®éããã³ã³ããã®èµ·åæéã«æ¯é ããã¦ããéçºç¾å ´ã«ããã¦ãé©åãªå¯¾å¿ãããããã¨ã¯ç¨ã§ãã 対å¿ããã«æ¾ç½®ããã¨ãåç´ã«æéããããã®ã§ãã¹ãã®å®è¡æ©ä¼ãæ¸ãã¾ããã¨ã¦ãæ®å¿µã§ããã
ããä¸ã¤åé¡ããã£ã¦ããã®ãªãã¸ã§ã¯ããã³ã³ããã«ä¾åãããã¨ã§ãã ãã£ããDIã³ã³ããã使ããã¨ã§ä¾åé¢ä¿ãé転ãããã®ã«ãDIã³ã³ããã使ããªãã¨é©åã«ã¤ã³ã¹ã¿ã³ã¹çæã§ããªãç©ä½ã«ãªã£ã¦ãã¾ãã ãªãã¨ããããæ¬æ«è»¢åæãããã¾ãã å人çã«ã¯ãã¡ãã®çç±ã§ä½¿ããããªãã
ã»ãã¿ã¼ã¤ã³ã¸ã§ã¯ã·ã§ã³
é»ææã«ããè¦ããã©ãæè¿ã¯ãã¾ãè¦ãªããããªã
class Hoge { Piyo piyo; @Inject void setPiyo(Piyo piyo) { this.piyo = piyo; } }
ãã®ç¶æ
ã§Hoge
ãã³ã³ããã«ã¤ã³ã¹ã¿ã³ã¹çæãã¦ãããã¨ãsetPiyo(Piyo)
ã¡ã½ãããã³ã³ãã管çããã¦ããPiyo
ã¤ã³ã¹ã¿ã³ã¹ãå¼æ°ã¨ãã¦å¼ã³åºãããã
åè¿°ã®ãªãã¬ã¯ã·ã§ã³äºã
ããã¯æåãããããããã§ããã
ã³ã³ããå¤ã§ä½¿ãæããèªåã§ã¤ã³ã¹ã¿ã³ã¹çæãã¦ããã»ãã¿ã¼ãå¼ã³åºãã°ããã ãã§ãã
åé¡ç¹ã¯ãã»ãã¿ã¼ãªãã¨ã ã£ã¦è¨ãã¨è¶£å³ãå ¥ã£ã¦ãããã«æããããããã§ãããã»ãã¿ã¼ãæã¤ã£ã¦ãã¨ã¯ãç¶æ é·ç§»ãããã£ã¦ãã¨ãªãã§ãã ç¶æ é·ç§»ããããªãã¸ã§ã¯ãã¯ä¸å®å®ã§ããçµã¿åãããã¨ãã£ã¨ä¸å®å®ã«ãªãã¾ãã çå¿æ鬼ã«ãªããªãã使ããããªãã§ãã
ãªãã ããã ã§ãã³ã³ãã管çããã¤ã³ã¹ã¿ã³ã¹ã£ã¦ã³ã³ããå ã§ã¯ã·ã³ã°ã«ãã³ãªãã¨ãå¤ãã§ãã ã¹ã³ã¼ããçç¥ããå ´åã®ããã©ã«ããã·ã³ã°ã«ãã³ãªãã¨ãããããããã¨æãã¾ãã ããã¯ããã§ã©ããªã£ã¦æãã®ã§ãããã¾ãç¾ç¶ã¯ããã§ãã ãããªã¤ã³ã¹ã¿ã³ã¹ã®ç¶æ ãä¸å®å®ã¨ãè¨ãããã¨ãç§ã¯è½ã¡çãã¦éçºã§ããªãã
ç§ã®å¿ççãªå®å ¨ã®ããã«ã»ãã¿ã¼ã¤ã³ã¸ã§ã¯ã·ã§ã³ã¯ä½¿ããªãã§ã»ããã§ãã ãã£ã¼ã«ãã¤ã³ã¸ã§ã¯ã·ã§ã³ã¯ããªãã¬ã¯ã·ã§ã³ã使ãæç¹ã§å±éºãªã®ãããã£ã¦ãã£ã¦ããã¨èªåã«è¨ãèããããã¨ãã§ããã®ã§ããã®ç¹ã¯è¨±å®¹ã§ãã¾ãã
ã³ã³ã¹ãã©ã¯ã¿ã¤ã³ã¸ã§ã¯ã·ã§ã³
ãªãããã¾ãè¦ãªãã
class Hoge { final Piyo piyo; @Inject public Hoge(Piyo piyo) { this.piyo = piyo; } }
ãã®ç¶æ
ã§Hoge
ãã³ã³ããã«ã¤ã³ã¹ã¿ã³ã¹çæãã¦ãããã¨ãã³ã³ãã管çããã¦ããPiyo
ã¤ã³ã¹ã¿ã³ã¹ãã³ã³ã¹ãã©ã¯ã¿ã®å¼æ°ã¨ãã¦å¼ã³åºãããã
ã¤ã³ã¸ã§ã¯ã·ã§ã³ã¿ã¤ãã³ã°ãããããããã®ãããã§ããã
ãã£ã¼ã«ãã¤ã³ã¸ã§ã¯ã·ã§ã³ã¨æ¯è¼ãã¦ãã¤ã³ã¸ã§ã¯ã·ã§ã³ããæ°ãå¢ããã¨ç©ã¿éãªã£ã¦ãããåé·ã«ãªããã¨è¨ãã®ãèãã¾ãã ã§ããããã ãå¤ãã®ãã®ã«ä¾åãã¦ããç¶æ ãããããããããã¨æè¨ã§ãã¾ãã
ã¤ã³ã¹ã¿ã³ã¹çææç¹ã§ç¶æ
ãå®å®ããã®ãå®ã«è¯ãã
ãã£ã¼ã«ãã«final
ã¤ãããã®ãå¬ããã§ãã
ãã£ã¼ã«ãããã¼ã«ã«å¤æ°ãããã©ã«ãfinal
ã«ãªããªãããªãã¨å¸¸ã
æããã©ãããã¯å¥ã®è©±ã
ï¼è±ç·ï¼
ãã£ã¼ã«ãã¤ã³ã¸ã§ã¯ã·ã§ã³ã¯ããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ãªã©ã§ã¤ã³ã¹ã¿ã³ã¹ãçæãã¦ããããã£ã¼ã«ãã«ãªãã¬ã¯ã·ã§ã³ã§è¨å®ãã¾ããã»ãã¿ã¼ã¤ã³ã¸ã§ã¯ã·ã§ã³ãåãã§ãã
ãã®ãããã¤ã³ã¹ã¿ã³ã¹çæç´å¾ã«å¤ãå
¥ã£ã¦ãªãã£ãããã¾ãã
ãã®ééãã¤ãã¦NullPointerException
ãèµ·ãã£ãããããã¨ãç¡ãã¨ã¯è¨ãã¾ããã
å®éèµ·ãããã¨ã¯ãã¾ããªãã®ã§ãããçããããªãæ°æã¡ãããã£ã¦ãã ããã
ï¼ï¼è±ç·ï¼
ã¾ããã¤ã³ã¹ã¿ã³ã¹çæã§å¿ è¦ãªãã®ãã³ã³ã¹ãã©ã¯ã¿ã§æ示ããã¦ãã¾ãã ãã¹ããªã©ã®ã³ã³ããã®å¤ã§ã¤ã³ã¹ã¿ã³ã¹çæããéãç¹ã«å¶ç´ã¯ããã¾ããã
ã¨è¨ããã¨ã§ãåé¡ç¹ã¯ç¹ã«æãããã¾ããã
ãªããSpringã§ãã³ã³ã¹ãã©ã¯ã¿ã¤ã³ã¸ã§ã¯ã·ã§ã³ã¯@Autowired
ã¨ãæ¸ããªãã¦ãã¤ã³ã¸ã§ã¯ã·ã§ã³ãã¦ãããããã«ãªã£ã¦ãããã¾ãã
ãã®è¾ºãã³ã³ã¹ãã©ã¯ã¿ã¤ã³ã¸ã§ã¯ã·ã§ã³ã®è¿½ã風ã
ãªãè¤æ°ã®ã¤ã³ã¸ã§ã¯ã·ã§ã³æ¹æ³ãããã®ã
ã³ã³ãããä½ãå´ã®é½åããªã¼ã¨ãç¥ãããã©ã 使ãå´ã®äºæ ã§ãã®3種é¡ã®ã¤ã³ã¸ã§ã¯ã·ã§ã³ã使ãåãããã¨ã¯ä»ã¾ã§ãªãã¦ãå¤åä»å¾ããªãã
ã¤ã³ã¹ã¿ã³ã¹ãçæããããã«ã¯ã³ã³ã¹ãã©ã¯ã¿ãå¼ã¶å¿
è¦ãããã¾ãã
ãã³ã³ãã管çããã«ã¯ããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ãå¿
è¦ããªãã¦ã®ãè¦èããããã¨ã¯ããã§ãããã
ããã¯ã¤ã³ã¹ã¿ã³ã¹çææã«Class#newInstance()
ã使ã£ã¦ããã…ãªãã¦çç±ã§ã¯ãªãã¨æãã¾ãããã¨ãããåã«ã¤ã³ã¹ã¿ã³ã¹çæããã ããªãããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ã楽ã§ãã
ã³ã³ããã«éãããã¤ã³ã¹ã¿ã³ã¹ãçæããã©ã¤ãã©ãªãããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ãè¦æ±ããã®ã¯çãã話ã§ã¯ããã¾ããã
ããè¨ãã°Class#newInstance()
ãJDK9ã§Deprecatedã«ãªãã¾ãããé¢ä¿ãªãã§ããã©ã
話ãæ»ãã¦ãã³ã³ãããä½ãå´ã®é½åã§ã¯ãã£ã¼ã«ãã¤ã³ã¸ã§ã¯ã·ã§ã³ã楽ã§ãã ã³ã³ãã管ç対象ã®ã¤ã³ã¹ã¿ã³ã¹ãä¸éãçæãã¦ããå¿ è¦ãªã¤ã³ã¹ã¿ã³ã¹ãã¤ã³ã¸ã§ã¯ã·ã§ã³ãã¦ããã°ããã ãã ããã§ãã
ã³ã³ã¹ãã©ã¯ã¿ã¤ã³ã¸ã§ã¯ã·ã§ã³ã§ã¯ãã³ã³ã¹ãã©ã¯ã¿ãè¦æ±ããã¤ã³ã¹ã¿ã³ã¹ãå ã«çæããå¿ è¦ãããã¾ãã ã¤ã³ã¸ã§ã¯ã·ã§ã³å¯¾è±¡ã®ã¤ã³ã¹ã¿ã³ã¹ããªããã°ã³ã³ã¹ãã©ã¯ã¿ãå¼ã³åºããªãã§ãããã ãããªã£ã¦ããã¨ãã¤ã³ã¹ã¿ã³ã¹ã®çæé ãèæ ®ããªãã¨ãããªããªãããã§ãã³ã³ãããå¤æ®µæ§æã«ãªã£ã¦ããããå®ç¾©ã®ä¸æ¸ãããã£ããããã¨ã¨ã¦ãé¢åã§ãã ä¾åé¢ä¿ã®å¾ªç°ãªã©ãåé¡ã«ãªã£ã¦ãã¾ãã
ã»ãã¿ã¼ã¤ã³ã¸ã§ã¯ã·ã§ã³ã§ãçæé ã¯åé¡ã«ãªã£ã¦ãã¾ãã ãªããªãã¤ã³ã¸ã§ã¯ã·ã§ã³ã§å¼ã³åºãã®ã¯ã¡ã½ãããªã®ã§ãã¡ã½ããã§å¼æ°ã®ãªãã¸ã§ã¯ãã®ã¡ã½ãããå¼ã³åºãããããããã¾ããã ãã»ãã¿ã¼ã§ã¯ã¡ã½ããå¼ã³åºããããªãã§ãã ãããã¨ãè¨ãããã«ããããªãã®ã§ãã¤ã³ã¸ã§ã¯ã·ã§ã³ãããªãã¸ã§ã¯ãã¯å®æãã¦ããå¿ è¦ãããã¾ãã
ãããªãããªã§ããã£ã¼ã«ãã¤ã³ã¸ã§ã¯ã·ã§ã³ãä½ãå´ã®é½åã¯æ¥½ã£ã½ãã ãã®æ¬¡ã«ã¤ã³ã¹ã¿ã³ã¹çæãããã©ã«ãã³ã³ã¹ãã©ã¯ã¿ã§ããã»ãã¿ã¼ã¤ã³ã¸ã§ã¯ã·ã§ã³ã楽ã§ãä¸çªé¢åãªã®ãã³ã³ã¹ãã©ã¯ã¿ã¤ã³ã¸ã§ã¯ã·ã§ã³ã ã£ããã
ã¾ãã使ãå´ã«ã¨ã£ã¦ã¯ç¥ã£ããã£ã¡ããªãã§ãã 使ãå´ã¯å®å®ããå®æããã¤ã³ã¹ã¿ã³ã¹ãæã«å ¥ãã°ãããã§ã
ã§ãçµè«
ã³ã³ã¹ãã©ã¯ã¿ã¤ã³ã¸ã§ã¯ã·ã§ã³ã使ãã¾ãããã