{"id":4265,"date":"2021-12-02T21:27:53","date_gmt":"2021-12-02T13:27:53","guid":{"rendered":"http:\/\/123.57.164.21\/?p=4265"},"modified":"2021-12-02T21:27:53","modified_gmt":"2021-12-02T13:27:53","slug":"spring-boot%e5%af%bc%e5%85%a5spring%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=4265","title":{"rendered":"Spring Boot\u5bfc\u5165Spring\u914d\u7f6e"},"content":{"rendered":"\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cSpring Boot \u4e2d\u662f\u4e0d\u5305\u542b\u4efb\u4f55\u7684 Spring \u914d\u7f6e\u6587\u4ef6\u7684\uff0c\u5373\u4f7f\u6211\u4eec\u624b\u52a8\u6dfb\u52a0 Spring \u914d\u7f6e\u6587\u4ef6\u5230\u9879\u76ee\u4e2d\uff0c\u4e5f\u4e0d\u4f1a\u88ab\u8bc6\u522b\u3002\u90a3\u4e48 Spring Boot \u9879\u76ee\u4e2d\u771f\u7684\u5c31\u65e0\u6cd5\u5bfc\u5165 Spring \u914d\u7f6e\u5417\uff1f\u7b54\u6848\u662f\u5426\u5b9a\u7684\u3002<\/p>\n\n\n\n<p>Spring Boot \u4e3a\u4e86\u6211\u4eec\u63d0\u4f9b\u4e86\u4ee5\u4e0b 2 \u79cd\u65b9\u5f0f\u6765\u5bfc\u5165 Spring \u914d\u7f6e\uff1a<\/p>\n\n\n\n<ul><li>\u4f7f\u7528 @ImportResource \u6ce8\u89e3\u52a0\u8f7d Spring \u914d\u7f6e\u6587\u4ef6<\/li><li>\u4f7f\u7528\u5168\u6ce8\u89e3\u65b9\u5f0f\u52a0\u8f7d Spring \u914d\u7f6e<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">@ImportResource \u5bfc\u5165 Spring \u914d\u7f6e\u6587\u4ef6<\/h4>\n\n\n\n<p>\u5728\u4e3b\u542f\u52a8\u7c7b\u4e0a\u4f7f\u7528 @ImportResource \u6ce8\u89e3\u53ef\u4ee5\u5bfc\u5165\u4e00\u4e2a\u6216\u591a\u4e2a Spring \u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u4f7f\u5176\u4e2d\u7684\u5185\u5bb9\u751f\u6548\u3002<\/p>\n\n\n\n<p>1. \u4ee5 helloworld \u4e3a\u4f8b\uff0c\u5728 net.biancheng.www.service \u5305\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u00a0PersonService \u7684\u63a5\u53e3\uff0c\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package net.biancheng.www.service;\nimport net.biancheng.www.bean.Person;\npublic interface PersonService {\n    public Person getPersonInfo();\n}<\/pre>\n\n\n\n<p>2. \u5728 net.biancheng.www.service.impl \u5305\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u00a0PersonServiceImpl \u7684\u7c7b\uff0c\u5e76\u5b9e\u73b0\u00a0PersonService \u63a5\u53e3\uff0c\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package net.biancheng.www.service.impl;\nimport net.biancheng.www.bean.Person;\nimport net.biancheng.www.service.PersonService;\nimport org.springframework.beans.factory.annotation.Autowired;\npublic class PersonServiceImpl implements PersonService {\n    @Autowired\n    private Person person;\n    @Override\n    public Person getPersonInfo() {\n        return person;\n    }\n}<\/pre>\n\n\n\n<p>3. \u5728\u8be5\u9879\u76ee\u7684 resources \u4e0b\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a beans.xml \u7684 Spring \u914d\u7f6e\u6587\u4ef6\uff0c\u914d\u7f6e\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?>\n&lt;beans xmlns=\"http:\/\/www.springframework.org\/schema\/beans\"\n       xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n       xsi:schemaLocation=\"http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\">\n    &lt;bean id=\"personService\" class=\"net.biancheng.www.service.impl.PersonServiceImpl\">&lt;\/bean>\n&lt;\/beans>\n<\/pre>\n\n\n\n<p>4. \u4fee\u6539\u8be5\u9879\u76ee\u7684\u5355\u5143\u6d4b\u8bd5\u7c7b\u00a0HelloworldApplicationTests \uff0c\u6821\u9a8c IOC \u5bb9\u5668\u662f\u5426\u5df2\u7ecf personService\uff0c\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package net.biancheng.www;\nimport net.biancheng.www.bean.Person;\nimport org.junit.jupiter.api.Test;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.test.context.SpringBootTest;\nimport org.springframework.context.ApplicationContext;\n@SpringBootTest\nclass HelloworldApplicationTests {\n    @Autowired\n    Person person;\n    \/\/IOC \u5bb9\u5668\n    @Autowired\n    ApplicationContext ioc;\n    @Test\n    public void testHelloService() {\n        \/\/\u6821\u9a8c IOC \u5bb9\u5668\u4e2d\u662f\u5426\u5305\u542b\u7ec4\u4ef6 personService\n        boolean b = ioc.containsBean(\"personService\");\n        if (b) {\n            System.out.println(\"personService \u5df2\u7ecf\u6dfb\u52a0\u5230 IOC \u5bb9\u5668\u4e2d\");\n        } else {\n            System.out.println(\"personService \u6ca1\u6dfb\u52a0\u5230 IOC \u5bb9\u5668\u4e2d\");\n        }\n    }\n    @Test\n    void contextLoads() {\n        System.out.println(person);\n    }\n}\n<\/pre>\n\n\n\n<p>5. \u8fd0\u884c\u5355\u5143\u6d4b\u8bd5\u4ee3\u7801\uff0c\u7ed3\u679c\u5982\u4e0b\u56fe\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-36-1024x295.png\" alt=\"\" class=\"wp-image-4266\" width=\"567\" height=\"162\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-36-1024x295.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-36-300x86.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-36-768x221.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-36-830x239.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-36-230x66.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-36-350x101.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-36-480x138.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-36.png 1790w\" sizes=\"(max-width: 567px) 100vw, 567px\" \/><\/figure><\/div>\n\n\n\n<p>6. \u5728\u4e3b\u542f\u52a8\u7a0b\u5e8f\u7c7b\u4e0a\u4f7f\u7528 @ImportResource \u6ce8\u89e3\uff0c\u5c06 Spring \u914d\u7f6e\u6587\u4ef6 beans.xml \u52a0\u8f7d\u5230\u9879\u76ee\u4e2d\uff0c\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package net.biancheng.www;\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.context.annotation.ImportResource;\n\/\/\u5c06 beans.xml \u52a0\u8f7d\u5230\u9879\u76ee\u4e2d\n@ImportResource(locations = {\"classpath:\/beans.xml\"})\n@SpringBootApplication\npublic class HelloworldApplication {\n    public static void main(String[] args) {\n        SpringApplication.run(HelloworldApplication.class, args);\n    }\n}\n<\/pre>\n\n\n\n<p>7. \u518d\u6b21\u6267\u884c\u6d4b\u8bd5\u4ee3\u7801\uff0c\u7ed3\u679c\u5982\u4e0b\u56fe\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-37-1024x296.png\" alt=\"\" class=\"wp-image-4267\" width=\"795\" height=\"229\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-37-1024x296.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-37-300x87.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-37-768x222.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-37-1536x443.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-37-830x240.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-37-230x66.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-37-350x101.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-37-480x139.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-37.png 1788w\" sizes=\"(max-width: 795px) 100vw, 795px\" \/><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">\u5168\u6ce8\u89e3\u65b9\u5f0f\u52a0\u8f7d Spring \u914d\u7f6e<\/h4>\n\n\n\n<p>Spring Boot \u63a8\u8350\u6211\u4eec\u4f7f\u7528\u5168\u6ce8\u89e3\u7684\u65b9\u5f0f\u52a0\u8f7d Spring \u914d\u7f6e\uff0c\u5176\u5b9e\u73b0\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol><li>\u4f7f\u7528 @Configuration \u6ce8\u89e3\u5b9a\u4e49\u914d\u7f6e\u7c7b\uff0c\u66ff\u6362 Spring \u7684\u914d\u7f6e\u6587\u4ef6\uff1b<\/li><li>\u914d\u7f6e\u7c7b\u5185\u90e8\u53ef\u4ee5\u5305\u542b\u6709\u4e00\u4e2a\u6216\u591a\u4e2a\u88ab @Bean \u6ce8\u89e3\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u4f1a\u88ab AnnotationConfigApplicationContext \u6216 AnnotationConfigWebApplicationContext \u7c7b\u626b\u63cf\uff0c\u6784\u5efa bean \u5b9a\u4e49\uff08\u76f8\u5f53\u4e8e Spring \u914d\u7f6e\u6587\u4ef6\u4e2d\u7684&lt;bean&gt;&lt;\/bean&gt;\u6807\u7b7e\uff09\uff0c\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u4f1a\u4ee5\u7ec4\u4ef6\u7684\u5f62\u5f0f\u6dfb\u52a0\u5230\u5bb9\u5668\u4e2d\uff0c\u7ec4\u4ef6\u7684 id \u5c31\u662f\u65b9\u6cd5\u540d\u3002<\/li><\/ol>\n\n\n\n<p>\u4ee5 helloworld \u4e3a\u4f8b\uff0c\u5220\u9664\u4e3b\u542f\u52a8\u7c7b\u7684 @ImportResource \u6ce8\u89e3\uff0c\u5728 net.biancheng.www.config \u5305\u4e0b\u6dfb\u52a0\u4e00\u4e2a\u540d\u4e3a\u00a0\u00a0MyAppConfig \u7684\u914d\u7f6e\u7c7b\uff0c\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package net.biancheng.www.config;\nimport net.biancheng.www.service.PersonService;\nimport net.biancheng.www.service.impl.PersonServiceImpl;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\n\/**\n* @Configuration \u6ce8\u89e3\u7528\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u914d\u7f6e\u7c7b\uff0c\u76f8\u5f53\u4e8e Spring \u7684\u914d\u7f6e\u6587\u4ef6\n* \u914d\u7f6e\u7c7b\u4e2d\u5305\u542b\u4e00\u4e2a\u6216\u591a\u4e2a\u88ab @Bean \u6ce8\u89e3\u7684\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u76f8\u5f53\u4e8e Spring \u914d\u7f6e\u6587\u4ef6\u4e2d\u7684 &lt;bean> \u6807\u7b7e\u5b9a\u4e49\u7684\u7ec4\u4ef6\u3002\n*\/\n@Configuration\npublic class MyAppConfig {\n    \/**\n     * \u4e0e &lt;bean id=\"personService\" class=\"PersonServiceImpl\">&lt;\/bean> \u7b49\u4ef7\n     * \u8be5\u65b9\u6cd5\u8fd4\u56de\u503c\u4ee5\u7ec4\u4ef6\u7684\u5f62\u5f0f\u6dfb\u52a0\u5230\u5bb9\u5668\u4e2d\n     * \u65b9\u6cd5\u540d\u662f\u7ec4\u4ef6 id\uff08\u76f8\u5f53\u4e8e &lt;bean> \u6807\u7b7e\u7684\u5c5e\u6027 id)\n     *\/\n    @Bean\n    public PersonService personService() {\n        System.out.println(\"\u5728\u5bb9\u5668\u4e2d\u6dfb\u52a0\u4e86\u4e00\u4e2a\u7ec4\u4ef6:peronService\");\n        return new PersonServiceImpl();\n    }\n}<\/pre>\n\n\n\n<p>\u6267\u884c\u6d4b\u8bd5\u4ee3\u7801\uff0c\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-38-1024x302.png\" alt=\"\" class=\"wp-image-4268\" width=\"599\" height=\"176\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-38-1024x302.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-38-300x89.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-38-768x227.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-38-1536x454.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-38-830x245.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-38-230x68.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-38-350x103.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-38-480x142.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-38.png 1774w\" sizes=\"(max-width: 599px) 100vw, 599px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cSpring Boot \u4e2d\u662f\u4e0d\u5305\u542b\u4efb\u4f55\u7684 Spring \u914d\u7f6e\u6587\u4ef6\u7684\uff0c\u5373\u4f7f\u6211\u4eec\u624b\u52a8\u6dfb\u52a0 Spring [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4265"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4265"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4265\/revisions"}],"predecessor-version":[{"id":4269,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4265\/revisions\/4269"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}