{"id":16779,"date":"2024-12-17T08:58:03","date_gmt":"2024-12-17T00:58:03","guid":{"rendered":"https:\/\/92it.top\/?p=16779"},"modified":"2024-12-17T08:58:03","modified_gmt":"2024-12-17T00:58:03","slug":"springboot%e4%b8%adapplication-properties%e5%92%8capplication-yml%e6%b7%b1%e5%ba%a6%e5%af%b9%e6%af%94%ef%bc%81","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=16779","title":{"rendered":"SpringBoot\u4e2dapplication.properties\u548capplication.yml\u6df1\u5ea6\u5bf9\u6bd4\uff01"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/mp.weixin.qq.com\/s?__biz=MzIwNDAyOTI2Nw==&amp;mid=2247500970&amp;idx=1&amp;sn=24b45e306c940be0cd380dadf8062a3b&amp;chksm=96c4e896a1b36180c7d437f9315644335b904aeff58746a2ca449ef3ba19a5498740561087a1&amp;cur_album_id=2818595943724974083&amp;scene=190#rd\">SpringBoot\u4e2dapplication.properties\u548capplication.yml\u6df1\u5ea6\u5bf9\u6bd4\uff01<\/a><\/p>\n\n\n\n<p>\u5728 Spring Boot \u4e2d\uff0c<code>application.properties<\/code> \u548c <code>application.yml<\/code>\uff08\u6216 <code>application.yaml<\/code>\uff09\u90fd\u662f\u7528\u4e8e\u914d\u7f6e\u5e94\u7528\u7a0b\u5e8f\u5c5e\u6027\u7684\u6587\u4ef6\u3002\u5b83\u4eec\u5728\u529f\u80fd\u4e0a\u76f8\u4f3c\uff0c\u4f46\u5728\u8bed\u6cd5\u548c\u53ef\u8bfb\u6027\u7b49\u65b9\u9762\u5b58\u5728\u4e00\u4e9b\u533a\u522b\u3002\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u591a\u7ef4\u5ea6\u6765\u6bd4\u8f83\u5b83\u4eec\u7684\u4e3b\u8981\u533a\u522b\u548c\u5404\u81ea\u7684\u7279\u70b9\u3002<\/p>\n\n\n\n<p><strong>1. \u914d\u7f6e\u683c\u5f0f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>1.1 <code>application.properties<\/code><\/strong><\/p>\n\n\n\n<ul>\n<li>\u4f7f\u7528\u7b80\u5355\u7684\u952e\u503c\u5bf9\u683c\u5f0f\uff0c\u6bcf\u4e2a\u5c5e\u6027\u4ee5 <code>key=value<\/code> \u7684\u5f62\u5f0f\u5b9a\u4e49\u3002<\/li>\n\n\n\n<li>\u9002\u5408\u914d\u7f6e\u7b80\u5355\u6216\u6241\u5e73\u5316\u7684\u5c5e\u6027\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u4e0b\u793a\u4f8b\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=\"\">server.port=8080\nspring.datasource.url=jdbc:mysql:\/\/localhost:3306\/mydb\nlogging.level.org.springframework=INFO<\/pre>\n\n\n\n<p><strong>1.2 <code>application.yml<\/code><\/strong><\/p>\n\n\n\n<ul>\n<li>\u4f7f\u7528 YAML\uff08YAML Ain&#8217;t Markup Language\uff09\u683c\u5f0f\uff0c\u652f\u6301\u5d4c\u5957\u548c\u5c42\u6b21\u5316\u7684\u914d\u7f6e\u3002<\/li>\n\n\n\n<li>\u66f4\u9002\u5408\u5904\u7406\u590d\u6742\u6216\u6709\u5c42\u7ea7\u7ed3\u6784\u7684\u914d\u7f6e\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5982\u4e0b\u793a\u4f8b\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=\"\">server:\n  port: 8080\n\nspring:\n  datasource:\n    url: jdbc:mysql:\/\/localhost:3306\/mydb\n\nlogging:\n  level:\n    org:\n      springframework: INFO<\/pre>\n\n\n\n<p><strong>2. \u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>2.1 <code>application.yml<\/code><\/strong><\/p>\n\n\n\n<ul>\n<li>\u5bf9\u4e8e\u590d\u6742\u7684\u914d\u7f6e\uff0cYAML \u7684\u5c42\u7ea7\u7ed3\u6784\u4f7f\u914d\u7f6e\u66f4\u52a0\u6e05\u6670\u548c\u6613\u4e8e\u7ef4\u62a4\u3002<\/li>\n\n\n\n<li>\u652f\u6301\u5217\u8868\u548c\u5d4c\u5957\u5bf9\u8c61\u7684\u8868\u793a\u66f4\u52a0\u76f4\u89c2\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=\"\">app:\n  datasource:\n    urls:\n      - jdbc:mysql:\/\/localhost:3306\/db1\n      - jdbc:mysql:\/\/localhost:3306\/db2\n    username: user\n    password: pass<\/pre>\n\n\n\n<p><strong>2.2 <code>application.properties<\/code><\/strong><\/p>\n\n\n\n<ul>\n<li>\u5904\u7406\u5d4c\u5957\u914d\u7f6e\u65f6\u9700\u8981\u4f7f\u7528\u70b9\uff08<code>.<\/code>\uff09\u5206\u9694\u7b26\uff0c\u53ef\u80fd\u5bfc\u81f4\u914d\u7f6e\u6587\u4ef6\u8f83\u957f\u4e14\u4e0d\u6613\u9605\u8bfb\u3002<\/li>\n\n\n\n<li>\u5217\u8868\u9700\u8981\u4f7f\u7528\u7d22\u5f15\u6216\u9017\u53f7\u5206\u9694\uff0c\u4f1a\u663e\u5f97\u4e0d\u591f\u76f4\u89c2\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=\"\">app.datasource.urls[0]=jdbc:mysql:\/\/localhost:3306\/db1\napp.datasource.urls[1]=jdbc:mysql:\/\/localhost:3306\/db2\napp.datasource.username=user\napp.datasource.password=pass<\/pre>\n\n\n\n<p><strong>3. \u529f\u80fd\u548c\u7279\u6027<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>\u4e24\u8005\u529f\u80fd\u76f8\u540c<\/strong>: \u90fd\u652f\u6301 Spring Boot \u7684\u6240\u6709\u914d\u7f6e\u529f\u80fd\uff0c\u5982\u5c5e\u6027\u5360\u4f4d\u7b26\u3001\u914d\u7f6e\u6587\u4ef6\u5206\u7247\uff08\u5982 <code>application-dev.properties<\/code>\uff09\u3001Profile-specific \u914d\u7f6e\u7b49\u3002<\/p>\n\n\n\n<p><strong>YAML \u7684\u9644\u52a0\u4f18\u52bf<\/strong>: \u652f\u6301\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u5982\u591a\u5c42\u5d4c\u5957\u3001\u5217\u8868\u7b49\uff0c\u9002\u5408\u914d\u7f6e\u8f83\u4e3a\u590d\u6742\u7684\u5e94\u7528\u3002\u66f4\u6613\u4e8e\u8868\u8fbe\u73af\u5883\u914d\u7f6e\u548c\u591a\u6a21\u5757\u914d\u7f6e\u3002<\/p>\n\n\n\n<p><strong>4. \u9009\u62e9\u5efa\u8bae<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><code>application.properties<\/code>:<\/p>\n\n\n\n<ul>\n<li>\u9002\u5408\u914d\u7f6e\u8f83\u4e3a\u7b80\u5355\u6216\u504f\u597d\u952e\u503c\u5bf9\u5f62\u5f0f\u7684\u5f00\u53d1\u8005\u3002<\/li>\n\n\n\n<li>\u5bf9\u4e8e\u5c0f\u578b\u9879\u76ee\u6216\u7b80\u5355\u914d\u7f6e\uff0c<code>properties<\/code> \u6587\u4ef6\u66f4\u7b80\u5355\u76f4\u63a5\u3002<\/li>\n<\/ul>\n\n\n\n<p><code>application.yml<\/code>:<\/p>\n\n\n\n<ul>\n<li>\u66f4\u9002\u5408\u590d\u6742\u914d\u7f6e\u6216\u9700\u8981\u5c42\u6b21\u5316\u8868\u793a\u7684\u573a\u666f\u3002<\/li>\n\n\n\n<li>\u63d0\u9ad8\u914d\u7f6e\u6587\u4ef6\u7684\u53ef\u8bfb\u6027\uff0c\u5c24\u5176\u662f\u5f53\u914d\u7f6e\u9879\u8f83\u591a\u65f6\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>5. IDE \u652f\u6301\u548c\u9519\u8bef\u68c0\u67e5<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<ul>\n<li><strong>YAML \u7279\u6027<\/strong>: \u73b0\u4ee3 IDE\uff08\u5982 IntelliJ IDEA\u3001VS Code\uff09\u5bf9 YAML \u8bed\u6cd5\u6709\u8f83\u597d\u7684\u652f\u6301\uff0c\u5305\u62ec\u8bed\u6cd5\u9ad8\u4eae\u3001\u81ea\u52a8\u8865\u5168\u548c\u9519\u8bef\u68c0\u67e5\u3002<\/li>\n\n\n\n<li><strong>Properties \u6587\u4ef6<\/strong>: \u4e5f\u53d7\u5230\u5e7f\u6cdb\u652f\u6301\uff0c\u4f46\u5728\u5904\u7406\u5c42\u6b21\u5316\u914d\u7f6e\u65f6\uff0c\u4e0d\u5982 YAML \u76f4\u89c2\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>6. \u7efc\u5408\u8003\u91cf<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<ul>\n<li><strong>\u4e2a\u4eba\u504f\u597d<\/strong>: \u6700\u7ec8\u9009\u62e9 <code>application.properties<\/code> \u8fd8\u662f <code>application.yml<\/code> \u53d6\u51b3\u4e8e\u4e2a\u4eba\u6216\u56e2\u961f\u7684\u504f\u597d\u3002<\/li>\n\n\n\n<li><strong>\u56e2\u961f\u4e00\u81f4\u6027<\/strong>: \u4fdd\u6301\u56e2\u961f\u5185\u90e8\u914d\u7f6e\u6587\u4ef6\u683c\u5f0f\u7684\u4e00\u81f4\u6027\uff0c\u4fbf\u4e8e\u534f\u4f5c\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>7. \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\u901a\u8fc7\u591a\u4e2a\u7ef4\u5ea6\u5bf9<code>application.properties<\/code> \u548c <code>application.yml<\/code>\u8fdb\u884c\u4e86\u5bf9\u6bd4\uff0c\u4e24\u8005\u90fd\u662f SpringBoot\u4e2d\u5f3a\u5927\u7684\u914d\u7f6e\u5de5\u5177\uff0c\u9009\u62e9\u54ea\u4e00\u4e2a\u4e3b\u8981\u53d6\u51b3\u4e8e\u9879\u76ee\u7684\u590d\u6742\u6027\u548c\u56e2\u961f\u7684\u504f\u597d\u3002\u5bf9\u4e8e\u7b80\u5355\u548c\u6241\u5e73\u5316\u7684\u914d\u7f6e\uff0c<code>application.properties<\/code> \u8db3\u591f\u4f7f\u7528\uff1b\u800c\u5bf9\u4e8e\u590d\u6742\u3001\u5c42\u6b21\u5316\u7684\u914d\u7f6e\uff0c<code>application.yml<\/code> \u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u53ef\u8bfb\u6027\u548c\u7ec4\u7ec7\u65b9\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1aSpringBoot\u4e2dapplication.properties\u548capplication.yml\u6df1\u5ea6\u5bf9 [&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\/16779"}],"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=16779"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16779\/revisions"}],"predecessor-version":[{"id":16780,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/16779\/revisions\/16780"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=16779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=16779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=16779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}