{"id":10006230,"date":"2022-08-17T12:36:16","date_gmt":"2022-08-17T04:36:16","guid":{"rendered":"https:\/\/ityet.com\/?p=10006230"},"modified":"2022-08-17T12:36:16","modified_gmt":"2022-08-17T04:36:16","slug":"%e9%9d%a2%e8%af%95%e7%aa%81%e5%87%bb75%ef%bc%9aspringboot-%e6%9c%89%e5%87%a0%e7%a7%8d%e8%af%bb%e5%8f%96%e9%85%8d%e7%bd%ae%e6%96%87%e4%bb%b6%e7%9a%84%e6%96%b9%e6%b3%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006230","title":{"rendered":"\u9762\u8bd5\u7a81\u51fb75\uff1aSpringBoot \u6709\u51e0\u79cd\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u7684\u65b9\u6cd5\uff1f"},"content":{"rendered":"<p>Spring Boot \u4e2d\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u6709\u4ee5\u4e0b 5 \u79cd\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528 @Value \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n<li>\u4f7f\u7528 @ConfigurationProperties \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n<li>\u4f7f\u7528 Environment \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n<li>\u4f7f\u7528 @PropertySource \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n<li>\u4f7f\u7528\u539f\u751f\u65b9\u5f0f\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p>\u5b83\u4eec\u7684\u5177\u4f53\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff0c\u4e3a\u4e86\u65b9\u4fbf\u6d4b\u8bd5\uff0c\u6211\u4eec\u5728 Spring Boot \u914d\u7f6e\u6587\u4ef6 application.properties \u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre><code class=\"properties\">profile.name=Spring Boot Profile\nprofile.desc=Spring Boot Profile Desc.<\/code><\/pre>\n<h2>1.\u4f7f\u7528 @Value \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6<\/h2>\n<p>\u4f7f\u7528 @Value \u53ef\u4ee5\u8bfb\u53d6\u5355\u4e2a\u914d\u7f6e\u9879\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n<pre><code class=\"java\">@SpringBootApplication\npublic class DemoApplication implements InitializingBean {\n    @Value(&quot;${profile.name}&quot;)\n    private String name;\n\n    public static void main(String[] args) {\n        SpringApplication.run(DemoApplication.class, args);\n    }\n\n    @Override\n    public void afterPropertiesSet() throws Exception {\n        System.out.println(&quot;My Profile Name\uff1a&quot; + name);\n    }\n}<\/code><\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u7684\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042331218\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h2>2.\u4f7f\u7528 @ConfigurationProperties \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6<\/h2>\n<p>@ConfigurationProperties \u548c @Value \u7684\u4f7f\u7528\u7565\u5fae\u4e0d\u540c\uff0c@Value \u662f\u8bfb\u53d6\u5355\u4e2a\u914d\u7f6e\u9879\u7684\uff0c\u800c @ConfigurationProperties \u662f\u8bfb\u53d6\u4e00\u7ec4\u914d\u7f6e\u9879\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 @ConfigurationProperties \u52a0\u5b9e\u4f53\u7c7b\u8bfb\u53d6\u4e00\u7ec4\u914d\u7f6e\u9879\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n<pre><code class=\"java\">import lombok.Data;\nimport org.springframework.boot.context.properties.ConfigurationProperties;\nimport org.springframework.stereotype.Component;\n\n@Component\n@ConfigurationProperties(prefix = &quot;profile&quot;)\n@Data\npublic class Profile {\n    private String name;\n    private String desc;\n}<\/code><\/pre>\n<p>\u5176\u4e2d prefix \u8868\u793a\u8bfb\u53d6\u4e00\u7ec4\u914d\u7f6e\u9879\u7684\u6839 name\uff0c\u76f8\u5f53\u4e8e Java \u4e2d\u7684\u7c7b\u540d\uff0c\u6700\u540e\u518d\u628a\u6b64\u914d\u7f6e\u7c7b\uff0c\u6ce8\u5165\u5230\u67d0\u4e00\u4e2a\u7c7b\u4e2d\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n<pre><code class=\"java\">@SpringBootApplication\npublic class DemoApplication implements InitializingBean {\n    @Autowired\n    private Profile profile;\n\n    public static void main(String[] args) {\n        SpringApplication.run(DemoApplication.class, args);\n    }\n\n    @Override\n    public void afterPropertiesSet() throws Exception {\n        System.out.println(&quot;Profile Object:&quot; + profile);\n    }\n}<\/code><\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u7684\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042331219\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h2>3.\u4f7f\u7528 Environment \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6<\/h2>\n<p>Environment \u662f Spring Core \u4e2d\u7684\u4e00\u4e2a\u7528\u4e8e\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u7684\u7c7b\uff0c\u5c06\u6b64\u7c7b\u4f7f\u7528 @Autowired \u6ce8\u5165\u5230\u7c7b\u4e2d\u5c31\u53ef\u4ee5\u4f7f\u7528\u5b83\u7684 getProperty \u65b9\u6cd5\u6765\u83b7\u53d6\u67d0\u4e2a\u914d\u7f6e\u9879\u7684\u503c\u4e86\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n<pre><code class=\"java\">import org.springframework.beans.factory.InitializingBean;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.core.env.Environment;\n\n@SpringBootApplication\npublic class DemoApplication implements InitializingBean {\n\n    @Autowired\n    private Environment environment;\n\n    public static void main(String[] args) {\n        SpringApplication.run(DemoApplication.class, args);\n    }\n\n    @Override\n    public void afterPropertiesSet() throws Exception {\n        System.out.println(&quot;Profile Name\uff1a&quot; + environment.getProperty(&quot;profile.name&quot;));\n    }\n}<\/code><\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u7684\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042331220\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h2>4.\u4f7f\u7528 @PropertySource \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6<\/h2>\n<p>\u4f7f\u7528 @PropertySource \u6ce8\u89e3\u53ef\u4ee5\u7528\u6765\u6307\u5b9a\u8bfb\u53d6\u67d0\u4e2a\u914d\u7f6e\u6587\u4ef6\uff0c\u6bd4\u5982\u6307\u5b9a\u8bfb\u53d6 application.properties \u914d\u7f6e\u6587\u4ef6\u7684\u914d\u7f6e\u5185\u5bb9\uff0c\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">@SpringBootApplication\n@PropertySource(&quot;classpath:application.properties&quot;)\npublic class DemoApplication implements InitializingBean {\n    @Value(&quot;${profile.name}&quot;)\n    private String name;\n\n    public static void main(String[] args) {\n        SpringApplication.run(DemoApplication.class, args);\n    }\n\n    @Override\n    public void afterPropertiesSet() throws Exception {\n        System.out.println(&quot;Name\uff1a&quot; + name);\n    }\n}<\/code><\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u7684\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042331221\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h3>\u4e2d\u6587\u4e71\u7801<\/h3>\n<p>\u5982\u679c\u914d\u7f6e\u6587\u4ef6\u4e2d\u51fa\u73b0\u4e2d\u6587\u4e71\u7801\u7684\u60c5\u51b5\uff0c\u53ef\u901a\u8fc7\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u7684\u65b9\u5f0f\u6765\u89e3\u51b3\u4e2d\u6587\u4e71\u7801\u7684\u95ee\u9898\uff0c\u5177\u4f53\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"java\">@PropertySource(value = &quot;dev.properties&quot;, encoding = &quot;utf-8&quot;)<\/code><\/pre>\n<h3>\u6ce8\u610f\u4e8b\u9879<\/h3>\n<p>@PropertySource \u6ce8\u89e3\u9ed8\u8ba4\u662f\u53ea\u652f\u6301 properties \u683c\u5f0f\u914d\u7f6e\u6587\u4ef6\u7684\u8bfb\u53d6\u7684\u3002<\/p>\n<h2>5.\u4f7f\u7528\u539f\u751f\u65b9\u5f0f\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6<\/h2>\n<p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6700\u539f\u59cb\u7684\u65b9\u5f0f Properties \u5bf9\u8c61\u6765\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\uff0c\u5982\u4e0b\u4ee3\u7801\u6240\u793a\uff1a<\/p>\n<pre><code class=\"java\">import org.springframework.beans.factory.InitializingBean;\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\n\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.nio.charset.StandardCharsets;\nimport java.util.Properties;\n\n@SpringBootApplication\npublic class DemoApplication implements InitializingBean {\n\n    public static void main(String[] args) {\n        SpringApplication.run(DemoApplication.class, args);\n    }\n\n    @Override\n    public void afterPropertiesSet() throws Exception {\n        Properties props = new Properties();\n        try {\n            InputStreamReader inputStreamReader = new InputStreamReader(\n                    this.getClass().getClassLoader().getResourceAsStream(&quot;application.properties&quot;),\n                    StandardCharsets.UTF_8);\n            props.load(inputStreamReader);\n        } catch (IOException e1) {\n            System.out.println(e1);\n        }\n        System.out.println(&quot;Properties Name\uff1a&quot; + props.getProperty(&quot;profile.name&quot;));\n    }\n}<\/code><\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u7684\u6267\u884c\u7ed3\u679c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042331222\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u5728 Spring Boot \u4e2d\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u6709\u4ee5\u4e0b 5 \u79cd\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528 @Value \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n<li>\u4f7f\u7528 @ConfigurationProperties \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n<li>\u4f7f\u7528 @PropertySource \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n<li>\u4f7f\u7528 Environment \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n<li>\u4f7f\u7528\u539f\u751f\u65b9\u5f0f\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p>\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f\u524d 3 \u79cd\uff0c\u5982\u679c\u8bfb\u53d6\u67d0\u4e00\u4e2a\u914d\u7f6e\u9879\u53ef\u4f7f\u7528 @Value\uff0c\u5982\u679c\u8bfb\u53d6\u4e00\u7ec4\u914d\u7f6e\u9879\u53ef\u4f7f\u7528 @ConfigurationProperties\uff0c\u5982\u679c\u8981\u6307\u5b9a\u8bfb\u53d6\u67d0\u4e00\u4e2a\u5177\u4f53\u7684\u914d\u7f6e\u6587\u4ef6\u53ef\u4f7f\u7528 @PropertySource \u6765\u6307\u5b9a\u3002<\/p>\n<blockquote>\n<p>\u662f\u975e\u5ba1\u4e4b\u4e8e\u5df1\uff0c\u6bc1\u8a89\u542c\u4e4b\u4e8e\u4eba\uff0c\u5f97\u5931\u5b89\u4e4b\u4e8e\u6570\u3002<\/p>\n<p>\u516c\u4f17\u53f7\uff1aJava\u9762\u8bd5\u771f\u9898\u89e3\u6790<\/p>\n<p>\u9762\u8bd5\u5408\u96c6\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=8IDsfSWq9k0m2QMkFfBOTg%3D%3D.m7uazylBD9I3EEwG1M%2Fktz%2BnoyS93lyYPmDLpZfe21Z2C%2FBWvmai9jd940uuLI%2B8\" rel=\"nofollow\">https:\/\/gitee.com\/mydb\/interview<\/a><\/p>\n<\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042331216\">https:\/\/segmentfault.com\/a\/1190000042331216<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot \u4e2d\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u6709\u4ee5\u4e0b 5 \u79cd\u65b9\u6cd5\uff1a \u4f7f\u7528 @Value \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u3002 \u4f7f\u7528 @Con [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006230","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006230","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006230"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006230\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}