{"id":7895,"date":"2023-11-28T20:05:53","date_gmt":"2023-11-28T12:05:53","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=7895"},"modified":"2023-12-05T15:56:34","modified_gmt":"2023-12-05T07:56:34","slug":"springboot-zhong-shen-me-shi-spring-profiles-2","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-zhong-shen-me-shi-spring-profiles-2\/","title":{"rendered":"SpringBoot \u4e2d\u4ec0\u4e48\u662f Spring Profiles?"},"content":{"rendered":"<p>Spring Boot \u4e2d\u7684 Spring Profiles \u662f\u4e00\u79cd\u673a\u5236\uff0c\u7528\u4e8e\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u4e3a\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u4e0d\u540c\u7684\u914d\u7f6e\u3002\u901a\u8fc7\u4f7f\u7528 Spring Profiles\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u548c\u6fc0\u6d3b\u4e00\u7ec4\u914d\u7f6e\uff0c\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u5728\u4e0d\u540c\u7684\u90e8\u7f72\u73af\u5883\u4e0b\u80fd\u591f\u7075\u6d3b\u9002\u5e94\u4e0d\u540c\u7684\u9700\u6c42\u3002<\/p>\n<p>Spring Profiles \u7684\u4e3b\u8981\u7528\u9014\u5305\u62ec\uff1a<\/p>\n<ol>\n<li>\n<strong>\u73af\u5883\u9694\u79bb\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u5f00\u53d1\u3001\u6d4b\u8bd5\u548c\u751f\u4ea7\u7b49\u4e0d\u540c\u7684\u73af\u5883\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u914d\u7f6e\u3002Spring Profiles \u5141\u8bb8\u4f60\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u6fc0\u6d3b\u4e0d\u540c\u7684\u914d\u7f6e\uff0c\u786e\u4fdd\u5e94\u7528\u7a0b\u5e8f\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u8868\u73b0\u4e00\u81f4\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>\u914d\u7f6e\u5207\u6362\uff1a<\/strong><\/p>\n<ul>\n<li>\u901a\u8fc7\u4f7f\u7528 Spring Profiles\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u9009\u62e9\u4e0d\u540c\u7684\u914d\u7f6e\uff0c\u4f8b\u5982\u9009\u62e9\u4f7f\u7528\u4e0d\u540c\u7684\u6570\u636e\u5e93\u8fde\u63a5\u3001\u6d88\u606f\u961f\u5217\u7b49\u3002\u8fd9\u4f7f\u5f97\u5728\u5207\u6362\u914d\u7f6e\u65f6\u66f4\u52a0\u65b9\u4fbf\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>\u6761\u4ef6\u5316\u914d\u7f6e\uff1a<\/strong><\/p>\n<ul>\n<li>Spring Profiles \u7ed3\u5408\u6761\u4ef6\u5316\u914d\u7f6e\uff08Conditional Configuration\uff09\u4f7f\u7528\uff0c\u53ef\u4ee5\u6839\u636e\u6fc0\u6d3b\u7684 Profile \u6765\u52a8\u6001\u5730\u51b3\u5b9a\u662f\u5426\u52a0\u8f7d\u7279\u5b9a\u7684\u914d\u7f6e\u7c7b\u6216 Bean\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3><a id=\"%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8spring-profiles%EF%BC%9A\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a>\u5982\u4f55\u4f7f\u7528 Spring Profiles\uff1a<\/h3>\n<ol>\n<li>\n<strong>\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u6fc0\u6d3b Profile\uff1a<\/strong><\/p>\n<ul>\n<li>\n\u901a\u8fc7\u5728 <code>application.properties<\/code> \u6216 <code>application.yml<\/code> \u6587\u4ef6\u4e2d\u8bbe\u7f6e <code>spring.profiles.active<\/code> \u5c5e\u6027\u6765\u6fc0\u6d3b\u7279\u5b9a\u7684 Profile\u3002<\/p>\n<pre><code class=\"language-properties\">spring.profiles.active=dev\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>\u5728\u542f\u52a8\u547d\u4ee4\u4e2d\u6fc0\u6d3b Profile\uff1a<\/strong><\/p>\n<ul>\n<li>\n\u5728\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u901a\u8fc7\u547d\u4ee4\u884c\u53c2\u6570 <code>-Dspring.profiles.active=dev<\/code> \u6765\u6fc0\u6d3b\u7279\u5b9a\u7684 Profile\u3002<\/p>\n<pre><code class=\"language-bash\">java -jar myapp.jar -Dspring.profiles.active=dev\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>\u4f7f\u7528\u6ce8\u89e3\u6fc0\u6d3b Profile\uff1a<\/strong><\/p>\n<ul>\n<li>\n\u5728 Spring Boot \u5e94\u7528\u7a0b\u5e8f\u7684\u542f\u52a8\u7c7b\u4e0a\u4f7f\u7528 <code>@ActiveProfiles<\/code> \u6ce8\u89e3\uff0c\u6307\u5b9a\u8981\u6fc0\u6d3b\u7684 Profile\u3002<\/p>\n<pre><code class=\"language-java\">import org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.test.context.ActiveProfiles;\n\n@SpringBootApplication\n@ActiveProfiles(&quot;dev&quot;)\npublic class MyApplication {\n\n    public static void main(String[] args) {\n        SpringApplication.run(MyApplication.class, args);\n    }\n}\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>\u6761\u4ef6\u5316\u914d\u7f6e\uff1a<\/strong><\/p>\n<ul>\n<li>\n\u5728\u914d\u7f6e\u7c7b\u6216\u8005 <code>@Configuration<\/code> \u6ce8\u89e3\u7684\u7c7b\u4e0a\u4f7f\u7528 <code>@Profile<\/code> \u6ce8\u89e3\uff0c\u6307\u5b9a\u5728\u7279\u5b9a Profile \u4e0b\u751f\u6548\u7684\u914d\u7f6e\u3002<\/p>\n<pre><code class=\"language-java\">import org.springframework.context.annotation.Configuration;\nimport org.springframework.context.annotation.Profile;\n\n@Configuration\n@Profile(&quot;dev&quot;)\npublic class DevConfiguration {\n    \/\/ Dev profile specific configurations\n}\n<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u65b9\u4fbf\u5730\u7ba1\u7406\u548c\u914d\u7f6e Spring Boot \u5e94\u7528\u7a0b\u5e8f\u5728\u4e0d\u540c\u73af\u5883\u4e0b\u7684\u884c\u4e3a\u3002Spring Profiles \u662f Spring Boot \u4e2d\u4e00\u9879\u975e\u5e38\u6709\u7528\u7684\u529f\u80fd\uff0c\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u66f4\u6613\u4e8e\u5728\u591a\u4e2a\u73af\u5883\u4e2d\u90e8\u7f72\u548c\u914d\u7f6e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot \u4e2d\u7684 Spring Profiles \u662f\u4e00\u79cd\u673a\u5236\uff0c\u7528\u4e8e\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u4e3a\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u4e0d\u540c [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[],"class_list":["post-7895","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7895","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=7895"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7895\/revisions"}],"predecessor-version":[{"id":16551,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7895\/revisions\/16551"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=7895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=7895"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=7895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}