{"id":8450,"date":"2023-11-28T20:28:29","date_gmt":"2023-11-28T12:28:29","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=8450"},"modified":"2023-12-06T10:32:47","modified_gmt":"2023-12-06T02:32:47","slug":"springboot-zhong-ru-he-gei-jing-tai-bian-liang-fu","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-zhong-ru-he-gei-jing-tai-bian-liang-fu\/","title":{"rendered":"SpringBoot\u4e2d\u5982\u4f55\u7ed9\u9759\u6001\u53d8\u91cf\u8d4b\u503c\uff1f"},"content":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0c\u7ed9\u9759\u6001\u53d8\u91cf\u8d4b\u503c\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u573a\u666f\u548c\u9700\u6c42\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u5f0f\uff1a<\/p>\n<ol>\n<li>\n<strong>\u901a\u8fc7\u6784\u9020\u65b9\u6cd5\u8d4b\u503c\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u4e2d\u7ed9\u9759\u6001\u53d8\u91cf\u8d4b\u503c\u3002\u8fd9\u901a\u5e38\u662f\u6700\u57fa\u672c\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u5728\u7c7b\u5b9e\u4f8b\u5316\u7684\u65f6\u5019\u5c31\u77e5\u9053\u9759\u6001\u53d8\u91cf\u7684\u503c\u7684\u60c5\u51b5\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">public class MyService {\n    private static String myStaticVariable;\n\n    public MyService(String value) {\n        myStaticVariable = value;\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u901a\u8fc7\u9759\u6001\u4ee3\u7801\u5757\u8d4b\u503c\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u9759\u6001\u4ee3\u7801\u5757\uff0c\u5728\u7c7b\u52a0\u8f7d\u65f6\u6267\u884c\u4e00\u6b21\u6027\u7684\u521d\u59cb\u5316\u64cd\u4f5c\uff0c\u5305\u62ec\u7ed9\u9759\u6001\u53d8\u91cf\u8d4b\u503c\u3002\u9002\u7528\u4e8e\u9700\u8981\u5728\u7c7b\u52a0\u8f7d\u65f6\u8fdb\u884c\u521d\u59cb\u5316\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">public class MyService {\n    private static String myStaticVariable;\n\n    static {\n        myStaticVariable = &quot;initialValue&quot;;\n    }\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u901a\u8fc7\u9759\u6001\u65b9\u6cd5\u8d4b\u503c\uff1a<\/strong><\/p>\n<ul>\n<li>\u63d0\u4f9b\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\uff0c\u901a\u8fc7\u8be5\u65b9\u6cd5\u7ed9\u9759\u6001\u53d8\u91cf\u8d4b\u503c\u3002\u8fd9\u6837\u53ef\u4ee5\u7075\u6d3b\u5730\u5728\u9700\u8981\u7684\u65f6\u5019\u8c03\u7528\u8be5\u65b9\u6cd5\u8fdb\u884c\u8d4b\u503c\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">public class MyService {\n    private static String myStaticVariable;\n\n    public static void setMyStaticVariable(String value) {\n        myStaticVariable = value;\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u4f7f\u7528\u65f6\uff0c\u901a\u8fc7\u8c03\u7528 <code>MyService.setMyStaticVariable(&quot;newValue&quot;)<\/code> \u8fdb\u884c\u8d4b\u503c\u3002\n<\/li>\n<li>\n<strong>\u901a\u8fc7@ConfigurationProperties\u6ce8\u89e3\u8d4b\u503c\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 <code>@ConfigurationProperties<\/code> \u6ce8\u89e3\u7ed3\u5408 <code>@Component<\/code> \u6216 <code>@Configuration<\/code> \u6ce8\u89e3\uff0c\u901a\u8fc7\u5916\u90e8\u914d\u7f6e\u6587\u4ef6\u7684\u65b9\u5f0f\u7ed9\u9759\u6001\u53d8\u91cf\u8d4b\u503c\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u4ece\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6\u9759\u6001\u53d8\u91cf\u7684\u503c\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Component\n@ConfigurationProperties(prefix = &quot;my.service&quot;)\npublic class MyService {\n    private static String myStaticVariable;\n\n    \/\/ getter and setter for myStaticVariable\n}\n<\/code><\/pre>\n<p>\u5728 <code>application.properties<\/code> \u6216 <code>application.yml<\/code> \u4e2d\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"language-properties\">my.service.myStaticVariable=propertyValue\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u7ed9\u9759\u6001\u53d8\u91cf\u8d4b\u503c\u7684\u65b9\u5f0f\u3002\u9009\u62e9\u9002\u5408\u4f60\u9700\u6c42\u7684\u65b9\u5f0f\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0c\u7ed9\u9759\u6001\u53d8\u91cf\u8d4b\u503c\u6709\u591a\u79cd\u65b9\u5f0f\uff0c\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u573a\u666f\u548c\u9700\u6c42\u3002\u4ee5\u4e0b\u662f\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u5f0f\uff1a \u901a\u8fc7 [&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-8450","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8450","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=8450"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8450\/revisions"}],"predecessor-version":[{"id":16615,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8450\/revisions\/16615"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=8450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=8450"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=8450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}