{"id":8456,"date":"2023-11-28T20:28:41","date_gmt":"2023-11-28T12:28:41","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=8456"},"modified":"2023-12-06T10:33:21","modified_gmt":"2023-12-06T02:33:21","slug":"springboot-chu-shi-hua-huan-jing-bian-liang-liu-ch","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/springboot-chu-shi-hua-huan-jing-bian-liang-liu-ch\/","title":{"rendered":"SpringBoot\u521d\u59cb\u5316\u73af\u5883\u53d8\u91cf\u6d41\u7a0b\uff1f"},"content":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0c\u521d\u59cb\u5316\u73af\u5883\u53d8\u91cf\u7684\u6d41\u7a0b\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u521d\u59cb\u5316\u73af\u5883\u53d8\u91cf\u7684\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\n<strong>\u901a\u8fc7<code>application.properties<\/code>\u6216<code>application.yml<\/code>\u6587\u4ef6\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728 Spring Boot \u9879\u76ee\u7684 <code>src\/main\/resources<\/code> \u76ee\u5f55\u4e0b\u521b\u5efa <code>application.properties<\/code> \u6216 <code>application.yml<\/code> \u6587\u4ef6\uff0c\u5e76\u5728\u6587\u4ef6\u4e2d\u5b9a\u4e49\u9700\u8981\u7684\u73af\u5883\u53d8\u91cf\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-properties\">my.property=value\n<\/code><\/pre>\n<p>\u5728\u4ee3\u7801\u4e2d\u901a\u8fc7 <code>@Value<\/code> \u6ce8\u89e3\u6765\u6ce8\u5165\uff1a<\/p>\n<pre><code class=\"language-java\">@Value(&quot;${my.property}&quot;)\nprivate String myProperty;\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u901a\u8fc7<code>@ConfigurationProperties<\/code>\u6ce8\u89e3\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528 <code>@ConfigurationProperties<\/code> \u6ce8\u89e3\uff0c\u5c06\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u5c5e\u6027\u503c\u7ed1\u5b9a\u5230 Java \u5bf9\u8c61\u4e2d\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u4e00\u6b21\u6027\u52a0\u8f7d\u591a\u4e2a\u5c5e\u6027\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Component\n@ConfigurationProperties(prefix = &quot;my&quot;)\npublic class MyProperties {\n    private String property;\n\n    \/\/ getters and setters\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.property=value\n<\/code><\/pre>\n<p>\u5728\u4ee3\u7801\u4e2d\u6ce8\u5165 <code>MyProperties<\/code> \u5bf9\u8c61\uff1a<\/p>\n<pre><code class=\"language-java\">@Autowired\nprivate MyProperties myProperties;\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u901a\u8fc7<code>Environment<\/code>\u63a5\u53e3\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u4ee3\u7801\u4e2d\u901a\u8fc7 <code>Environment<\/code> \u63a5\u53e3\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u9700\u8981\u52a8\u6001\u83b7\u53d6\u73af\u5883\u53d8\u91cf\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Autowired\nprivate Environment environment;\n\npublic void someMethod() {\n    String propertyValue = environment.getProperty(&quot;my.property&quot;);\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u901a\u8fc7<code>CommandLineRunner<\/code>\u63a5\u53e3\uff1a<\/strong><\/p>\n<ul>\n<li>\u5b9e\u73b0 <code>CommandLineRunner<\/code> \u63a5\u53e3\uff0c\u91cd\u5199 <code>run<\/code> \u65b9\u6cd5\uff0c\u5728\u8be5\u65b9\u6cd5\u4e2d\u53ef\u4ee5\u8fdb\u884c\u4e00\u4e9b\u521d\u59cb\u5316\u5de5\u4f5c\uff0c\u5305\u62ec\u521d\u59cb\u5316\u73af\u5883\u53d8\u91cf\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Component\npublic class MyCommandLineRunner implements CommandLineRunner {\n\n    @Override\n    public void run(String... args) throws Exception {\n        \/\/ \u521d\u59cb\u5316\u73af\u5883\u53d8\u91cf\u7684\u903b\u8f91\n    }\n}\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u542f\u52a8\u65f6\u6267\u884c\u4e00\u4e9b\u521d\u59cb\u5316\u64cd\u4f5c\uff0c\u5305\u62ec\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002\n<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u521d\u59cb\u5316\u73af\u5883\u53d8\u91cf\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\u5728 Spring Boot \u4e2d\uff0c\u4f7f\u7528 <code>application.properties<\/code> \u6216 <code>application.yml<\/code> \u6587\u4ef6\u662f\u6700\u5e38\u89c1\u7684\u914d\u7f6e\u65b9\u5f0f\uff0c\u800c\u901a\u8fc7 <code>@ConfigurationProperties<\/code> \u6ce8\u89e3\u548c <code>Environment<\/code> \u63a5\u53e3\u5219\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u9009\u62e9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Spring Boot \u4e2d\uff0c\u521d\u59cb\u5316\u73af\u5883\u53d8\u91cf\u7684\u6d41\u7a0b\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u521d\u59cb\u5316\u73af\u5883\u53d8\u91cf\u7684\u65b9\u6cd5 [&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-8456","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8456","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=8456"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8456\/revisions"}],"predecessor-version":[{"id":16616,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/8456\/revisions\/16616"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=8456"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=8456"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=8456"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}