{"id":10006877,"date":"2022-11-21T10:59:46","date_gmt":"2022-11-21T02:59:46","guid":{"rendered":"https:\/\/ityet.com\/?p=10006877"},"modified":"2022-11-21T10:59:46","modified_gmt":"2022-11-21T02:59:46","slug":"spring-boot-%e5%88%86%e7%a6%bb%e9%85%8d%e7%bd%ae%e6%96%87%e4%bb%b6%e7%9a%84-n-%e7%a7%8d%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006877","title":{"rendered":"Spring Boot \u5206\u79bb\u914d\u7f6e\u6587\u4ef6\u7684 N \u79cd\u65b9\u5f0f"},"content":{"rendered":"<p>\u4eca\u5929\u804a\u4e00\u4e2a\u5c0f\u4f19\u4f34\u5728\u661f\u7403\u4e0a\u7684\u63d0\u95ee\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042855046\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u95ee\u9898\u4e0d\u96be\uff0c\u89e3\u51b3\u65b9\u6848\u4e5f\u6709\u5f88\u591a\uff0c\u56e0\u6b64\u6211\u51b3\u5b9a\u64b8\u4e00\u7bc7\u6587\u7ae0\u548c\u5927\u5bb6\u4ed4\u7ec6\u8bf4\u8bf4\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<h2>1. \u914d\u7f6e\u6587\u4ef6\u4f4d\u7f6e<\/h2>\n<p>\u9996\u5148\u5c0f\u4f19\u4f34\u4eec\u8981\u660e\u767d\uff0cSpring Boot \u9ed8\u8ba4\u52a0\u8f7d\u7684\u914d\u7f6e\u6587\u4ef6\u662f application.properties \u6216\u8005 application.yaml\uff0c\u9ed8\u8ba4\u7684\u52a0\u8f7d\u4f4d\u7f6e\u4e00\u5171\u6709\u4e94\u4e2a\uff0c\u4e94\u4e2a\u4f4d\u7f6e\u53ef\u4ee5\u5206\u4e3a\u4e24\u7c7b\uff1a<\/p>\n<p><strong>\u4ece classpath \u4e0b\u52a0\u8f7d<\/strong>\uff0c\u8fd9\u4e2a\u53c8\u7ec6\u5206\u4e3a\u4e24\u79cd\uff1a<\/p>\n<ol>\n<li>\u76f4\u63a5\u8bfb\u53d6 classpath \u4e0b\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u5bf9\u5e94\u5230 Spring Boot \u9879\u76ee\u4e2d\uff0c\u5c31\u662f resources \u76ee\u5f55\u4e0b\u7684\u914d\u7f6e\u3002<\/li>\n<li>\u8bfb\u53d6 classpath:\/config\/ \u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\uff0c\u5bf9\u5e94\u5230 Spring Boot \u9879\u76ee\u4e2d\u5c31\u662f resources\/config \u76ee\u5f55\u4e0b\u7684\u914d\u7f6e\u3002<\/li>\n<\/ol>\n<p>\u8fd9\u4e24\u79cd\u60c5\u51b5\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042855047\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>\u4ece\u9879\u76ee\u6240\u5728\u7684\u5f53\u524d\u76ee\u5f55\u4e0b\u52a0\u8f7d<\/strong>\uff0c\u8fd9\u4e2a\u53c8\u7ec6\u5206\u4e3a\u4e09\u79cd\u60c5\u51b5\uff1a<\/p>\n<ol>\n<li>\u4ece\u9879\u76ee\u5f53\u524d\u76ee\u5f55\u4e0b\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n<li>\u4ece\u9879\u76ee\u5f53\u524d\u76ee\u5f55\u4e0b\u7684 config \u6587\u4ef6\u5939\u4e2d\u52a0\u8f7d\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n<li>\u4ece\u9879\u76ee\u5f53\u524d\u76ee\u5f55\u4e0b\u7684 config \u6587\u4ef6\u5939\u7684\u5b50\u6587\u4ef6\u5939\u4e2d\u52a0\u8f7d\uff08\u5b59\u5b50\u6587\u4ef6\u5939\u4e0d\u53ef\u4ee5\uff09\u3002<\/li>\n<\/ol>\n<p>\u8fd9\u4e09\u79cd\u60c5\u51b5\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042855048\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<blockquote><p>config \u76ee\u5f55\u4e0b\u7684\u914d\u7f6e\u6587\u4ef6\u53ef\u4ee5\u88ab\u52a0\u8f7d\uff0cconfig\/a \u76ee\u5f55\u4e0b\u7684\u914d\u7f6e\u6587\u4ef6\u4e5f\u53ef\u4ee5\u88ab\u52a0\u8f7d\uff0c\u4f46\u662f config\/a\/b \u76ee\u5f55\u4e0b\u7684\u914d\u7f6e\u6587\u4ef6\u4e0d\u4f1a\u88ab\u52a0\u8f7d\uff0c\u56e0\u4e3a\u4e0d\u662f\u76f4\u63a5\u5b50\u6587\u4ef6\u5939\u3002<\/p><\/blockquote>\n<p>\u914d\u7f6e\u6587\u4ef6\u53ef\u4ee5\u653e\u5728\u8fd9\u4e48\u591a\u4e0d\u540c\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u540c\u4e00\u4e2a\u5c5e\u6027\u5728\u591a\u4e2a\u914d\u7f6e\u6587\u4ef6\u4e2d\u90fd\u5199\u4e86\uff0c\u90a3\u4e48\u540e\u9762\u52a0\u8f7d\u7684\u914d\u7f6e\u4f1a\u8986\u76d6\u6389\u524d\u9762\u7684\u3002\u4f8b\u5982\u5728 <code>classpath:application.yaml<\/code> \u4e2d\u8bbe\u7f6e\u9879\u76ee\u7aef\u53e3\u53f7\u662f 8080\uff0c\u5728 <code>\u9879\u76ee\u5f53\u524d\u76ee\u5f55\/config\/a\/application.yaml<\/code> \u4e2d\u8bbe\u7f6e\u9879\u76ee\u7aef\u53e3\u662f 8081\uff0c\u90a3\u4e48\u6700\u7ec8\u7684\u9879\u76ee\u7aef\u53e3\u53f7\u5c31\u662f 8081\u3002<\/p>\n<p>\u8fd9\u662f\u9ed8\u8ba4\u7684\u6587\u4ef6\u4f4d\u7f6e\u3002<\/p>\n<p>\u5982\u679c\u4f60\u4e0d\u60f3\u8ba9\u81ea\u5df1\u7684\u914d\u7f6e\u6587\u4ef6\u53eb application.properties \u6216\u8005 application.yaml\uff0c\u90a3\u4e48\u4e5f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u914d\u7f6e\u6587\u4ef6\u540d\u79f0\uff0c\u53ea\u9700\u8981\u5728\u9879\u76ee\u542f\u52a8\u7684\u65f6\u5019\u6307\u5b9a\u914d\u7f6e\u6587\u4ef6\u540d\u5373\u53ef\uff0c\u4f8b\u5982\u6211\u60f3\u8bbe\u7f6e\u6211\u7684\u914d\u7f6e\u6587\u4ef6\u540d\u4e3a app.yaml\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u5728\u542f\u52a8 jar \u5305\u7684\u65f6\u5019\u6309\u7167\u5982\u4e0b\u65b9\u5f0f\u914d\u7f6e\uff0c\u6b64\u65f6\u7cfb\u7edf\u4f1a\u81ea\u52a8\u53bb\u4e0a\u9762\u63d0\u5230\u7684\u4e94\u4e2a\u4f4d\u7f6e\u67e5\u627e\u5bf9\u5e94\u7684\u914d\u7f6e\u6587\u4ef6\uff1a<\/p>\n<pre><code>java -jar boot_config_file-0.0.1-SNAPSHOT.jar --spring.config.name=app<\/code><\/pre>\n<blockquote><p>\u5982\u679c\u9879\u76ee\u5df2\u7ecf\u6253\u6210 jar \u5305\u542f\u52a8\u4e86\uff0c\u90a3\u4e48\u524d\u9762\u6240\u8bf4\u7684\u76ee\u5f55\u4e2d\uff0c\u540e\u4e09\u4e2a\u4e2d\u7684\u9879\u76ee\u5f53\u524d\u76ee\u5f55\u5c31\u662f\u6307 jar \u5305\u6240\u5728\u7684\u76ee\u5f55\u3002<\/p><\/blockquote>\n<p>\u5982\u679c\u4f60\u4e0d\u60f3\u53bb\u8fd9\u4e94\u4e2a\u4f4d\u7f6e\u67e5\u627e\uff0c\u90a3\u4e48\u4e5f\u53ef\u4ee5\u5728\u542f\u52a8 jar \u5305\u7684\u65f6\u5019\u660e\u786e\u6307\u5b9a\u914d\u7f6e\u6587\u4ef6\u7684\u4f4d\u7f6e\u548c\u540d\u79f0\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre><code>java -jar boot_config_file-0.0.1-SNAPSHOT.jar --spring.config.location=optional:classpath:\/app.yaml<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u6211\u5728 classpath \u524d\u9762\u52a0\u4e0a\u4e86 <code>optional:<\/code> \u8868\u793a\u5982\u679c\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u6309\u7167\u9ed8\u8ba4\u7684\u65b9\u5f0f\u542f\u52a8\uff0c\u800c\u4e0d\u4f1a\u62a5\u9519\u8bf4\u627e\u4e0d\u5230\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u3002\u5982\u679c\u4e0d\u52a0\u8fd9\u4e2a\u524d\u7f00\uff0c\u90a3\u4e48\u5f53\u7cfb\u7edf\u627e\u4e0d\u5230\u6307\u5b9a\u7684\u914d\u7f6e\u6587\u4ef6\u65f6\uff0c\u5c31\u4f1a\u629b\u51fa <code>ConfigDataLocationNotFoundException<\/code> \u5f02\u5e38\uff0c\u8fdb\u800c\u5bfc\u81f4\u5e94\u7528\u542f\u52a8\u5931\u8d25\u3002<\/p>\n<p>\u5982\u679c\u914d\u7f6e\u6587\u4ef6\u548c jar \u5305\u5728\u76f8\u540c\u7684\u76ee\u5f55\u7ed3\u6784\u4e0b\uff0c\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042855049\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u90a3\u4e48\u542f\u52a8\u811a\u672c\u5982\u4e0b\uff1a<\/p>\n<pre><code>java -jar boot_config_file-0.0.1-SNAPSHOT.jar --spring.config.location=optional:javaboy\/app.yaml<\/code><\/pre>\n<p>\u5982\u679c spring.config.location \u7684\u914d\u7f6e\uff0c\u53ea\u662f\u6307\u5b9a\u4e86\u76ee\u5f55\uff0c\u90a3\u4e48\u5fc5\u987b\u4ee5 \/ \u7ed3\u5c3e\uff0c\u4f8b\u5982\u4e0a\u9762\u8fd9\u4e2a\u542f\u52a8\u811a\u672c\uff0c\u4e5f\u53ef\u4ee5\u6309\u7167\u5982\u4e0b\u65b9\u5f0f\u542f\u52a8\uff1a<\/p>\n<pre><code>java -jar boot_config_file-0.0.1-SNAPSHOT.jar --spring.config.location=optional:javaboy\/ --spring.config.name=app<\/code><\/pre>\n<p>\u901a\u8fc7 spring.config.location \u5c5e\u6027\u9501\u5b9a\u914d\u7f6e\u6587\u4ef6\u7684\u4f4d\u7f6e\uff0c\u901a\u8fc7 spring.config.name \u5c5e\u6027\u9501\u5b9a\u914d\u7f6e\u6587\u4ef6\u7684\u6587\u4ef6\u540d\u3002<\/p>\n<h2>2. \u989d\u5916\u4f4d\u7f6e<\/h2>\n<p>\u524d\u9762\u6211\u4eec\u5173\u4e8e\u914d\u7f6e\u6587\u4ef6\u4f4d\u7f6e\u7684\u8bbe\u7f6e\uff0c\u90fd\u662f\u8986\u76d6\u6389\u5df2\u6709\u7684\u914d\u7f6e\uff0c\u5982\u679c\u4e0d\u60f3\u8986\u76d6\u6389 Spring Boot \u9ed8\u8ba4\u7684\u914d\u7f6e\u6587\u4ef6\u67e5\u627e\u7b56\u7565\uff0c\u53c8\u60f3\u52a0\u5165\u81ea\u5df1\u7684\uff0c\u90a3\u4e48\u53ef\u4ee5\u6309\u7167\u5982\u4e0b\u65b9\u5f0f\u6307\u5b9a\u914d\u7f6e\u6587\u4ef6\u4f4d\u7f6e\uff1a<\/p>\n<pre><code>java -jar boot_config_file-0.0.1-SNAPSHOT.jar --spring.config.additional-location=optional:javaboy\/app.yaml<\/code><\/pre>\n<p>\u5982\u679c\u8fd9\u4e2a\u989d\u5916\u6307\u5b9a\u7684\u914d\u7f6e\u6587\u4ef6\u548c\u5df2\u6709\u7684\u914d\u7f6e\u6587\u4ef6\u6709\u51b2\u7a81\uff0c\u90a3\u4e48\u8fd8\u662f\u4ee5\u540e\u6765\u8005\u4e3a\u51c6\u3002<\/p>\n<h2>3. \u4f4d\u7f6e\u901a\u914d\u7b26<\/h2>\n<p>\u6709\u4e00\u79cd\u60c5\u51b5\uff0c\u5047\u8bbe\u6211\u6709 redis \u548c mysql \u7684\u914d\u7f6e\uff0c\u6211\u60f3\u5c06\u4e4b\u653e\u5728\u4e24\u4e2a\u4e0d\u540c\u7684\u6587\u4ef6\u5939\u4e2d\u4ee5\u4fbf\u4e8e\u7ba1\u7406\uff0c\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042855050\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u90a3\u4e48\u5728\u9879\u76ee\u542f\u52a8\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u901a\u914d\u7b26 * \u6279\u91cf\u626b\u63cf\u76f8\u5e94\u7684\u6587\u4ef6\u5939\uff1a<\/p>\n<pre><code>java -jar boot_config_file-0.0.1-SNAPSHOT.jar --spring.config.location=optional:config\/*\/<\/code><\/pre>\n<p>\u4f7f\u7528\u901a\u914d\u7b26\u6279\u91cf\u626b\u63cf mysql \u548c redis \u76ee\u5f55\u65f6\uff0c\u9ed8\u8ba4\u7684\u52a0\u8f7d\u987a\u5e8f\u662f\u6309\u7167\u6587\u4ef6\u5939\u7684\u5b57\u6bcd\u6392\u5e8f\uff0c\u5373\u5148\u52a0\u8f7d mysql \u76ee\u5f55\u540e\u52a0\u8f7d redis \u76ee\u5f55\u3002<\/p>\n<blockquote><p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u901a\u914d\u7b26\u53ea\u80fd\u7528\u5728\u5916\u90e8\u76ee\u5f55\u4e2d\uff0c\u4e0d\u53ef\u4ee5\u7528\u5728 classpath \u4e2d\u7684\u76ee\u5f55\u4e0a\u3002\u53e6\u5916\uff0c\u5305\u542b\u4e86\u901a\u914d\u7b26\u7684\u76ee\u5f55\uff0c\u53ea\u80fd\u6709\u4e00\u4e2a\u901a\u914d\u7b26 <code>*<\/code>\uff0c\u4e0d\u53ef\u4ee5\u6709\u591a\u4e2a\uff0c\u5e76\u4e14\u8fd8\u5fc5\u987b\u662f\u4ee5 <code>*\/<\/code> \u7ed3\u5c3e\uff0c\u5373\u4e00\u4e2a\u76ee\u5f55\u7684\u6700\u540e\u90e8\u5206\u53ef\u4ee5\u4e0d\u786e\u5b9a\u3002<\/p><\/blockquote>\n<h2>4. \u5bfc\u5165\u5916\u90e8\u914d\u7f6e<\/h2>\n<p>\u4ece Spring Boot2.4 \u5f00\u59cb\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>spring.config.import<\/code> \u65b9\u6cd5\u6765\u5bfc\u5165\u914d\u7f6e\u6587\u4ef6\uff0c\u76f8\u6bd4\u4e8e <code>additional-location<\/code> \u914d\u7f6e\uff0c\u8fd9\u4e2a import \u5bfc\u5165\u66f4\u52a0\u7075\u6d3b\uff0c\u53ef\u4ee5\u5bfc\u5165\u4efb\u610f\u540d\u79f0\u7684\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<pre><code class=\"properties\">spring.config.import=optional:file:.\/dev.properties<\/code><\/pre>\n<p>\u751a\u81f3\uff0c\u8fd9\u4e2a <code>spring.config.import<\/code> \u8fd8\u53ef\u4ee5\u5bfc\u5165\u65e0\u6269\u5c55\u540d\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u4f8b\u5982\u6211\u6709\u4e00\u4e2a\u914d\u7f6e\u6587\u4ef6\uff0c\u662f properties \u683c\u5f0f\u7684\uff0c\u4f46\u662f\u8fd9\u4e2a\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u6ca1\u6709\u6269\u5c55\u540d\uff0c\u73b0\u5728\u6211\u60f3\u5c06\u4e4b\u4f5c\u4e3a properties \u683c\u5f0f\u7684\u914d\u7f6e\u6587\u4ef6\u5bfc\u5165\uff0c\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"properties\">spring.config.import=optional:file:\/Users\/sang\/dev[.properties]<\/code><\/pre>\n<p>\u597d\u5566\uff0c\u770b\u5b8c\u4e0a\u9762\u7684\u5185\u5bb9\uff0c\u6587\u7ae0\u4e00\u5f00\u59cb\u7684\u95ee\u9898\u7b54\u6848\u5c31\u4e0d\u7528\u6211\u591a\u8bf4\u4e86\u5427\uff5e<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042855044\">https:\/\/segmentfault.com\/a\/1190000042855044<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u804a\u4e00\u4e2a\u5c0f\u4f19\u4f34\u5728\u661f\u7403\u4e0a\u7684\u63d0\u95ee\uff1a \u95ee\u9898\u4e0d\u96be\uff0c\u89e3\u51b3\u65b9\u6848\u4e5f\u6709\u5f88\u591a\uff0c\u56e0\u6b64\u6211\u51b3\u5b9a\u64b8\u4e00\u7bc7\u6587\u7ae0\u548c\u5927\u5bb6\u4ed4\u7ec6\u8bf4\u8bf4\u8fd9\u4e2a\u95ee\u9898\u3002 1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10006877","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006877","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006877"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006877\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006877"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}