{"id":9614,"date":"2023-11-28T21:07:31","date_gmt":"2023-11-28T13:07:31","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=9614"},"modified":"2023-12-06T17:23:08","modified_gmt":"2023-12-06T09:23:08","slug":"springboot-zhong-shen-me-shi-spring-profiles-3","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-zhong-shen-me-shi-spring-profiles-3\/","title":{"rendered":"SpringBoot \u4e2d\u4ec0\u4e48\u662f Spring Profiles?"},"content":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0cSpring Profiles \u662f\u4e00\u79cd\u673a\u5236\uff0c\u5141\u8bb8\u4f60\u5b9a\u4e49\u4e00\u7ec4\u914d\u7f6e\uff0c\u5e76\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u9009\u62e9\u6027\u5730\u6fc0\u6d3b\u8fd9\u4e9b\u914d\u7f6e\u3002\u5b83\u4f7f\u5f97\u4f60\u53ef\u4ee5\u6839\u636e\u5e94\u7528\u7a0b\u5e8f\u8fd0\u884c\u7684\u73af\u5883\uff0c\u8f7b\u677e\u5207\u6362\u548c\u7ba1\u7406\u4e0d\u540c\u7684\u914d\u7f6e\u96c6\u3002<\/p>\n<p>\u901a\u8fc7 Spring Profiles\uff0c\u4f60\u53ef\u4ee5\u5728\u4e0d\u540c\u7684\u8fd0\u884c\u65f6\u73af\u5883\u4e2d\u4f7f\u7528\u4e0d\u540c\u7684\u914d\u7f6e\uff0c\u4f8b\u5982\u5f00\u53d1\u3001\u6d4b\u8bd5\u3001\u751f\u4ea7\u7b49\u3002Spring Boot \u63d0\u4f9b\u4e86\u4e00\u4e2a <code>application.properties<\/code> \u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>spring.profiles.active<\/code> \u5c5e\u6027\u6765\u6fc0\u6d3b\u5177\u4f53\u7684 profile\u3002\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u548c\u7ba1\u7406\u591a\u4e2a\u914d\u7f6e\u6587\u4ef6\uff0c\u7136\u540e\u5728\u8fd0\u884c\u65f6\u901a\u8fc7\u6fc0\u6d3b\u4e0d\u540c\u7684 profile \u6765\u9009\u62e9\u52a0\u8f7d\u76f8\u5e94\u7684\u914d\u7f6e\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f Spring Profiles \u7684\u4e00\u4e9b\u57fa\u672c\u7528\u6cd5\uff1a<\/p>\n<ol>\n<li>\n<strong>\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u6fc0\u6d3b Profile\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728 <code>application.properties<\/code> \u6216 <code>application.yml<\/code> \u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>spring.profiles.active<\/code> \u5c5e\u6027\u6765\u6fc0\u6d3b\u4e00\u4e2a\u6216\u591a\u4e2a profile\u3002\u4f8b\u5982\uff1a\n<pre><code class=\"language-yaml\">spring:\n  profiles:\n    active: dev\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>\u8fd9\u6837\u5c31\u6fc0\u6d3b\u4e86\u540d\u4e3a &quot;dev&quot; \u7684 profile\u3002\n<\/li>\n<li>\n<strong>\u547d\u4ee4\u884c\u53c2\u6570\u6fc0\u6d3b Profile\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f60\u4e5f\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570\u6765\u6fc0\u6d3b profile\u3002\u4f8b\u5982\uff0c\u5728\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\u65f6\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a\n<pre><code class=\"language-bash\">java -jar myapp.jar --spring.profiles.active=dev\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>\u591a\u4e2a\u914d\u7f6e\u6587\u4ef6\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f60\u53ef\u4ee5\u4e3a\u6bcf\u4e2a profile \u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u547d\u540d\u89c4\u5219\u4e3a <code>application-{profile}.properties<\/code> \u6216 <code>application-{profile}.yml<\/code>\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e &quot;dev&quot; profile\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>application-dev.properties<\/code> \u7684\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>Profile-specific Bean \u5b9a\u4e49\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f60\u8fd8\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528 <code>@Profile<\/code> \u6ce8\u89e3\u6765\u6807\u8bb0\u7279\u5b9a profile \u4e0b\u7684 Bean \u5b9a\u4e49\u3002\u8fd9\u6837\uff0c\u53ea\u6709\u5728\u6fc0\u6d3b\u4e86\u76f8\u5e94 profile \u65f6\uff0c\u624d\u4f1a\u521b\u5efa\u8fd9\u4e9b Bean\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<pre><code class=\"language-java\">@Configuration\n@Profile(&quot;dev&quot;)\npublic class DevConfig {\n    \/\/ Configuration for the &quot;dev&quot; profile\n}\n<\/code><\/pre>\n<p>\u901a\u8fc7 Spring Profiles\uff0c\u4f60\u53ef\u4ee5\u66f4\u7075\u6d3b\u5730\u7ba1\u7406\u4e0d\u540c\u73af\u5883\u4e0b\u7684\u914d\u7f6e\uff0c\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u80fd\u591f\u9002\u5e94\u4e0d\u540c\u7684\u90e8\u7f72\u548c\u8fd0\u884c\u9700\u6c42\u3002\u8fd9\u5728\u5f00\u53d1\u3001\u6d4b\u8bd5\u548c\u751f\u4ea7\u73af\u5883\u4e4b\u95f4\u8fdb\u884c\u5207\u6362\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0cSpring Profiles \u662f\u4e00\u79cd\u673a\u5236\uff0c\u5141\u8bb8\u4f60\u5b9a\u4e49\u4e00\u7ec4\u914d\u7f6e\uff0c\u5e76\u5728\u4e0d\u540c\u7684\u73af\u5883 [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117],"tags":[],"class_list":["post-9614","post","type-post","status-publish","format-standard","hentry","category-springcloud"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=9614"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9614\/revisions"}],"predecessor-version":[{"id":16744,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/9614\/revisions\/16744"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=9614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=9614"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=9614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}