{"id":127,"date":"2013-12-02T13:44:17","date_gmt":"2013-12-02T04:44:17","guid":{"rendered":"http:\/\/kei0310.info\/?p=127"},"modified":"2013-12-02T13:46:00","modified_gmt":"2013-12-02T04:46:00","slug":"javaspring-di%e3%81%99%e3%82%8b%e6%96%b9%e5%bc%8f%e3%82%92%e7%b4%b0%e3%81%8b%e3%81%8f%e8%a6%8b%e3%81%a6%e3%81%bf%e3%82%8b%e2%91%a0%e3%80%80scope%e3%81%a7%e3%81%aesingleton%e3%81%a8prototype","status":"publish","type":"post","link":"http:\/\/kei0310.info\/?p=127","title":{"rendered":"[Java][Spring] DI\u3059\u308b\u65b9\u5f0f\u3092\u7d30\u304b\u304f\u898b\u3066\u307f\u308b\u2460\u3000@Scope\u3067\u306eSingleton\u3068Prototype\u306e\u6307\u5b9a"},"content":{"rendered":"<p>\u4eca\u56de\u306f@Scope\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u7528\u3044\u3066\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092DI\u3059\u308b\u65b9\u5f0f\u3092\u9078\u629e\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<h3>\u25cf\u89e3\u8aac<\/h3>\n<p>@Scope\u3067\u6307\u5b9a\u53ef\u80fd\u306a\u5c5e\u6027\u306f\u4ee5\u4e0b\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>@Scope(\"singleton\") :<br \/>\n \u7701\u7565\u53ef\u80fd\u3002Spring\u30b3\u30f3\u30c6\u30ca\u5185\u306b\u5358\u4e00\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u751f\u6210\u3057\u3001\u5e38\u306b\u305d\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304cDI\u3055\u308c\u308b\u3002<br \/>\n@Scope(\"prototype\") :<br \/>\n \u6307\u5b9a\u5fc5\u9808\u3002DI\u3055\u308c\u308b\u5ea6\u306b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092new\u3059\u308b\u3002<\/p>\n<h3>\u25cf\u30b5\u30f3\u30d7\u30eb\u30d7\u30ed\u30b0\u30e9\u30e0<\/h3>\n<p><a href=\"http:\/\/kei0310.info\/?p=115\">\u3053\u3061\u3089\u306e\u8a18\u4e8b<\/a>\u306e\u30b5\u30f3\u30d7\u30eb\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u62e1\u5f35\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u30fbTestService.java<br \/>\n@scope\u3092\u7701\u7565\u3057\u3066\u3044\u308b\u306e\u3067\u3001singleton\u3067DI\u3055\u308c\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<h6>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage test.spring3;\r\n\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component\r\npublic class TestService {\r\n\r\n\tprivate int value = 0;\r\n\r\n\tpublic void exec(){\r\n\t\tSystem.out.println(&quot;exec()\u304c\u5b9f\u884c\u3055\u308c\u307e\u3057\u305f\u3002\u5024\u306f[&quot; + this.value + &quot;]\u3067\u3059\u3002&quot;);\r\n\t}\r\n\r\n\tpublic void setValue(int value){\r\n\t\tthis.value = value;\r\n\t}\r\n}\r\n<\/pre>\n<\/h6>\n<p>\u30fbTestServicePrototype.java<br \/>\n@scope\u3067prototype\u306e\u6307\u5b9a\u3092\u3057\u3066\u3044\u308b\u306e\u3067\u3001\u6bce\u56denew\u3067DI\u3055\u308c\u308b\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<h6>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage test.spring3;\r\n\r\nimport org.springframework.context.annotation.Scope;\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component\r\n@Scope(&quot;prototype&quot;)\r\npublic class TestServicePrototype {\r\n\r\n\tprivate int value = 0;\r\n\r\n\tpublic void exec(){\r\n\t\tSystem.out.println(&quot;exec()\u304c\u5b9f\u884c\u3055\u308c\u307e\u3057\u305f\u3002\u5024\u306f[&quot; + this.value + &quot;]\u3067\u3059\u3002&quot;);\r\n\t}\r\n\r\n\tpublic void setValue(int value){\r\n\t\tthis.value = value;\r\n\t}\r\n}\r\n<\/pre>\n<\/h6>\n<p>\u30fbApp.java<br \/>\nTestService\u3068TestServicePrototype\u30922\u500b\u305a\u3064DI\u3057\u3066\u52d5\u304d\u306e\u9055\u3044\u3092\u78ba\u8a8d\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<h6>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage test.spring3;\r\n\r\nimport org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.context.ApplicationContext;\r\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component\r\npublic class App {\r\n\r\n\t@Autowired\r\n\tprivate TestService service1 = null;\r\n\t@Autowired\r\n\tprivate TestService service2 = null;\r\n\t@Autowired\r\n\tprivate TestServicePrototype servicePrototype1 = null;\r\n\t@Autowired\r\n\tprivate TestServicePrototype servicePrototype2 = null;\r\n\r\n\tpublic void execService(){\r\n\t\tservice1.setValue(1);\r\n\t\tservice2.setValue(2);\r\n\t\tservice1.exec();\r\n\t\tservice2.exec();\r\n\t\tSystem.out.println(&quot;service1\u3068service2\u306f\u540c\u3058\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff08Singleton\uff09\u306a\u306e\u3067\u540c\u4e00\u306e\u5024\u304c\u51fa\u529b\u3055\u308c\u308b\u3002&quot;);\r\n\r\n\t\tservicePrototype1.setValue(1);\r\n\t\tservicePrototype2.setValue(2);\r\n\t\tservicePrototype1.exec();\r\n\t\tservicePrototype2.exec();\r\n\t\tSystem.out.println(&quot;servicePrototype1\u3068servicePrototype2\u306f\u5225\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff08Prototype\uff09\u306a\u306e\u3067\u5225\u306e\u5024\u304c\u51fa\u529b\u3055\u308c\u308b\u3002&quot;);\r\n\t}\r\n\r\n\tpublic static void main( String[] args ){\r\n\r\n\t\t\/\/ Spring\u30b3\u30f3\u30c6\u30ca\u53d6\u5f97\r\n\t\tApplicationContext appContext =\r\n\t\t\t\tnew ClassPathXmlApplicationContext(&quot;classpath:.\/META-INF\/spring\/app-context.xml&quot;);\r\n\t\t\/\/ Spring\u30b3\u30f3\u30c6\u30ca\u304b\u3089App\u30af\u30e9\u30b9\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u53d6\u5f97\r\n\t\tApp app = appContext.getBean(App.class);\r\n\t\t\/\/ app\u3092\u5b9f\u884c\r\n\t\tapp.execService();\r\n    }\r\n}\r\n<\/pre>\n<\/h6>\n<p>\u30fb\u5b9f\u52b9\u7d50\u679c<br \/>\n\u5b9f\u52b9\u7d50\u679c\u306f\u4ee5\u4e0b\u306e\u3068\u304a\u308a\u3067\u3059\u3002<br \/>\nsingleton\u3068prototype\u306e\u52d5\u304d\u306e\u9055\u3044\u304c\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n<h6>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n2013\/12\/02 13:20:56 org.springframework.context.support.AbstractApplicationContext prepareRefresh\r\n\u60c5\u5831: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@38503429: startup date [Mon Dec 02 13:20:56 JST 2013]; root of context hierarchy\r\n2013\/12\/02 13:20:56 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions\r\n\u60c5\u5831: Loading XML bean definitions from class path resource [META-INF\/spring\/app-context.xml]\r\n2013\/12\/02 13:20:56 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons\r\n\u60c5\u5831: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@217f242c: defining beans [app,testService,testServicePrototype,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy\r\nexec()\u304c\u5b9f\u884c\u3055\u308c\u307e\u3057\u305f\u3002\u5024\u306f[2]\u3067\u3059\u3002\r\nexec()\u304c\u5b9f\u884c\u3055\u308c\u307e\u3057\u305f\u3002\u5024\u306f[2]\u3067\u3059\u3002\r\nservice1\u3068service2\u306f\u540c\u3058\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff08Singleton\uff09\u306a\u306e\u3067\u540c\u4e00\u306e\u5024\u304c\u51fa\u529b\u3055\u308c\u308b\u3002\r\nexec()\u304c\u5b9f\u884c\u3055\u308c\u307e\u3057\u305f\u3002\u5024\u306f[1]\u3067\u3059\u3002\r\nexec()\u304c\u5b9f\u884c\u3055\u308c\u307e\u3057\u305f\u3002\u5024\u306f[2]\u3067\u3059\u3002\r\nservicePrototype1\u3068servicePrototype2\u306f\u5225\u306e\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\uff08Prototype\uff09\u306a\u306e\u3067\u5225\u306e\u5024\u304c\u51fa\u529b\u3055\u308c\u308b\u3002\r\n<\/pre>\n<\/h6>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u56de\u306f@Scope\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u7528\u3044\u3066\u3001\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092DI\u3059\u308b\u65b9\u5f0f\u3092\u9078\u629e\u3057\u3066\u307f\u307e\u3059\u3002 \u25cf\u89e3\u8aac @Scope\u3067\u6307\u5b9a\u53ef\u80fd\u306a\u5c5e\u6027\u306f\u4ee5\u4e0b\u3068\u306a\u308a\u307e\u3059\u3002 @Scope(\"singleton\") : \u7701\u7565\u53ef\u80fd\u3002Spring\u30b3\u30f3\u30c6\u30ca\u5185 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[18,183,17,16,13,14],"_links":{"self":[{"href":"http:\/\/kei0310.info\/index.php?rest_route=\/wp\/v2\/posts\/127"}],"collection":[{"href":"http:\/\/kei0310.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/kei0310.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/kei0310.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kei0310.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=127"}],"version-history":[{"count":4,"href":"http:\/\/kei0310.info\/index.php?rest_route=\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":131,"href":"http:\/\/kei0310.info\/index.php?rest_route=\/wp\/v2\/posts\/127\/revisions\/131"}],"wp:attachment":[{"href":"http:\/\/kei0310.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/kei0310.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/kei0310.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}