{"id":2585,"date":"2018-05-06T21:31:52","date_gmt":"2018-05-06T12:31:52","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=2585"},"modified":"2023-05-27T23:11:48","modified_gmt":"2023-05-27T14:11:48","slug":"%e3%80%90spring-framework%e3%80%91di%e4%be%9d%e5%ad%98%e6%80%a7%e3%81%ae%e6%b3%a8%e5%85%a5%e3%82%92%e5%ae%9f%e8%a3%85%e3%81%97%e3%81%a6%e3%81%bf%e3%82%8b%e3%80%82%e8%a8%ad%e5%ae%9a%e7%94%a8","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=2585","title":{"rendered":"\u3010Spring Framework\u3011DI(\u4f9d\u5b58\u6027\u306e\u6ce8\u5165)\u3092\u5b9f\u88c5\u3057\u3066\u307f\u308b\u3002(\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306b\u3088\u308b\u8a2d\u5b9a\u7528\u30af\u30e9\u30b9\u304b\u3089\uff09"},"content":{"rendered":"<p>\u524d\u56de\u306f\u3001bean\u306e\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u3092\u7528\u3044\u3066\u3001DI\u3092\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"upTOKEMqMW\"><p><a href=\"http:\/\/www.code-magagine.com\/?p=2571\">\u3010Spring Framework\u3011DI(\u4f9d\u5b58\u6027\u306e\u6ce8\u5165)\u3092\u5b9f\u88c5\u3057\u3066\u307f\u308b\u3002(\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u304b\u3089\uff09<\/a><\/p><\/blockquote>\n<p><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"http:\/\/www.code-magagine.com\/?p=2571&#038;embed=true#?secret=upTOKEMqMW\" data-secret=\"upTOKEMqMW\" width=\"500\" height=\"282\" title=\"&#8220;\u3010Spring Framework\u3011DI(\u4f9d\u5b58\u6027\u306e\u6ce8\u5165)\u3092\u5b9f\u88c5\u3057\u3066\u307f\u308b\u3002(\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u304b\u3089\uff09&#8221; &#8212; \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u30de\u30ac\u30b8\u30f3\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>\u6b21\u306f\u3001<span style=\"color: #ff0000;\"><strong>\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u3063\u3066<\/strong><\/span>\u3001\u8a2d\u5b9a\u7528\u306e\u30af\u30e9\u30b9\u3092\u4f5c\u308a\u305d\u3053\u304b\u3089\u6ce8\u5165\u3059\u308b\u30b5\u30f3\u30d7\u30eb\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p>\u6700\u8fd1\u306eSpring\u306b\u3088\u308bDI\u306f\u3001Bean\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306b\u3088\u308b\u3082\u306e\u3088\u308a\u3082\u3001<span style=\"color: #ff0000;\"><strong>\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306b\u3088\u308b\u8a2d\u5b9a\u30af\u30e9\u30b9\u3092\u4f5c\u3063\u3066\u6ce8\u5165\u3059\u308b\u5f62\u304c\u4e3b<\/strong><\/span>\u3068\u306a\u3063\u3066\u304d\u3066\u3044\u307e\u3059\u3002\u662f\u975e\u3001\u3053\u3061\u3089\u3082\u899a\u3048\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>\u30b5\u30f3\u30d7\u30eb<\/h2>\n<h3>\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306b\u3088\u308b\u8a2d\u5b9a\u7528\u30af\u30e9\u30b9<\/h3>\n<pre class=\"lang:default decode:true\">import org.springframework.context.annotation.Bean;\r\nimport org.springframework.context.annotation.Configuration;\r\n\r\n@Configuration\r\npublic class BeanConfig {\r\n\r\n\t@Bean\r\n\tpublic TestBean configBean(){\r\n\t\treturn new TestBean(\"Hello World!\");\r\n\t}\t\r\n\r\n}<\/pre>\n<h4>\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306e\u8aac\u660e<\/h4>\n<table>\n<tbody>\n<tr>\n<th>\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u540d<\/th>\n<th>\u8aac\u660e<\/th>\n<\/tr>\n<tr>\n<td>@Configuration<\/td>\n<td>Bean\u306e\u8a2d\u5b9a\u3092\u884c\u3046\u30af\u30e9\u30b9\u3067\u3042\u308b\u3053\u3068\u3092\u793a\u3057\u307e\u3059\u3002\uff08\u30af\u30e9\u30b9\u306e\u5148\u982d\u306b\u3064\u3051\u308b\u3002\uff09<\/td>\n<\/tr>\n<tr>\n<td>@Bean<\/td>\n<td>\u3053\u308c\u3092\u3064\u3051\u305f\u30e1\u30bd\u30c3\u30c9\u306f\u3001Bean\u3092\u4f5c\u6210\u3059\u308b\u3082\u306e\u3068\u8a8d\u8b58\u3055\u308c\u308b\u3002\u623b\u308a\u5024\u3068\u3057\u3066\u5fc5\u305aBean\u30af\u30e9\u30b9\u540d\u306b\u3059\u308b\u3002\uff08\u30e1\u30bd\u30c3\u30c9\u306e\u5148\u982d\u306b\u3064\u3051\u308b\u3002\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u8a2d\u5b9a\u3092\u6ce8\u5165\u3059\u308bBean\u30af\u30e9\u30b9<\/h3>\n<pre class=\"lang:default decode:true\">public class TestBean{\r\n\r\n\tprivate String helloworld; \r\n\t\t\r\n\tpublic TestBean(String helloworld){\r\n\t\tthis.helloworld = helloworld;\r\n\t}\r\n\r\n\tpublic String getHelloworld() {\r\n\t\treturn helloworld;\r\n\t}\r\n\r\n\tpublic void setHelloworld(String helloworld) {\r\n\t\tthis.helloworld = helloworld;\r\n\t}\r\n\r\n}<\/pre>\n<h3>\u5b9f\u884c\u7528\u30af\u30e9\u30b9<\/h3>\n<pre class=\"lang:default decode:true\">import org.springframework.context.ApplicationContext;\r\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\r\n\r\npublic class Main {\r\n\r\n\tpublic static void main(String[] args) {\r\n\r\n\t\tApplicationContext app = new AnnotationConfigApplicationContext(BeanConfig.class);\r\n\t\tTestBean beantest = (TestBean)app.getBean(TestBean.class);\r\n\t\tSystem.out.println(beantest.getHelloworld());\r\n\t\r\n\t}\r\n\r\n}<\/pre>\n<p>\u300cAnnotationConfigApplicationContext(\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u306e\u30af\u30e9\u30b9.class);\u300d\u3068\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u3001<span style=\"color: #ff0000;\"><strong>\u6ce8\u5165\u3059\u308b\u8a2d\u5b9a\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u53d6\u5f97<\/strong><\/span>\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h3>\u30d7\u30ed\u30b0\u30e9\u30e0\u69cb\u6210<\/h3>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2588\" src=\"http:\/\/www.code-magagine.com\/wp-content\/uploads\/2018\/05\/\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3_\u30d7\u30ed\u30b0\u30e9\u30e0\u69cb\u6210.png\" alt=\"\" width=\"236\" height=\"127\" \/><\/p>\n<p>\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u304c\u306a\u304f\u306a\u308a\u307e\u3057\u305f\u306d\u3002<\/p>\n<h3>\u5b9f\u884c\u7d50\u679c<\/h3>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2589\" src=\"http:\/\/www.code-magagine.com\/wp-content\/uploads\/2018\/05\/\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3DI_\u5b9f\u884c\u7d50\u679c.png\" alt=\"\" width=\"292\" height=\"161\" \/><\/p>\n<h2>\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3092\u4f7f\u3046\u3002<\/h2>\n<h3>\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u7528\u30af\u30e9\u30b9<\/h3>\n<pre class=\"lang:default decode:true\">import org.springframework.beans.factory.annotation.Autowired;\r\nimport org.springframework.stereotype.Component;\r\n\r\n@Component\r\npublic class BeanComponent {\r\n\t@Autowired\r\n\tprivate TestBean bean;\r\n\t\r\n\tpublic void getHello() {\r\n\t\tSystem.out.println(bean.getHelloworld());\r\n\t}\r\n}\r\n<\/pre>\n<h4>\u4f7f\u3063\u3066\u3044\u308b\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3<\/h4>\n<table>\n<tbody>\n<tr>\n<th>\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u540d<\/th>\n<th>\u8aac\u660e<\/th>\n<\/tr>\n<tr>\n<td>@Component<\/td>\n<td>\u305d\u306e\u30af\u30e9\u30b9\u304c\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3067\u3042\u308b\u3053\u3068\u3092\u6307\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td>@Autowired<\/td>\n<td>\u30b3\u30f3\u30d5\u30a3\u30b0\u30af\u30e9\u30b9\u304b\u3089\u3001\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u3092\u63a2\u3057\u3066\u81ea\u52d5\u7684\u306b\u4ee3\u5165\u3057\u3066\u304f\u308c\u308b\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u30b3\u30f3\u30d5\u30a3\u30b0\u30af\u30e9\u30b9<\/h3>\n<p>\u57fa\u672c\u306f\u3001\u4e0a\u3067\u3054\u7d39\u4ecb\u3057\u305f\u30af\u30e9\u30b9\u3068\u540c\u3058\u3067\u3059\u304c\u3001\u300c@ComponentScan\u300d\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u30af\u30e9\u30b9\u306e\u524d\u306b\u8ffd\u52a0\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u3092\u3064\u3051\u308b\u3053\u3068\u3067\u3001\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u3092\u691c\u7d22\u3057\u3066\u3001\u30a4\u30f3\u30b9\u30bf\u30f3\u30b9\u5316\u3057\u3066ApplicationContext\u306b\u767b\u9332\u3057\u3066\u304f\u308c\u308b\u3002<\/p>\n<pre class=\"lang:default decode:true\">import org.springframework.context.annotation.Bean;\r\nimport org.springframework.context.annotation.ComponentScan;\r\nimport org.springframework.context.annotation.Configuration;\r\n\r\n@Configuration\r\n@ComponentScan\r\npublic class BeanConfig {\r\n\r\n\t@Bean\r\n\tpublic TestBean configBean(){\r\n\t\treturn new TestBean(\"Hello World!\");\r\n\t}\t\r\n\r\n}\r\n<\/pre>\n<h3>\u5b9f\u884c\u7528\u30af\u30e9\u30b9<\/h3>\n<pre class=\"lang:default decode:true\">import org.springframework.context.ApplicationContext;\r\nimport org.springframework.context.annotation.AnnotationConfigApplicationContext;\r\n\r\npublic class Main {\r\n\r\n\tpublic static void main(String[] args) {\r\n\r\n\t\tApplicationContext app = new AnnotationConfigApplicationContext(BeanConfig.class);\r\n\t\tBeanComponent comp = app.getBean(BeanComponent.class);\r\n\t\tcomp.getHello();\r\n\t\t\r\n\t}\r\n\r\n}<\/pre>\n<h3>\u5b9f\u884c\u7d50\u679c<\/h3>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2589\" src=\"http:\/\/www.code-magagine.com\/wp-content\/uploads\/2018\/05\/\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3DI_\u5b9f\u884c\u7d50\u679c.png\" alt=\"\" width=\"292\" height=\"161\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"\u524d\u56de\u306f\u3001bean\u306e\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u3092\u7528\u3044\u3066\u3001DI\u3092\u5b9f\u88c5\u3057\u3066\u307f\u307e\u3057\u305f\u3002 \u3010Spring Framework\u3011DI(\u4f9d\u5b58\u6027\u306e\u6ce8\u5165)\u3092\u5b9f\u88c5\u3057\u3066\u307f\u308b\u3002(\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u304b\u3089\uff09 \u6b21\u306f\u3001\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u4f7f\u3063\u3066\u3001\u8a2d\u5b9a\u7528\u306e\u30af\u30e9\u30b9\u3092\u4f5c\u308a\u305d\u3053\u304b\u3089\u6ce8 [&hellip;]","protected":false},"author":1,"featured_media":2588,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[31],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/2585"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2585"}],"version-history":[{"count":8,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/2585\/revisions"}],"predecessor-version":[{"id":20574,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/2585\/revisions\/20574"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/media\/2588"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2585"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}