Spring Java Configuration(1)
Guice㨠Springframeworkã®æ大ã®éãã¯DIãAOPã®è¨å®ãJavaã³ã¼ãã§æ¸ããXMLã§æ¸ããã§ãããå®ã¯(?)Springã§ãJava ã³ã¼ãã«ããè¨å®ã®è¨è¿°æ段ãç¨æããã¦ãã¾ã*1ãä»åã¯ãã®"Spring Java Configuration"ãç°¡åã«è¦ã¦ã¿ã¾ãããªããã¾ã ãã¤ã«ã¹ãã¼ã³1ãªã®ã§ãç¨æããã¦ããã¨ããããã¯æºåããã¦ããããããã¯è¨ç»ä¸ã¨è¨ãæ¹ãé©åããç¥ãããä»å試ãå 容ãä»å¾ã®ãªãªã¼ã¹ã§è²ã å¤ããå¯è½æ§ãããã¾ãã
Spring Java Configurationã®æãåºæ¬çãªãµã³ãã«
ã¾ãinjectããããµã¼ãã¹ãã¤ã³ã¿ãã§ã¼ã¹ã¨ãã®å®è£ ã§ãã
public interface Service { String getResponse(String msg); } public class ServiceImpl implements Service { public String getResponse(String msg) { return "Re: " + msg; } }
次ã«ãinjectå ´æã§ããClientãå¾ã§ãæ¸ãã¾ãããGuiceã®å ´åã¨ç°ãªããå³å¯ãªæå³ã§ã®POJOã§ãã
public class SpringClient { private Service service; public SpringClient(Service service) { this.service = service; } public void execute() { System.out.println(service.getResponse("Hello")); } /** ãã¹ãç¨ **/ Service getService() { return service; } }
ããã¦ããããè¨å®ããã³ã¼ãã§ããGuiceã¯Moduleãå®è£ ããã¯ã©ã¹ã§ããããSpring Java Configurationã§ã¯ã¢ããã¼ã·ã§ã³ã§è¨å®ã³ã¼ãã§ããäºã示ãã¾ãã
import org.springframework.beans.factory.annotation.Bean; import org.springframework.beans.factory.annotation.Configuration; import org.springframework.beans.factory.annotation.Scope; // è¨å®ã³ã¼ãã§ããäºã示ãã¢ããã¼ã·ã§ã³ï¼è²ã ãªãã·ã§ã³ãããã¾ããä»åã¯ããã©ã«ãã§ä½¿ç¨ï¼ @Configuration public class SpringConfiguration { // XMLã®<bean>å®ç¾©ã«ç¸å½ããäºã示ãã¢ããã¼ã·ã§ã³ @Bean public Service service() { return new ServiceImpl(); } // ãã¡ãã§ã¯ãããã¿ã¤ããæå®ï¼æå®ããªãã¨ã·ã³ã°ã«ãã³ã«ãªãã®ã¯é常ã®Springè¨å®ã¨åãï¼ @Bean(scope = Scope.PROTOTYPE) public SpringClient client() { return new SpringClient(service()); } }
@Beanãã¤ããã¡ã½ããããã¡ã¯ããªã¡ã½ããã«ãªãã¨ããäºã§ãããã¢ããã¼ã·ã§ã³ãç¡ããã°è¨å®ã®çºã¨ã¯æããªããããªãæ®éã®Javaã³ã¼ãã«ãªã£ã¦ãã¾ãã
æå¾ã«ã³ã³ãããçæããèµ·åé¨åã
public class SpringBootStrap { public static void main(String[] args) { // JavaConfigurationç¨ã®ã³ã³ãããçæ AnnotationApplicationContext context = new AnnotationApplicationContext(SpringConfiguration.class); // ã³ã³ããããBeanãåå¾ï¼ãã®ã³ã¼ãã¯XMLã®å ´åã¨åãï¼ SpringClient client = (SpringClient)context.getBean("client"); client.execute(); SpringClient anotherClient = (SpringClient)context.getBean("client"); // èªåã®newã§ã¯ãªãã¡ããã¨ã³ã³ãã管çããã¦ããäºã確èªããçºãPrototypeã¨Singletonã®ã¤ã³ã¹ã¿ã³ã¹ããã§ã㯠System.out.println("client == anotherClient : " + (client == anotherClient)); System.out.println("client.getService() == anotherClient.getService() : " + (client.getService() == anotherClient.getService())); } }
å®è¡çµæ
Re: Hello
client == anotherClient : false
client.getService() == anotherClient.getService() : true
ã¡ããã¨ãããã¿ã¤ãæå®ããã¤ã³ã¹ã¿ã³ã¹ã¯å¥ã«ãã·ã³ã°ã«ãã³æå®ï¼ããã©ã«ãï¼ã®ã¤ã³ã¹ã¿ã³ã¹ã¯åãã¨ãªã£ã¦å®è¡ããã¾ããã
ææ³
Guiceã®ææ³ã¨æ¯ã¹ã¦éç«ã¤ã®ã¯ãSpring Java Configurationã¯ã¢ããªã±ã¼ã·ã§ã³ã®POJOããã¬ã¼ã ã¯ã¼ã¯ã«ä¾åãããªãï¼=æ±æããªãï¼ããã«ããã¨ããæ¹éã§ããGuiceã¯injectãããå ´æã«@Injectã¢ããã¼ã·ã§ã³ãã¤ããã®ã§ããã®ã¯ã©ã¹ãã¤ã³ã¿ãã§ã¼ã¹ãGuiceã«ä¾åãã¦ãã¾ãã¾ããããããã®Springã®ææ³ã¯XMLã®å ´ååæ§ãå®å
¨ã«å¤é¨ããinject対象ãinjectå ´æãæå®ããã®ã§ãã¢ããªã±ã¼ã·ã§ã³ã®ãªãã¸ã§ã¯ãã¯ã¯ãªã¼ã³ãªã¾ã¾ã§ãã
éã«è¨ãã¨ã©ãã«injectããããã対象ã³ã¼ããè¦ãã ãã ã¨åãããªãã¨ãããã¡ãªãããããã¾ãããããã¯IDEã®ãã©ã°ã¤ã³ãããã解決ãã¦ãããããªã®ã§ãå®éã«ã¯ãã¾ããã¡ãªããã«ãªããªãå¯è½æ§ãé«ãã§ãã
ãã å人çãªææ³ã¨ãã¦ã¯ãè¨å®ã³ã¼ãï¼@Beanãã¤ããã¡ã½ãã群ï¼ãæ®éã®Javaã³ã¼ããªã®ã§ã³ã³ããããªãã¬ã¯ã·ã§ã³ã®ãèãããããªãçºãéã«æ°æã¡ãæªãã¨ããããç´æçã«åãããªãã¨ããã¢ã¤ã¢ã¤æãæãã¦ãã¾ãã¾ãããæ®éã«ã¡ã½ããå¼ã³åºãã¦newãã¦ãçãªã®ã«ãªãã§ã·ã³ã°ã«ãã³ã«ãªããã ãã¨ãããããã¨ããã§ããAOPãããæå³åããªã®ã§ãããå¦çã追å ãããã®ã§ã¯ãªããæ¸ãã¦ããäºã¨åä½ãï¼å°ãï¼éã£ã¦ããã¨ããã®ãã©ããâ¦ãã¾ãæ £ãã®åé¡ã大ããã¨æãã®ã§ãã¡ãã£ã¨çµã£ããããã£ã±ããã£ã¡ã®æ¹ãè¯ããã¨ãè¨ãåºãããç¥ãã¾ãããã
ã¨ããäºã§ãä»åã¯é常ã«åç´ãªä¾ã試ãã¦ã¿ã¾ãããXMLã®è¨å®ã¨ãå ±åã§ããï¼åæã«ä½¿ããï¼ã®ã§ãå¿ è¦ã«å¿ãã¦ã©ã¡ãããé¸ãã ããä¸æã使ãåãã¦ä¸¡æ¹ä½¿ã£ãããç°¡åã«ã§ããããªæãã§ãããããã§ã¯æ¬¡åã¯AOPã試ãã¦ã¿ããã¨æãã¾ãã
*1:Groovyã§æ¸ãã¨ãã£ãæ段ãããããã§ãããå ¨ç¶ç¥ããªãã®ã¨ããã¾ãåºãã¯åãå ¥ãããããã«ãªãæ°ãããã®ã§çç¥ãã¾ã