DIã£ã¦æ¬å½ã«å¿ è¦? ãã®2
http://d.hatena.ne.jp/higayasuo/20070416#1176719022ã®ç¶ãã
ããã§ã¯ãDIContainerã®æ©è½ã§æ¬å½ã«å¿
è¦ãª2ã¤ã®æ©è½ã£ã¦ä½ã§ãããããä¸ã¤ã¯ãã¤ã³ã¹ã¿ã³ã¹ã®ã¹ã³ã¼ã管çã§ããã¹ã³ã¼ãã¨ã¯ãsingletonã ã¨ãprototypeã®ããã«ã¤ã³ã¹ã¿ã³ã¹ãåå¨ããç¯å²ã®ãã¨ã§ãããã¨ãã°ãsingletonã®å ´åã¯ããã¤åå¾ãã¦ãåãã¤ã³ã¹ã¿ã³ã¹ãè¿ã£ã¦ãã¦ãprototypeã®å ´åã¯ãåå¾ãããã³ã«æ°ããã¤ã³ã¹ã¿ã³ã¹ãè¿ã£ã¦ãã¾ãã
ãã®ã¹ã³ã¼ã管çã¯ãåç´ã«ãªãã¸ã§ã¯ããnewãã¦ããã ãã§ã¯éæã§ãã¾ããã以åã®ã³ã¼ããè¦ã¦ã¿ã¾ãããã
ããã¯ãããæå³prototypeã§ããããããsingletonã«ãããå ´åã次ã®ãããªã³ã¼ãã«ããå¿ è¦ãããã¾ãã
Service service = new Service();
prototypeãsingletonã«å¤ããå ´åã«ãå©ç¨è å´ã«ã³ã¼ãã®å¤æ´ãå ¥ãã®ãã¡ãã£ã¨ãããªæãã§ããããã¾ã§ã®DIä¿¡è ã®è¨ãåã ã¨ããã¹ã³ã¼ãã®å¤æ´ã«ãã£ã¦å©ç¨è å´ã®ã³ã¼ãã«å¤æ´ãå ¥ãã®ã¯ãããªããå¤æ´ã«å¼·ãããããã«ã¯ãDIContainerãå¿ è¦ã ããã®ãããªè©±ã«ãªãã§ãããã
Service service = Service.getInstance();
ã§ããå®éã«éä¸ã§ã¹ã³ã¼ããå¤æ´ã«ãªããã¨ã£ã¦ããã§ãããããServiceãããã¯Daoã¨ãã£ãããã«ã³ã³ãã¼ãã³ãã®ç¨®é¡ã«ãã£ã¦ããã¹ãã¹ã³ã¼ãã¨ããã®ã¯ã決ã¾ã£ã¦ãã¾ãããéä¸ã§å¤æ´ã«ãªããã¨ã¯ã»ã¨ãã©ãªãæ°ããã¾ãã
ä»®ã«ãã£ãå ´åããã¨ãã°ãprototypeãsingletonã«å¤ããå ´åãpublic defaultã®ã³ã³ã¹ãã©ã¯ã¿ãåé¤ãã¦privateã«staticãªgetInstance()ã¡ã½ããã追å ãã¦ããã°ãããã¾ã§ç´æ¥newãã¦ããé¨åã¯ãã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªãã¾ããããã¨ã©ã¼ã«ãªã£ãã¨ãããçã£ç«¯ãããgetInstance()ããããã«ä¿®æ£ããã°ããã®ã§ããããªé£ãããã®ã§ã¯ããã¾ããã
ãããã¹ã³ã¼ã管çããããªãã¸ã£ã³ãã¨ãã話ã«ãªãããã§ãããã¡ãã£ã¨éãã¾ããsingletonã«ããã¨ããæ©æ¢°çã§åããããªã³ã¼ããåã¯ã©ã¹ã«ã³ã¼ãã£ã³ã°ããã®ã¯ããã£ããã話ã§ãããã¨ãã°ãã¯ã©ã¹ã«@Singletonã¨æ¸ãã¦ããã°ãèªåçã«singletonã«ãªããªãé常ã«æ¥½ãã³ã§ãã
ã¤ã¾ããã¹ã³ã¼ã管çã¯DIContainerã¨ãã¦å¿ è¦ãªæ©è½ã ã¨æãã¾ãã
ç¶ãã¯æ¬¡ã®ã¨ã³ããªã¼ã§ã
DIã£ã¦æ¬å½ã«å¿ è¦? ãã®3
http://d.hatena.ne.jp/higayasuo/20070417#1176775996ã®ç¶ãã
ããã§ã¯ãDIContainerãæä¾ããæ©è½ã§ãã£ã¨ãéè¦ãªæ©è½ã£ã¦ä½ã§ããããã
AOPã ã¨æãã¾ãã
ãããªãããAOPå°ç¨ãã¬ã¼ã ã¯ã¼ã¯ã使ãã°ããããããã¨ãã声ãèããã¦ãããã§ãããç¾å®çã«æ®åãã¦ããAOPå°ç¨ãã¬ã¼ã ã¯ã¼ã¯ããªã(ã©ã¤ãã©ãªã¨ãã¦ã¯Javassist, cglib, asmãªã©ã¯æ®åãã¦ãã¾ãã)ã®ã§ãAOPã使ãããã¨ãã¯ãDIContainerã使ãã¨ããã®ãç¾å®çãªé¸æè¢ã§ã¯ãªãã§ããããã
AOPã¯ããã©ã³ã¶ã¯ã·ã§ã³ããã°ãã»ãã¥ãªãã£ãä¾å¤ã®ä¸å
管çãªã©é常ã«æç¨ã§ãAOPæãã®éçºã¯èããããªãã»ã©ã§ãã
AOPã使ããªãã¦ãåæ§ã®ãã¨ã¯ã§ãã¾ããããã®åå·¥æ°ãããã¿ã¾ãã
AOPã¯ãã·ã¹ãã ã«ç²¾éããã¨ã³ã¸ãã¢ãä¸å
è¨å®ãã¦ããã°ãæ®éã®éçºè
ã¯ãã®åå¨ãæ°ã«ããå¿
è¦ã¯ãªãã®ã§ã失æãã«ããå®å®ããæè¡ã ã¨æãã¾ãã
ã¾ã¨ããã¨ãDIã¯ãã¹ããããããããããã«éè¦ã§ãããå¥ã«ããããDIContainerã使ããªãã¦ãsetterã¡ã½ããããç¨æãã¦ããã°å©ç¨ã§ãã¾ããDIContainerã使ãã¡ãªããã¯ãAOPã¨ã¤ã³ã¹ã¿ã³ã¹ç®¡çã«ããã®ã§ã¯ãªããã¨æãã¾ããè¨ãæ¹ãå¤ããã°ãDIã¯è¡ãããAOPã¨ã¤ã³ã¹ã¿ã³ã¹ç®¡çã«ç¹åãããã¬ã¼ã ã¯ã¼ã¯ããã£ã¦ãååã«æç¨ãªã®ã§ã¯ãªãã§ããããã
ã¯ã£ãããã£ã¦ä»ã®éçºãè¤éã«ãã¦ããã®ã¯ãããããããPOJOä¿¡ä»°ãã§ã¯ãªããã¨æã£ã¦ãã¾ããä»åã®ä¸é£ã®ã¨ã³ããªã¼ã¯èªåèªèº«ã¸ã®åçãè¸ã¾ãã¦æ¸ãã¦ãã¾ãã
ãã¨ãã¨ã®POJOã®ç®çã¯ã
- ç¹å®ã®ç°å¢ã«ä¾åããªãã®ã§ãã¹ãããããã
- ç¹å®ã®ãã¬ã¼ã ã¯ã¼ã¯ã®ãã¨ãè¦ããªãã¦ãéçºã§ãã(æ®éã®Javaã ãã)ã
ã¨ãã£ããã¨ããã£ãã¨æãã¾ããæ²è
ã¯ãäºã¤ç®ã§ããããããã«POJOãæ¸ãåã«ã¯ä½åãªç¥èã¯è¦ãã¾ããããDIContainerã使ã£ã¦ã³ã³ãã¼ãã³ããçµã¿åãããã¨ãããé£ããã¨ããã ã¨æãã¾ãã
çµå±ãDIContainerã®è¨å®ãã¡ã¤ã«ãæ¸ãã®ã¯æ·å±
ã®é«ãè¡çºã§ããSeasar2ã®ããã«è¨å®ãã¡ã¤ã«ããªããã°ãè¨å®ãã¡ã¤ã«ãæ¸ãã¨ããè¡çºã¯å¿
è¦ãªããªãã¾ãããèªåã§å¦çããã¦ä¸å®ã¨ãã声ã¯ããã§ãããã
IoC(èªåã§å¼ã³åºããã«å¤é¨ããèªåãå¼ã³åºããã)ã¨ããã®ã¯ãèªåã§å¦çãã³ã³ããã¼ã«ã§ããªãã®ã§ããã¨ãã¨ç´æçãªãã®ã§ã¯ãªãã®ã§ããIoCãDIãæ·å±
ã®é«ãæè¡ã«ããåå ã ã¨ç§ã¯åæãã¦ãã¾ãã
ããã§ã¯ãã©ãããã°ããã®ã§ãããããç¶ãã¯æ¬¡ã®ã¨ã³ããªã¼ã§ã