{"id":16997,"date":"2025-03-26T20:14:11","date_gmt":"2025-03-26T12:14:11","guid":{"rendered":"https:\/\/92it.top\/?p=16997"},"modified":"2025-03-26T20:14:12","modified_gmt":"2025-03-26T12:14:12","slug":"spring%e7%9a%84%e5%90%af%e5%8a%a8%e6%b5%81%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=16997","title":{"rendered":"Spring\u7684\u542f\u52a8\u6d41\u7a0b"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzIwNDAyOTI2Nw==&amp;mid=2247503327&amp;idx=1&amp;sn=6a0cdb7a2ff9e033fa4fcc4cf2d5c0a5&amp;chksm=96c4e1e3a1b368f5200624ff6c41e9d330897fc271de4699af0dc32fb503fc6b71be260fc37e&amp;cur_album_id=2818595943724974083&amp;scene=190#rd\">Spring\u7684\u542f\u52a8\u6d41\u7a0b\uff0c90%\u7684Java\u7a0b\u5e8f\u5458\u90fd\u8bf4\u4e0d\u51fa\u6765\uff01<\/a><\/p>\n\n\n\n<p>\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u6765\u804a\u804a Spring\u6846\u67b6\u7684\u542f\u52a8\u8fc7\u7a0b\uff0c\u672c\u6587\u4f1a\u9010\u6b65\u62c6\u89e3 Spring\u7684\u542f\u52a8\u6d41\u7a0b\uff0c\u5206\u6790\u5176\u80cc\u540e\u7684\u539f\u7406\uff0c\u5e76\u901a\u8fc7\u5b9e\u4f8b\u6765\u52a0\u6df1\u7406\u89e3\u3002\u6700\u540e\uff0c\u8fd8\u4f1a\u8fdb\u884c\u4e00\u4e2a\u5c0f\u7ed3\uff0c\u5e2e\u52a9\u5927\u5bb6\u68b3\u7406\u5173\u952e\u70b9\u3002<\/p>\n\n\n\n<p><strong>1. Spring\u542f\u52a8\u7684\u6574\u4f53\u6d41\u7a0b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5bf9\u4e8e Spring\u5e94\u7528\u7a0b\u5e8f\u7684\u542f\u52a8\u8fc7\u7a0b\uff0c\u6211\u4eec\u53ef\u4ee5\u603b\u7ed3\u6210\u4ee5\u4e0b 6\u4e2a\u4e3b\u8981\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<ol>\n<li><strong>\u5f15\u5bfc\u9636\u6bb5<\/strong>\uff1a\u542f\u52a8\u5668\uff08\u5982Spring Boot\u7684<code>SpringApplication<\/code>\uff09\u88ab\u8c03\u7528\uff0c\u521d\u59cb\u5316\u5e94\u7528\u4e0a\u4e0b\u6587\u3002<\/li>\n\n\n\n<li><strong>\u73af\u5883\u51c6\u5907<\/strong>\uff1a\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\uff0c\u8bbe\u7f6e\u73af\u5883\u5c5e\u6027\u3002<\/li>\n\n\n\n<li><strong>\u521b\u5efa\u5e94\u7528\u4e0a\u4e0b\u6587<\/strong>\uff1a\u6839\u636e\u914d\u7f6e\u521b\u5efa\u9002\u5f53\u7684<code>ApplicationContext<\/code>\uff08\u5982<code>AnnotationConfigApplicationContext<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u6ce8\u518cBean\u5b9a\u4e49<\/strong>\uff1a\u626b\u63cf\u5305\u8def\u5f84\uff0c\u627e\u5230\u5e76\u6ce8\u518c\u6240\u6709\u7684Bean\u5b9a\u4e49\u3002<\/li>\n\n\n\n<li><strong>\u5237\u65b0\u4e0a\u4e0b\u6587<\/strong>\uff1a\u6267\u884cBean\u5b9e\u4f8b\u5316\u3001\u4f9d\u8d56\u6ce8\u5165\u3001\u521d\u59cb\u5316\u7b49\u6b65\u9aa4\u3002<\/li>\n\n\n\n<li><strong>\u542f\u52a8\u5b8c\u6210<\/strong>\uff1a\u5e94\u7528\u7a0b\u5e8f\u6b63\u5f0f\u542f\u52a8\uff0c\u53ef\u4ee5\u63a5\u53d7\u8bf7\u6c42\u6216\u6267\u884c\u4efb\u52a1\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u9010\u6b65\u6df1\u5165\u6bcf\u4e00\u4e2a\u9636\u6bb5\u3002<\/p>\n\n\n\n<p><strong>2. \u5173\u952e\u6b65\u9aa4\u8be6\u89e3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>1. \u5f15\u5bfc\u9636\u6bb5<\/strong><\/p>\n\n\n\n<p>\u5728Spring Boot\u9879\u76ee\u4e2d\uff0c\u542f\u52a8\u901a\u5e38\u4ece\u4e00\u4e2a\u4e3b\u7c7b\u7684<code>main<\/code>\u65b9\u6cd5\u5f00\u59cb\uff0c\u4f8b\u5982\uff1a<\/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=\"\">@SpringBootApplication\npublic class DemoApplication {\n    public static void main(String[] args) {\n        SpringApplication.run(DemoApplication.class, args);\n    }\n}\n<\/pre>\n\n\n\n<p>\u8fd9\u91cc\uff0c<code>SpringApplication.run()<\/code>\u65b9\u6cd5\u8d1f\u8d23\u5f15\u5bfc\u6574\u4e2a Spring\u5e94\u7528\u7684\u542f\u52a8\u8fc7\u7a0b\u3002\u5b83\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u521b\u5efa\u4e00\u4e2a\u5408\u9002\u7684<code>ApplicationContext<\/code>\uff0c\u51c6\u5907\u73af\u5883\uff0c\u5e76\u5237\u65b0\u4e0a\u4e0b\u6587\u4ee5\u5b8c\u6210\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<p><strong>2. \u73af\u5883\u51c6\u5907<\/strong><\/p>\n\n\n\n<p>Spring\u5e94\u7528\u542f\u52a8\u65f6\uff0c\u9700\u8981\u52a0\u8f7d\u5404\u79cd\u914d\u7f6e\uff0c\u4f8b\u5982<code>application.properties<\/code>\u6216<code>application.yml<\/code>\u6587\u4ef6\u3002\u8fd9\u4e9b\u914d\u7f6e\u51b3\u5b9a\u4e86\u5e94\u7528\u7684\u884c\u4e3a\u548c\u5404\u79cd\u53c2\u6570\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=\"\">ConfigurableApplicationContext context = new SpringApplicationBuilder(DemoApplication.class)\n    .properties(\"spring.config.name=application\")\n    .run(args);\n<\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u9636\u6bb5\uff0cSpring\u4f1a\u6839\u636e\u73af\u5883\uff08\u5982\u5f00\u53d1\u3001\u6d4b\u8bd5\u3001\u751f\u4ea7\uff09\u52a0\u8f7d\u76f8\u5e94\u7684\u914d\u7f6e\uff0c\u5e76\u8bbe\u7f6e\u7cfb\u7edf\u5c5e\u6027\u548c\u73af\u5883\u53d8\u91cf\u3002<\/p>\n\n\n\n<p><strong>3. \u521b\u5efa\u5e94\u7528\u4e0a\u4e0b\u6587<\/strong><\/p>\n\n\n\n<p><code>ApplicationContext<\/code>\u662fSpring\u7684\u6838\u5fc3\u5bb9\u5668\uff0c\u8d1f\u8d23\u7ba1\u7406Bean\u7684\u751f\u547d\u5468\u671f\u548c\u4f9d\u8d56\u5173\u7cfb\u3002\u6839\u636e\u4e0d\u540c\u7684\u9879\u76ee\u9700\u6c42\uff0cSpring\u53ef\u4ee5\u521b\u5efa\u4e0d\u540c\u7c7b\u578b\u7684\u4e0a\u4e0b\u6587\uff0c\u5982<code>AnnotationConfigApplicationContext<\/code>\u7528\u4e8e\u57fa\u4e8e\u6ce8\u89e3\u7684\u914d\u7f6e\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=\"\">AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();\ncontext.register(DemoApplication.class);\n<\/pre>\n\n\n\n<p><strong>4. \u6ce8\u518cBean\u5b9a\u4e49<\/strong><\/p>\n\n\n\n<p>\u8fd9\u4e00\u6b65\u9aa4\u6d89\u53ca\u5230\u626b\u63cf\u6307\u5b9a\u7684\u5305\u8def\u5f84\uff0c\u67e5\u627e\u6807\u6ce8\u4e86<code>@Component<\/code>\u3001<code>@Service<\/code>\u3001<code>@Repository<\/code>\u7b49\u6ce8\u89e3\u7684\u7c7b\uff0c\u5e76\u5c06\u5b83\u4eec\u6ce8\u518c\u4e3aBean\u3002<\/p>\n\n\n\n<p>\u4f8b\u5982\uff1a<\/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=\"\">@Component\npublic class MyService {\n    \/\/ ...\n}\n<\/pre>\n\n\n\n<p>Spring\u901a\u8fc7<code>ClassPathBeanDefinitionScanner<\/code>\u6765\u626b\u63cf\u8fd9\u4e9b\u7c7b\uff0c\u5e76\u5c06\u5b83\u4eec\u7684\u5b9a\u4e49\u4fdd\u5b58\u5230<code>BeanFactory<\/code>\u4e2d\uff0c\u4ee5\u5907\u540e\u7eed\u5b9e\u4f8b\u5316\u3002<\/p>\n\n\n\n<p><strong>5. \u5237\u65b0\u4e0a\u4e0b\u6587<\/strong><\/p>\n\n\n\n<p>\u5237\u65b0\u4e0a\u4e0b\u6587\u662f\u6574\u4e2a\u542f\u52a8\u8fc7\u7a0b\u7684\u5173\u952e\uff0c\u6d89\u53ca\u5230Bean\u7684\u5b9e\u4f8b\u5316\u3001\u4f9d\u8d56\u6ce8\u5165\u3001\u521d\u59cb\u5316\u7b49\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p>\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>\u5b9e\u4f8b\u5316Bean<\/strong>\uff1a\u6839\u636eBean\u5b9a\u4e49\u521b\u5efaBean\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li><strong>\u4f9d\u8d56\u6ce8\u5165<\/strong>\uff1a\u5c06\u9700\u8981\u7684\u4f9d\u8d56\u81ea\u52a8\u6ce8\u5165\u5230Bean\u4e2d\u3002<\/li>\n\n\n\n<li><strong>\u521d\u59cb\u5316Bean<\/strong>\uff1a\u6267\u884cBean\u7684\u521d\u59cb\u5316\u65b9\u6cd5\uff0c\u5982\u5b9e\u73b0<code>InitializingBean<\/code>\u63a5\u53e3\u7684<code>afterPropertiesSet()<\/code>\u65b9\u6cd5\uff0c\u6216\u901a\u8fc7<code>@PostConstruct<\/code>\u6ce8\u89e3\u7684\u65b9\u6cd5\u3002<\/li>\n\n\n\n<li><strong>\u5904\u7406Bean\u540e\u7f6e\u5904\u7406\u5668<\/strong>\uff1a\u6267\u884c<code>BeanPostProcessor<\/code>\u63a5\u53e3\u7684\u76f8\u5173\u65b9\u6cd5\uff0c\u5141\u8bb8\u5f00\u53d1\u8005\u81ea\u5b9a\u4e49Bean\u7684\u5904\u7406\u903b\u8f91\u3002<\/li>\n<\/ul>\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=\"\">context.refresh();\n<\/pre>\n\n\n\n<p><strong>6. \u542f\u52a8\u5b8c\u6210<\/strong><\/p>\n\n\n\n<p>\u4e00\u65e6\u5e94\u7528\u4e0a\u4e0b\u6587\u5237\u65b0\u5b8c\u6210\uff0c\u6240\u6709\u7684Bean\u90fd\u5df2\u7ecf\u521d\u59cb\u5316\u597d\u4e86\uff0cSpring\u5e94\u7528\u5c31\u6b63\u5f0f\u542f\u52a8\u5b8c\u6210\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u5f00\u59cb\u63a5\u6536HTTP\u8bf7\u6c42\u3001\u6267\u884c\u5b9a\u65f6\u4efb\u52a1\u7b49\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p><strong>3.\u793a\u4f8b\u6f14\u793a<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4e0b\u9762\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u6765\u5b9e\u9645\u770b\u770b Spring\u662f\u5982\u4f55\u542f\u52a8\u548c\u7ba1\u7406 Bean\u7684\u3002<\/p>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u670d\u52a1\u7c7b\u548c\u63a7\u5236\u5668\uff1a<\/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=\"\">@Component\npublicclass GreetingService {\n    public String greet() {\n        return\"Hello, Spring!\";\n    }\n}\n\n@RestController\npublicclass GreetingController {\n    privatefinal GreetingService greetingService;\n\n    @Autowired\n    public GreetingController(GreetingService greetingService) {\n        this.greetingService = greetingService;\n    }\n\n    @GetMapping(\"\/greet\")\n    public String greet() {\n        return greetingService.greet();\n    }\n}\n<\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff1a<\/p>\n\n\n\n<ol>\n<li><code>GreetingService<\/code>\u88ab\u6807\u6ce8\u4e3a<code>@Component<\/code>\uff0c\u4f1a\u88abSpring\u626b\u63cf\u5e76\u6ce8\u518c\u4e3a\u4e00\u4e2aBean\u3002<\/li>\n\n\n\n<li><code>GreetingController<\/code>\u901a\u8fc7\u6784\u9020\u5668\u6ce8\u5165\u4e86<code>GreetingService<\/code>\uff0c\u8fd9\u5c31\u662f\u4f9d\u8d56\u6ce8\u5165\u7684\u4f53\u73b0\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u5f53Spring\u542f\u52a8\u65f6\uff0c\u5b83\u4f1a\uff1a<\/p>\n\n\n\n<ul>\n<li>\u626b\u63cf\u5230<code>GreetingService<\/code>\u5e76\u521b\u5efa\u5176\u5b9e\u4f8b\u3002<\/li>\n\n\n\n<li>\u521b\u5efa<code>GreetingController<\/code>\uff0c\u5e76\u5c06<code>GreetingService<\/code>\u7684\u5b9e\u4f8b\u6ce8\u5165\u5230\u63a7\u5236\u5668\u4e2d\u3002<\/li>\n\n\n\n<li>\u5b8c\u6210\u6240\u6709\u521d\u59cb\u5316\u6b65\u9aa4\u540e\uff0c\u5e94\u7528\u5c31\u53ef\u4ee5\u901a\u8fc7<code>\/greet<\/code>\u7aef\u70b9\u8fd4\u56de\u95ee\u5019\u8bed\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>4. \u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u672c\u6587\uff0c\u6211\u4eec\u5206\u6790\u4e86 Spring\u542f\u52a8\u7684\u6d41\u7a0b\uff0c\u901a\u8fc7\u4ee5\u4e0a\u7684\u5206\u6790\u548c\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230Spring\u7684\u542f\u52a8\u8fc7\u7a0b\u662f\u4e00\u4e2a\u76f8\u5bf9\u590d\u6742\u4f46\u9ad8\u5ea6\u81ea\u52a8\u5316\u7684\u8fc7\u7a0b\u3002\u5176\u4e3b\u8981\u6b65\u9aa4\u5305\u62ec\u5f15\u5bfc\u9636\u6bb5\u3001\u73af\u5883\u51c6\u5907\u3001\u521b\u5efa\u5e94\u7528\u4e0a\u4e0b\u6587\u3001\u6ce8\u518c Bean\u5b9a\u4e49\u3001\u5237\u65b0\u4e0a\u4e0b\u6587\u4ee5\u53ca\u6700\u7ec8\u7684\u542f\u52a8\u5b8c\u6210\u3002\u6bcf\u4e00\u4e2a\u6b65\u9aa4\u90fd\u6d89\u53ca\u5230\u5927\u91cf\u7684\u5e95\u5c42\u673a\u5236\uff0c\u4f8b\u5982 Bean\u7684\u5b9e\u4f8b\u5316\u3001\u4f9d\u8d56\u6ce8\u5165\u3001\u540e\u7f6e\u5904\u7406\u5668\u7b49\uff0c\u8fd9\u4e9b\u90fd\u662f Spring\u80fd\u591f\u9ad8\u6548\u7ba1\u7406\u5e94\u7528\u7684\u5173\u952e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1aSpring\u7684\u542f\u52a8\u6d41\u7a0b\uff0c90%\u7684Java\u7a0b\u5e8f\u5458\u90fd\u8bf4\u4e0d\u51fa\u6765\uff01 \u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u6765\u804a\u804a Spring\u6846\u67b6\u7684\u542f\u52a8 [&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\/16997"}],"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=16997"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16997\/revisions"}],"predecessor-version":[{"id":16998,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16997\/revisions\/16998"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16997"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16997"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16997"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}