SpringFrameworkã§å½åè¦ç´ãã¼ã¹ã®è¨å®ãå®ç¾ããArid POJOs
è¨äºã®ã¿ã¤ãã«ã¿ããã§ããã1è¡ã§è¨ãã°ããããäºã§ããä»ã¾ã§ï¼Springã«ï¼ç¡ãã£ãã®ãä¸æè°ãªãããã§ããã
ä½è
ã¯POJOs IN ACTIONã®èè
Chris Richardsonæ°ã
ã§ã¯ãæ©é使ã£ã¦ã¿ã¾ãã
åºæ¬ãµã³ãã«
ã¾ãä¸è¨ãµã¤ããããã¦ã³ãã¼ãã
Arid POJOsのWebサイト
Guiceã«ç¶ãGoogle Codeã§ããã¡ãªã¿ã«ãªã¼ãã³ã½ã¼ã¹ãªãã¹ãã§ã¯Codehausãä¸çªå°ã£ãã¤ã¡ã¼ã¸ã§ãã£ãè¯ãã¨æãã¾ããç§ã®ä¸ã§ãã©ãã§ãè¯ãã§ããã
- ä»å試ããµã³ãã«ã®ããã±ã¼ã¸æ§æ
- sample.arid
- SimpleBootStrap : èµ·åã¯ã©ã¹
- SimpleClient : ãµã¼ãã¹å¼ã³åºãå
- sample.arid.service
- SimpleService : ãµã¼ãã¹ã¤ã³ã¿ãã§ã¼ã¹
- SimpleServiceImple : ãµã¼ãã¹å®è£
- sample.arid
å®è£
ã®ä¸èº«ã¯ä¾ã«ãã£ã¦ã©ãã§ãè¯ãã®ã§å²æãã¾ãï¼1ç¹ã ããSimpleClientã¯SetterInjectionã§ãï¼ã
ãã¦XMLå®ç¾©ã§ãããArid POJOsã®å®ç¾©ã¯Spring2ã§å°å
¥ãããXML Schemaã®名前空間拡張ãç¨ãã¦ãã¾ããXMLæ¡å¼µã¨è¨ãã°XBeanã¯ã©ããã¦ãã ããã¨æã£ã¦1å¹´åã¶ããããã«è¦ããããªããããåãããªããªã£ã¦ã¾ãâ¦ãXBeanã¯ãJames Strachanæ°ãSpringãã¼ã ã«ææ¡ãããï¼çèã®æ«ï¼æå¦ããã¦ããSpringãã¼ã ã«ã¯å¤±æããï¼ãã¨ãæã£ã¦ããã§ããããçµå±2.0ã§åããããªä»æ§ã«ãªã£ããã§ããã
話ãé¸ãã¾ããããAridã®å®ç¾©ãåå空éã®å®£è¨ã¨ãå®ç¾©æ¬ä½ã
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:arid="http://chrisrichardson.net/schema/arid" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://chrisrichardson.net/schema/arid http://chrisrichardson.net/schema/arid.xsd"> <arid:define-beans package="sample.arid" autowire="byType"/> </beans>
ããã ãã§ããããã±ã¼ã¸ã¨autowireã®ã¿ã¤ããæå®ããã ããã§ã¯å®è¡ã
Re: Hello, Arid POJOs!
æåï¼*1
ã¨ãããã ãã ã¨ä½ã®ãã£ã¡ããªã®ã§ãããä¸è¨ã®XMLå®ç¾©ã¯ä»¥ä¸ã®ããã«æ¸ããã®ã¨åãã§ãã
<bean id="simpleClient" class="sample.arid.SimpleClient" autowire="byType" /> <bean id="simpleService" class="sample.arid.service.SimpleServiceImpl" autowire="byType"/>
ä¸è¬åããã¨ã以ä¸ã®ããã«åä½ãã¾ãã
- packageå±æ§ã§æå®ããããã±ã¼ã¸ä»¥ä¸ï¼ãµãããã±ã¼ã¸ãå«ãï¼ã®å ¨å ·è±¡ã¯ã©ã¹ã対象ã«ã
- ã¯ã©ã¹åã®é ãå°æåã«ãï¼æ´ã«"Impl"ã§çµããå ´åã¯"Impl"ãåã£ã¦ï¼idã«ãã
- autowireå±æ§ã§æå®ããautowireãæå®ãã¦
ãå®ç¾©ãããã®ã¨ããã
ã¡ãªã¿ã«autowireã®ããã©ã«ãå¤ã¯constractorãªã®ã§ãã³ã³ã¹ãã©ã¯ã¿ã¤ã³ã¸ã§ã¯ã·ã§ã³ã®å ´åã«ã¯autowireãçç¥å¯è½ã§ãã
ãã®ä»ã®è¨å®
ä¸è¨ã®åºæ¬è¨å®ã«å ãã¦ãAridã§ã¯ä»¥ä¸ã®ãããªè¨å®ãå¯è½ã§ãã
æå®ãããã¿ã¼ã³ã«åè´ããã¯ã©ã¹ã®ã¿ã対象ã¨ãã
ããã±ã¼ã¸ãæå®ããã ãã ã¨ããµãããã±ã¼ã¸ãå«ãå
¨å
·è±¡ã¯ã©ã¹ã対象ã¨ãªã£ã¦ãã¾ãã¾ãããªããAridã®ã½ã¼ã¹ã¯ String classPattern = "classpath*:/" + packagePart + "/**/*.class"; ã¨ããªã£ã¦ãã
ããã§ãpatternå±æ§ãã¤ãã¦ãSpring2.0ãããAspectJææ³ã§æ´ã«ã¯ã©ã¹ãçµãè¾¼ãã¾ã*2ã
<arid:define-beans package="sample.arid" pattern="@org.springframework.transaction.annotation.Transactional sample..Simple*" autowire="byType"/>
ãã®ä¾ã ã¨ã@Transactionalãä»ãã¦ãã¦ãsampleããã±ã¼ã¸ä»¥ä¸ã§ãã¯ã©ã¹åãSimpleã§å§ã¾ãã¯ã©ã¹ã対象ã¨ãã¦ãã¾ãã
ã¯ã©ã¹åâbeanåã®å¤æã«ã¼ã«ãå¤ãã
ããã©ã«ãã ã¨é ãå°æååï¼æ«å°¾ã«Implããã£ããé¤å»ãã¨ããã«ã¼ã«ã§ãããèªåã§å¤æã«ã¼ã«ãå®è£
ãã¦ä½¿ç¨ããäºãã§ãã¾ãã
å®è£
ä¾ã¯çç¥ãã¾ãããAridBeanNameGeneratorãå®è£
ãã¦ãXMLã§ä»¥ä¸ã®ããã«name-generatorå±æ§ãæå®ããã ãã§ãã
<arid:define-beans package="sample.arid" name-generator="sample.arid.MyBeanNameGenerator" />
ä¸é¨ã®ã¯ã©ã¹ãä¾å¤çãªå®ç¾©ã«ãã
以ä¸ã®ããã«è¨è¿°ããäºã§ãèªåçã«çæãããå®ç¾©ããªã¼ãã¼ã©ã¤ãããäºãå¯è½ã§ãã
<arid:define-beans package="sample.arid" autowire="byType"> <arid:extras> <bean id="simpleService" class="sample.arid.service.SimpleServiceImpl"> <property name="key" value="1" /> </bean> </arid:extras> </arid:define-beans>
ãã®ä¾ã§ã¯ã"simpleService"ã¨ããidã®å®ç¾©ããªã¼ãã¼ã©ã¤ããã¦ãã¾ãã®ã§ãå®è¡æã¯èªåçã«ä½ãããå®ç¾©ã§ã¯ãªããã¡ãã®å®ç¾©ãæ¡ç¨ããã¾ãã
çµè«
ã¿ã¤ãã«ã«ãæ¸ããéãããã®Arid POJOsã使ãã°Spring Frameworkã§æè¬CoC(Convention Over Configuration)ãé¨åçã«å®ç¾ã§ãã¾ãã®ã§ãXMLãè¥å¤§åããå ´åã®ã½ãªã¥ã¼ã·ã§ã³ã¨ãã¦æ¤è¨ãã価å¤ãããã¨æãã¾ãããã¡ããçç¥ããäºã«ãããã¡ãªãããããã¾ããããã¯ãå±éã«è¿ããããªã·ã³ãã«ãªä»çµã¿ãªã®ã§é¨åçå°å ¥ãå¯è½ã§ããããªã«ãããçç¥ãããã¨ããé¸æè¢ãã§ããäºãã¨ã¦ãåã°ããã§ããã
*1:ã½ã¼ã¹è¦ããã«ãæåï¼ãããªãã§ãã
*2:AspectJ weaverãå¿ è¦ãªã®ã§ãSpringã®dependencyã«å ¥ã£ã¦ããã¤ã使ããªã©ãã¦ä¸ãããmavenã使ãã¨åæã«å ¥ãã¨æãã¾ãã