{"id":7593,"date":"2023-11-28T19:41:38","date_gmt":"2023-11-28T11:41:38","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=7593"},"modified":"2023-12-05T15:39:13","modified_gmt":"2023-12-05T07:39:13","slug":"springboot-zhong-shen-me-shi-spring-profiles","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-zhong-shen-me-shi-spring-profiles\/","title":{"rendered":"SpringBoot \u4e2d\u4ec0\u4e48\u662f Spring Profiles?"},"content":{"rendered":"<p>Spring Boot \u4e2d\u7684 Spring Profiles \u662f\u4e00\u79cd\u673a\u5236\uff0c\u5141\u8bb8\u4f60\u5b9a\u4e49\u548c\u6fc0\u6d3b\u4e00\u7ec4\u914d\u7f6e\u3002\u901a\u8fc7\u4f7f\u7528 Spring Profiles\uff0c\u4f60\u53ef\u4ee5\u4e3a\u540c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u5b9a\u4e49\u591a\u4e2a\u4e0d\u540c\u7684\u914d\u7f6e\u96c6\uff0c\u5e76\u6839\u636e\u9700\u8981\u9009\u62e9\u6027\u5730\u6fc0\u6d3b\u5176\u4e2d\u4e00\u4e2a\u914d\u7f6e\u96c6\u3002\u8fd9\u5bf9\u4e8e\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e2d\u8fd0\u884c\u5e94\u7528\u7a0b\u5e8f\u3001\u5e94\u5bf9\u4e0d\u540c\u7684\u90e8\u7f72\u573a\u666f\u6216\u8005\u5728\u5f00\u53d1\u9636\u6bb5\u4f7f\u7528\u4e0d\u540c\u7684\u914d\u7f6e\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>Spring Profiles \u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u4e2a\u5173\u952e\u70b9\uff1a<\/p>\n<ol>\n<li>\n<strong>\u5b9a\u4e49 Profiles\uff1a<\/strong> \u4f60\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u914d\u7f6e\u6587\u4ef6\uff08\u5982 <code>application.properties<\/code> \u6216 <code>application.yml<\/code>\uff09\u4e2d\u5b9a\u4e49 Profiles\u3002\u901a\u8fc7\u4f7f\u7528 <code>spring.profiles.active<\/code> \u5c5e\u6027\uff0c\u4f60\u53ef\u4ee5\u6307\u5b9a\u8981\u6fc0\u6d3b\u7684 Profile\u3002<\/p>\n<pre><code class=\"language-yaml\"># application.properties\nspring.profiles.active=dev\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>Profile-specific \u914d\u7f6e\u6587\u4ef6\uff1a<\/strong> \u4f60\u53ef\u4ee5\u4e3a\u6bcf\u4e2a Profile \u521b\u5efa\u4e13\u95e8\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u4e3a <code>application-{profile}.properties<\/code> \u6216 <code>application-{profile}.yml<\/code>\u3002Spring Boot \u4f1a\u6839\u636e\u6fc0\u6d3b\u7684 Profile \u81ea\u52a8\u52a0\u8f7d\u5bf9\u5e94\u7684\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<pre><code class=\"language-yaml\"># application-dev.properties\ndatabase.url=jdbc:mysql:\/\/localhost:3306\/devdb\n<\/code><\/pre>\n<pre><code class=\"language-yaml\"># application-prod.properties\ndatabase.url=jdbc:mysql:\/\/prod-server:3306\/proddb\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528 Profiles\uff1a<\/strong> \u4f60\u53ef\u4ee5\u5728 Java \u4ee3\u7801\u4e2d\u4f7f\u7528 <code>@Profile<\/code> \u6ce8\u89e3\u6765\u9650\u5b9a\u67d0\u4e2a Bean \u53ea\u5728\u7279\u5b9a\u7684 Profile \u6fc0\u6d3b\u65f6\u624d\u521b\u5efa\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-java\">import org.springframework.context.annotation.Bean;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.context.annotation.Profile;\n\n@Configuration\npublic class MyConfig {\n\n    @Bean\n    @Profile(&quot;dev&quot;)\n    public DataSource dataSourceDev() {\n        \/\/ Return dev data source configuration\n    }\n\n    @Bean\n    @Profile(&quot;prod&quot;)\n    public DataSource dataSourceProd() {\n        \/\/ Return prod data source configuration\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u5728\u6d4b\u8bd5\u4e2d\u4f7f\u7528 Profiles\uff1a<\/strong> \u4f60\u53ef\u4ee5\u5728\u6d4b\u8bd5\u7c7b\u4e2d\u4f7f\u7528 <code>@ActiveProfiles<\/code> \u6ce8\u89e3\u6765\u6307\u5b9a\u6fc0\u6d3b\u7684 Profiles\u3002\u8fd9\u5bf9\u4e8e\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u8fd0\u884c\u6d4b\u8bd5\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code class=\"language-java\">import org.springframework.test.context.ActiveProfiles;\n\n@ActiveProfiles(&quot;test&quot;)\npublic class MyTest {\n    \/\/ Test code here\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4f7f\u7528 Spring Profiles\uff0c\u4f60\u53ef\u4ee5\u66f4\u8f7b\u677e\u5730\u7ba1\u7406\u5e94\u7528\u7a0b\u5e8f\u5728\u4e0d\u540c\u73af\u5883\u4e2d\u7684\u914d\u7f6e\uff0c\u540c\u65f6\u63d0\u9ad8\u4e86\u914d\u7f6e\u7684\u7075\u6d3b\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u8fd9\u5bf9\u4e8e\u5904\u7406\u5f00\u53d1\u3001\u6d4b\u8bd5\u548c\u751f\u4ea7\u73af\u5883\u7684\u5dee\u5f02\u975e\u5e38\u6709\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot \u4e2d\u7684 Spring Profiles \u662f\u4e00\u79cd\u673a\u5236\uff0c\u5141\u8bb8\u4f60\u5b9a\u4e49\u548c\u6fc0\u6d3b\u4e00\u7ec4\u914d\u7f6e\u3002\u901a\u8fc7\u4f7f\u7528  [&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-7593","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7593","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=7593"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7593\/revisions"}],"predecessor-version":[{"id":16527,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/7593\/revisions\/16527"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=7593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=7593"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=7593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}