{"id":533,"date":"2023-03-06T22:30:37","date_gmt":"2023-03-06T13:30:37","guid":{"rendered":"https:\/\/hirabay.net\/?p=533"},"modified":"2023-03-06T22:30:41","modified_gmt":"2023-03-06T13:30:41","slug":"%e3%80%90springboot%e3%80%91bean%e3%82%84component%e4%bb%a5%e5%a4%96%e3%81%a7bean%e7%99%bb%e9%8c%b2%e3%81%99%e3%82%8b%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/hirabay.net\/?p=533","title":{"rendered":"\u3010SpringBoot\u3011@Bean\u3084@Component\u4ee5\u5916\u3067Bean\u767b\u9332\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">SpringBoot\u3067DI\u30b3\u30f3\u30c6\u30ca\u306bBean\u767b\u9332\u3059\u308b\u3068\u3044\u3048\u3070\u3001<code>@Bean<\/code>\u3084<code>@Component<\/code>\u304c\u30e1\u30a4\u30f3\u3067\u3059\u304c\u3001\u305d\u308c\u4ee5\u5916\u306e\u65b9\u6cd5\u3067Bean\u767b\u9332\u3092\u3059\u308b\u65b9\u6cd5\u3092\u8abf\u3079\u3066\u307f\u307e\u3057\u305f\uff01<\/p>\n\n\n\n<div class=\"toc\"><br \/>\n<b>Warning<\/b>:  Undefined array key \"is_admin\" in <b>\/home\/c4179872\/public_html\/hirabay.net\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>110<\/b><br \/>\n<br \/>\n<b>Warning<\/b>:  Undefined array key \"is_category_top\" in <b>\/home\/c4179872\/public_html\/hirabay.net\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>115<\/b><br \/>\n<br \/>\n<b>Warning<\/b>:  Undefined array key \"is_top\" in <b>\/home\/c4179872\/public_html\/hirabay.net\/wp-content\/themes\/sango-theme\/library\/gutenberg\/dist\/classes\/Toc.php<\/b> on line <b>122<\/b><br \/>\n    <div id=\"toc_container\" class=\"sgb-toc--bullets js-smooth-scroll\">\n      <p class=\"toc_title\">\u76ee\u6b21 <\/p>\n      <ul class=\"toc_list\">\n  <li class=\"first\">\n    <a href=\"#i-0\">\u7d50\u8ad6<\/a>\n  <\/li>\n  <li class=\"last\">\n    <a href=\"#i-1\">\u4f7f\u3044\u6240<\/a>\n  <\/li>\n<\/ul>\n\n      \n    <\/div><\/div><h2 class=\"wp-block-heading\" id=\"i-0\">\u7d50\u8ad6<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>GenericApplicationContext<\/code>\u3092\u4f7f\u3048\u3070\u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306b\u983c\u3089\u305aBean\u767b\u9332\u304c\u3067\u304d\u307e\u3059\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9f\u88c5\u4f8b\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-java\" data-lang=\"Java\"><code>@Configuration\npublic class SampleAutoConfiguration {\n    public SampleAutoConfiguration(GenericApplicationContext context) {\n        \/\/ Bean\u767b\u9332\u3057\u305f\u3044\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u751f\u6210\n        var restTemplate1 = new RestTemplateBuilder()\n                .rootUri(\"http:\/\/localhost:8080\")\n                        .build();\n        var restTemplate2 = new RestTemplateBuilder()\n                .rootUri(\"http:\/\/localhost:80\")\n                .build();\n\n        \/\/ registerBean\u3067\u767b\u9332\n        context.registerBean(\"restTemplate1\", RestTemplate.class, () -&gt; restTemplate1);\n        context.registerBean(\"restTemplate2\", RestTemplate.class, () -&gt; restTemplate2);\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"i-1\">\u4f7f\u3044\u6240<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u540c\u4e00\u30af\u30e9\u30b9\u306eBean\u3092\u5927\u91cf\u306b\u751f\u6210\u3057\u305f\u3044\u6642\u306b\u4fbf\u5229\u3067\u3059\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u3048\u3070\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u306byaml\u3067\u30de\u30c3\u30d7\uff08\u3082\u3057\u304f\u306f\u30ea\u30b9\u30c8\uff09\u3067RestTemplate\u751f\u6210\u306e\u305f\u3081\u306eproperty\u3092\u5b9a\u7fa9\u3057\u3001\u305d\u308c\u305e\u308c\u306eproperty\u306b\u3042\u3063\u305fRestTemplate\u3092\u751f\u6210\u3057\u305f\u3044\u5834\u5408\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-yaml\" data-file=\"application.yaml\" data-lang=\"Yaml\"><code>sample-app:\n  rest-templates:\n    a:\n      root-uri: https:\/\/a.com\n    b:\n      root-uri: https:\/\/b.com<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-java\" data-file=\"Property\u30af\u30e9\u30b9\" data-lang=\"Java\"><code>@Data\n@ConfigurationProperties(\"sample-app\")\npublic class RestTemplateProperties {\n    private Map&lt;String, RestTemplateProperty&gt; restTemplates;\n\n    @Data\n    public static class RestTemplateProperty {\n        private String rootUri;\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Bean\u751f\u6210\u306f\u3053\u3093\u306a\u611f\u3058\u3067\uff11\u3064\u306e\u30e1\u30bd\u30c3\u30c9\u3067\u5b8c\u7d50\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism undefined-numbers lang-java\" data-lang=\"Java\"><code>@Configuration\n@EnableConfigurationProperties(RestTemplateProperties.class)\npublic class SampleAutoConfiguration {\n    public SampleAutoConfiguration(GenericApplicationContext context, RestTemplateProperties properties) {\n        properties.getRestTemplates().keySet().forEach(name -&gt; {\n            RestTemplateProperties.RestTemplateProperty property = properties.getRestTemplates().get(\"name\");\n            var restTemplate = new RestTemplateBuilder()\n                    .rootUri(property.getRootUri())\n                    .build();\n            context.registerBean(name + \"RestTemplate\", RestTemplate.class, () -&gt; restTemplate);\n        });\n    }\n}<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">\u3053\u308c\u3067Bean\u5229\u7528\u3059\u308b\u5834\u5408\u306f <code>aRestTemplate<\/code>\u3084<code>bRestTemplate<\/code>\u3068\u3044\u3046\u540d\u524d\u3067\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\uff01\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SpringBoot\u3067DI\u30b3\u30f3\u30c6\u30ca\u306bBean\u767b\u9332\u3059\u308b\u3068\u3044\u3048\u3070\u3001@Bean\u3084@Component\u304c\u30e1\u30a4\u30f3\u3067\u3059\u304c\u3001\u305d\u308c\u4ee5\u5916\u306e\u65b9\u6cd5\u3067Bean\u767b\u9332\u3092\u3059\u308b\u65b9\u6cd5\u3092\u8abf\u3079\u3066\u307f\u307e\u3057\u305f\uff01 Warning: Undefined array k &#8230; <\/p>\n","protected":false},"author":1,"featured_media":544,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[13],"class_list":["post-533","post","type-post","status-publish","format-standard","has-post-thumbnail","category-6","tag-springboot"],"jetpack_featured_media_url":"https:\/\/hirabay.net\/wp-content\/uploads\/2023\/03\/SpringBoot.png","_links":{"self":[{"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/posts\/533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=533"}],"version-history":[{"count":2,"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/posts\/533\/revisions"}],"predecessor-version":[{"id":545,"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/posts\/533\/revisions\/545"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=\/wp\/v2\/media\/544"}],"wp:attachment":[{"href":"https:\/\/hirabay.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hirabay.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}