{"id":4252,"date":"2021-12-02T15:11:28","date_gmt":"2021-12-02T07:11:28","guid":{"rendered":"http:\/\/123.57.164.21\/?p=4252"},"modified":"2021-12-02T15:11:28","modified_gmt":"2021-12-02T07:11:28","slug":"spring-boot%e9%85%8d%e7%bd%ae%e7%bb%91%e5%ae%9a","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=4252","title":{"rendered":"Spring Boot\u914d\u7f6e\u7ed1\u5b9a"},"content":{"rendered":"\n<p>\u6240\u8c13\u201c\u914d\u7f6e\u7ed1\u5b9a\u201d\u5c31\u662f\u628a\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u503c\u4e0e JavaBean \u4e2d\u5bf9\u5e94\u7684\u5c5e\u6027\u8fdb\u884c\u7ed1\u5b9a\u3002\u901a\u5e38\uff0c\u6211\u4eec\u4f1a\u628a\u4e00\u4e9b\u914d\u7f6e\u4fe1\u606f\uff08\u4f8b\u5982\uff0c\u6570\u636e\u5e93\u914d\u7f6e\uff09\u653e\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u901a\u8fc7 Java \u4ee3\u7801\u53bb\u8bfb\u53d6\u8be5\u914d\u7f6e\u6587\u4ef6\uff0c\u5e76\u4e14\u628a\u914d\u7f6e\u6587\u4ef6\u4e2d\u6307\u5b9a\u7684\u914d\u7f6e\u5c01\u88c5\u5230 JavaBean\uff08\u5b9e\u4f53\u7c7b\uff09 \u4e2d\u3002<br><br>SpringBoot \u63d0\u4f9b\u4e86\u4ee5\u4e0b 2 \u79cd\u65b9\u5f0f\u8fdb\u884c\u914d\u7f6e\u7ed1\u5b9a\uff1a<\/p>\n\n\n\n<ul><li>\u4f7f\u7528 @ConfigurationProperties \u6ce8\u89e3<\/li><li>\u4f7f\u7528 @Value \u6ce8\u89e3<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">@ConfigurationProperties<\/h4>\n\n\n\n<p>\u901a\u8fc7 Spring Boot \u63d0\u4f9b\u7684 @ConfigurationProperties \u6ce8\u89e3\uff0c\u53ef\u4ee5\u5c06\u5168\u5c40\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u914d\u7f6e\u6570\u636e\u7ed1\u5b9a\u5230 JavaBean \u4e2d\u3002\u4e0b\u9762\u6211\u4eec\u4ee5 Spring Boot \u9879\u76ee&nbsp;helloworld \u4e3a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u901a\u8fc7&nbsp;@ConfigurationProperties \u6ce8\u89e3\u8fdb\u884c\u914d\u7f6e\u7ed1\u5b9a\u3002<\/p>\n\n\n\n<p>1. \u5728 helloworld \u7684\u5168\u5c40\u914d\u7f6e\u6587\u4ef6 application.yml \u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u81ea\u5b9a\u4e49\u5c5e\u6027\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-30-1024x338.png\" alt=\"\" class=\"wp-image-4253\" width=\"636\" height=\"210\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-30-1024x338.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-30-300x99.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-30-768x254.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-30-1536x507.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-30-830x274.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-30-230x76.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-30-350x116.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-30-480x159.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-30.png 1798w\" sizes=\"(max-width: 636px) 100vw, 636px\" \/><\/figure><\/div>\n\n\n\n<p>2.&nbsp; \u5728 helloworld \u9879\u76ee\u7684 net.biancheng.www.bean \u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a Person \u7684\u5b9e\u4f53\u7c7b\uff0c\u5e76\u5c06\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u5c5e\u6027\u6620\u5c04\u5230\u8fd9\u4e2a\u5b9e\u4f53\u7c7b\u4e0a\uff0c\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package net.biancheng.www.bean;\nimport org.springframework.boot.context.properties.ConfigurationProperties;\nimport org.springframework.stereotype.Component;\nimport java.util.Date;\nimport java.util.List;\nimport java.util.Map;\n\/**\n* \u5c06\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u7684\u6bcf\u4e00\u4e2a\u5c5e\u6027\u7684\u503c\uff0c\u6620\u5c04\u5230\u8fd9\u4e2a\u7ec4\u4ef6\u4e2d\n*\n* @ConfigurationProperties\uff1a\u544a\u8bc9 SpringBoot \u5c06\u672c\u7c7b\u4e2d\u7684\u6240\u6709\u5c5e\u6027\u548c\u914d\u7f6e\u6587\u4ef6\u4e2d\u76f8\u5173\u7684\u914d\u7f6e\u8fdb\u884c\u7ed1\u5b9a\uff1b\n* prefix = \"person\"\uff1a\u914d\u7f6e\u6587\u4ef6\u4e2d\u54ea\u4e2a\u4e0b\u9762\u7684\u6240\u6709\u5c5e\u6027\u8fdb\u884c\u4e00\u4e00\u6620\u5c04\n*\n* \u53ea\u6709\u8fd9\u4e2a\u7ec4\u4ef6\u662f\u5bb9\u5668\u4e2d\u7684\u7ec4\u4ef6\uff0c\u624d\u80fd\u4f7f\u7528\u5bb9\u5668\u63d0\u4f9b\u7684@ConfigurationProperties\u529f\u80fd\uff1b\n*\/\n@Component\n@ConfigurationProperties(prefix = \"person\")\npublic class Person {\n    private String lastName;\n    private Integer age;\n    private Boolean boss;\n    private Date birth;\n    private Map&lt;String, Object> maps;\n    private List&lt;Object> lists;\n    private Dog dog;\n    public Person() {\n    }\n    public String getLastName() {\n        return lastName;\n    }\n    public void setLastName(String lastName) {\n        this.lastName = lastName;\n    }\n    public Integer getAge() {\n        return age;\n    }\n    public void setAge(Integer age) {\n        this.age = age;\n    }\n    public Boolean getBoss() {\n        return boss;\n    }\n    public void setBoss(Boolean boss) {\n        this.boss = boss;\n    }\n    public Date getBirth() {\n        return birth;\n    }\n    public void setBirth(Date birth) {\n        this.birth = birth;\n    }\n    public Map&lt;String, Object> getMaps() {\n        return maps;\n    }\n    public void setMaps(Map&lt;String, Object> maps) {\n        this.maps = maps;\n    }\n    public List&lt;Object> getLists() {\n        return lists;\n    }\n    public void setLists(List&lt;Object> lists) {\n        this.lists = lists;\n    }\n    public Dog getDog() {\n        return dog;\n    }\n    public void setDog(Dog dog) {\n        this.dog = dog;\n    }\n    public Person(String lastName, Integer age, Boolean boss, Date birth, Map&lt;String, Object> maps, List&lt;Object> lists, Dog dog) {\n        this.lastName = lastName;\n        this.age = age;\n        this.boss = boss;\n        this.birth = birth;\n        this.maps = maps;\n        this.lists = lists;\n        this.dog = dog;\n    }\n    @Override\n    public String toString() {\n        return \"Person{\" +\n                \"lastName='\" + lastName + '\\'' +\n                \", age=\" + age +\n                \", boss=\" + boss +\n                \", birth=\" + birth +\n                \", maps=\" + maps +\n                \", lists=\" + lists +\n                \", dog=\" + dog +\n                '}';\n    }\n}\n<\/pre>\n\n\n\n<p>\u6ce8\u610f\uff1a<\/p>\n\n\n\n<ul><li>\u53ea\u6709\u5728\u5bb9\u5668\u4e2d\u7684\u7ec4\u4ef6\uff0c\u624d\u4f1a\u62e5\u6709 SpringBoot \u63d0\u4f9b\u7684\u5f3a\u5927\u529f\u80fd\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u4f7f\u7528 @ConfigurationProperties \u6ce8\u89e3\u8fdb\u884c\u914d\u7f6e\u7ed1\u5b9a\uff0c\u90a3\u4e48\u9996\u5148\u5c31\u8981\u4fdd\u8bc1\u8be5\u5bf9 JavaBean \u5bf9\u8c61\u5728 IoC \u5bb9\u5668\u4e2d\uff0c\u6240\u4ee5\u9700\u8981\u7528\u5230 @Component \u6ce8\u89e3\u6765\u6dfb\u52a0\u7ec4\u4ef6\u5230\u5bb9\u5668\u4e2d\u3002<\/li><li>JavaBean \u4e0a\u4f7f\u7528\u4e86\u6ce8\u89e3 @ConfigurationProperties(prefix = &#8220;person&#8221;) \uff0c\u5b83\u8868\u793a\u5c06\u8fd9\u4e2a JavaBean \u4e2d\u7684\u6240\u6709\u5c5e\u6027\u4e0e\u914d\u7f6e\u6587\u4ef6\u4e2d\u4ee5\u201cperson\u201d\u4e3a\u524d\u7f00\u7684\u914d\u7f6e\u8fdb\u884c\u7ed1\u5b9a\u3002<\/li><\/ul>\n\n\n\n<p>2. \u5728 net.biancheng.www.bean \u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a Dog \u7684 JavaBean\uff0c\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package net.biancheng.www.bean;\npublic class Dog {\n    private String name;\n    private String age;\n    public Dog() {\n    }\n    public Dog(String name, String age) {\n        this.name = name;\n        this.age = age;\n    }\n    public void setName(String name) {\n        this.name = name;\n    }\n    public void setAge(String age) {\n        this.age = age;\n    }\n    public String getName() {\n        return name;\n    }\n    public String getAge() {\n        return age;\n    }\n}\n<\/pre>\n\n\n\n<p>3. \u4fee\u6539&nbsp;HelloController \u7684\u4ee3\u7801\uff0c\u5728\u6d4f\u89c8\u5668\u4e2d\u5c55\u793a\u914d\u7f6e\u6587\u4ef6\u4e2d\u5404\u4e2a\u5c5e\u6027\u503c\uff0c\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package net.biancheng.www.controller;\nimport net.biancheng.www.bean.Person;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Controller;\nimport org.springframework.web.bind.annotation.RequestMapping;\nimport org.springframework.web.bind.annotation.ResponseBody;\n@Controller\npublic class HelloController {\n    @Autowired\n    private Person person;\n    @ResponseBody\n    @RequestMapping(\"\/hello\")\n    public Person hello() {\n        return person;\n    }\n}\n<\/pre>\n\n\n\n<p>4. \u91cd\u542f\u9879\u76ee\uff0c\u4f7f\u7528\u6d4f\u89c8\u5668\u8bbf\u95ee \u201chttp:\/\/localhost:8081\/hello\u201d\uff0c\u7ed3\u679c\u5982\u4e0b\u56fe\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-31-1024x131.png\" alt=\"\" class=\"wp-image-4256\" width=\"571\" height=\"73\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-31-1024x131.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-31-300x38.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-31-768x98.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-31-1536x196.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-31-830x106.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-31-230x29.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-31-350x45.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-31-480x61.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-31.png 1816w\" sizes=\"(max-width: 571px) 100vw, 571px\" \/><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">@Value<\/h4>\n\n\n\n<p>\u5f53\u6211\u4eec\u53ea\u9700\u8981\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u67d0\u4e00\u4e2a\u914d\u7f6e\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7 @Value \u6ce8\u89e3\u83b7\u53d6\u3002<\/p>\n\n\n\n<p>1. \u4ee5 Spring Boot \u9879\u76ee helloworld \u4e3a\u4f8b\uff0c\u4fee\u6539\u5b9e\u4f53\u7c7b Person \u4e2d\u7684\u4ee3\u7801\uff0c\u4f7f\u7528 @Value \u6ce8\u89e3\u8fdb\u884c\u914d\u7f6e\u7ed1\u5b9a\uff0c\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package net.biancheng.www.bean;\nimport org.springframework.beans.factory.annotation.Value;\nimport org.springframework.boot.context.properties.ConfigurationProperties;\nimport org.springframework.stereotype.Component;\nimport java.util.Date;\nimport java.util.List;\nimport java.util.Map;\n@Component\npublic class Person {\n    @Value(\"${person.lastName}\")\n    private String lastName;\n    @Value(\"${person.age}\")\n    private Integer age;\n    @Value(\"${person.boss}\")\n    private Boolean boss;\n    @Value(\"${person.birth}\")\n    private Date birth;\n    private Map&lt;String, Object> maps;\n    private List&lt;Object> lists;\n    private Dog dog;\n    public Person() {\n    }\n    public String getLastName() {\n        return lastName;\n    }\n    public void setLastName(String lastName) {\n        this.lastName = lastName;\n    }\n    public Integer getAge() {\n        return age;\n    }\n    public void setAge(Integer age) {\n        this.age = age;\n    }\n    public Boolean getBoss() {\n        return boss;\n    }\n    public void setBoss(Boolean boss) {\n        this.boss = boss;\n    }\n    public Date getBirth() {\n        return birth;\n    }\n    public void setBirth(Date birth) {\n        this.birth = birth;\n    }\n    public Map&lt;String, Object> getMaps() {\n        return maps;\n    }\n    public void setMaps(Map&lt;String, Object> maps) {\n        this.maps = maps;\n    }\n    public List&lt;Object> getLists() {\n        return lists;\n    }\n    public void setLists(List&lt;Object> lists) {\n        this.lists = lists;\n    }\n    public Dog getDog() {\n        return dog;\n    }\n    public void setDog(Dog dog) {\n        this.dog = dog;\n    }\n    public Person(String lastName, Integer age, Boolean boss, Date birth, Map&lt;String, Object> maps, List&lt;Object> lists, Dog dog) {\n        this.lastName = lastName;\n        this.age = age;\n        this.boss = boss;\n        this.birth = birth;\n        this.maps = maps;\n        this.lists = lists;\n        this.dog = dog;\n    }\n    @Override\n    public String toString() {\n        return \"Person{\" +\n                \"lastName='\" + lastName + '\\'' +\n                \", age=\" + age +\n                \", boss=\" + boss +\n                \", birth=\" + birth +\n                \", maps=\" + maps +\n                \", lists=\" + lists +\n                \", dog=\" + dog +\n                '}';\n    }\n}\n<\/pre>\n\n\n\n<p>2. \u91cd\u542f\u9879\u76ee\uff0c\u4f7f\u7528\u6d4f\u89c8\u5668\u8bbf\u95ee \u201chttp:\/\/localhost:8081\/hello\u201d\uff0c\u7ed3\u679c\u5982\u4e0b\u56fe\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-32-1024x189.png\" alt=\"\" class=\"wp-image-4257\" width=\"604\" height=\"111\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-32-1024x189.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-32-300x55.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-32-768x142.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-32-1536x283.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-32-830x153.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-32-230x42.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-32-350x65.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-32-480x89.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-32.png 1800w\" sizes=\"(max-width: 604px) 100vw, 604px\" \/><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">@Value \u4e0e @ConfigurationProperties \u5bf9\u6bd4<\/h4>\n\n\n\n<p>@Value \u548c @ConfigurationProperties \u6ce8\u89e3\u90fd\u80fd\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u5c5e\u6027\u503c\u5e76\u7ed1\u5b9a\u5230 JavaBean \u4e2d\uff0c\u4f46\u4e24\u8005\u5b58\u5728\u4ee5\u4e0b\u4e0d\u540c\u3002<\/p>\n\n\n\n<p><strong>1. \u4f7f\u7528\u4f4d\u7f6e\u4e0d\u540c<\/strong><\/p>\n\n\n\n<ul><li>@ConfigurationProperties\uff1a\u6807\u6ce8\u5728 JavaBean \u7684\u7c7b\u540d\u4e0a\uff1b<\/li><li>@Value\uff1a\u6807\u6ce8\u5728 JavaBean \u7684\u5c5e\u6027\u4e0a\u3002<\/li><\/ul>\n\n\n\n<p><strong>2. \u529f\u80fd\u4e0d\u540c<\/strong><\/p>\n\n\n\n<ul><li>@ConfigurationProperties\uff1a\u7528\u4e8e\u6279\u91cf\u7ed1\u5b9a\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u914d\u7f6e\uff1b<\/li><li>@Value\uff1a\u53ea\u80fd\u4e00\u4e2a\u4e00\u4e2a\u7684\u6307\u5b9a\u9700\u8981\u7ed1\u5b9a\u7684\u914d\u7f6e\u3002<\/li><\/ul>\n\n\n\n<p><strong>3. \u677e\u6563\u7ed1\u5b9a\u652f\u6301\u4e0d\u540c<\/strong><\/p>\n\n\n\n<p>@ConfigurationProperties\uff1a\u652f\u6301\u677e\u6563\u7ed1\u5b9a\uff08\u677e\u6563\u8bed\u6cd5\uff09\uff0c\u4f8b\u5982\u5b9e\u4f53\u7c7b Person \u4e2d\u6709\u4e00\u4e2a\u5c5e\u6027\u4e3a lastName\uff0c\u90a3\u4e48\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u5c5e\u6027\u540d\u652f\u6301\u4ee5\u4e0b\u5199\u6cd5\uff1a<\/p>\n\n\n\n<ul><li>person.firstName<\/li><li>person.first-name<\/li><li>person.first_name<\/li><li>PERSON_FIRST_NAME<\/li><\/ul>\n\n\n\n<p>&nbsp;<br>@Vaule\uff1a\u4e0d\u652f\u6301\u677e\u6563\u7ed1\u5b9a\u3002<\/p>\n\n\n\n<p><strong>4. SpEL \u652f\u6301\u4e0d\u540c<\/strong><\/p>\n\n\n\n<ul><li>@ConfigurationProperties\uff1a\u4e0d\u652f\u6301 SpEL \u8868\u8fbe\u5f0f;<\/li><li>@Value\uff1a\u652f\u6301 SpEL \u8868\u8fbe\u5f0f\u3002<\/li><\/ul>\n\n\n\n<p><strong>5. \u590d\u6742\u7c7b\u578b\u5c01\u88c5<\/strong><\/p>\n\n\n\n<ul><li>@ConfigurationProperties\uff1a\u652f\u6301\u6240\u6709\u7c7b\u578b\u6570\u636e\u7684\u5c01\u88c5\uff0c\u4f8b\u5982 Map\u3001List\u3001Set\u3001\u4ee5\u53ca\u5bf9\u8c61\u7b49\uff1b<\/li><li>@Value\uff1a\u53ea\u652f\u6301\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u5c01\u88c5\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u3001\u5e03\u5c14\u503c\u3001\u6574\u6570\u7b49\u7c7b\u578b\u3002<\/li><\/ul>\n\n\n\n<p><strong>6. \u5e94\u7528\u573a\u666f\u4e0d\u540c<\/strong><\/p>\n\n\n\n<p>@Value \u548c @ConfigurationProperties \u4e24\u4e2a\u6ce8\u89e3\u4e4b\u95f4\uff0c\u5e76\u6ca1\u6709\u660e\u663e\u7684\u4f18\u52a3\u4e4b\u5206\uff0c\u5b83\u4eec\u53ea\u662f\u9002\u5408\u7684\u5e94\u7528\u573a\u666f\u4e0d\u540c\u800c\u5df2\u3002<\/p>\n\n\n\n<ul><li>\u82e5\u53ea\u662f\u83b7\u53d6\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u67d0\u9879\u503c\uff0c\u5219\u63a8\u8350\u4f7f\u7528 @Value \u6ce8\u89e3\uff1b<\/li><li>\u82e5\u4e13\u95e8\u7f16\u5199\u4e86\u4e00\u4e2a JavaBean \u6765\u548c\u914d\u7f6e\u6587\u4ef6\u8fdb\u884c\u6620\u5c04\uff0c\u5219\u5efa\u8bae\u4f7f\u7528 @ConfigurationProperties \u6ce8\u89e3\u3002<\/li><\/ul>\n\n\n\n<p><br>\u6211\u4eec\u5728\u9009\u7528\u65f6\uff0c\u6839\u636e\u5b9e\u9645\u5e94\u7528\u573a\u666f\u9009\u62e9\u5408\u9002\u7684\u6ce8\u89e3\u80fd\u8fbe\u5230\u4e8b\u534a\u529f\u500d\u7684\u6548\u679c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">@PropertySource\u00a0<\/h4>\n\n\n\n<p>\u5982\u679c\u5c06\u6240\u6709\u7684\u914d\u7f6e\u90fd\u96c6\u4e2d\u5230 application.properties \u6216 application.yml \u4e2d\uff0c\u90a3\u4e48\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u4f1a\u5341\u5206\u7684\u81c3\u80bf\u4e14\u96be\u4ee5\u7ef4\u62a4\uff0c\u56e0\u6b64\u6211\u4eec\u901a\u5e38\u4f1a\u5c06\u4e0e Spring Boot \u65e0\u5173\u7684\u914d\u7f6e\uff08\u4f8b\u5982\u81ea\u5b9a\u4e49\u914d\u7f6e\uff09\u63d0\u53d6\u51fa\u6765\uff0c\u5199\u5728\u4e00\u4e2a\u5355\u72ec\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u5e76\u5728\u5bf9\u5e94\u7684 JavaBean \u4e0a\u4f7f\u7528 @PropertySource \u6ce8\u89e3\u6307\u5411\u8be5\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n\n\n\n<p>1. \u4ee5 helloworld \u4e3a\u4f8b\uff0c\u5c06\u4e0e person \u76f8\u5173\u7684\u81ea\u5b9a\u4e49\u914d\u7f6e\u79fb\u52a8\u5230 src\/main\/resources \u4e0b\u7684 person.properties \u4e2d\uff08\u6ce8\u610f\uff0c\u5fc5\u987b\u628a application.properties \u6216 application.yml \u4e2d\u7684\u76f8\u5173\u914d\u7f6e\u5220\u9664\uff09\uff0c\u5982\u4e0b\u56fe\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-33.png\" alt=\"\" class=\"wp-image-4259\" width=\"457\" height=\"533\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-33.png 854w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-33-257x300.png 257w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-33-768x897.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-33-830x970.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-33-230x269.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-33-350x409.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-33-480x561.png 480w\" sizes=\"(max-width: 457px) 100vw, 457px\" \/><\/figure><\/div>\n\n\n\n<p>person.properties \u7684\u914d\u7f6e\u5982\u4e0b\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-34-1024x266.png\" alt=\"\" class=\"wp-image-4260\" width=\"616\" height=\"160\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-34-1024x266.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-34-300x78.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-34-768x199.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-34-1536x399.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-34-830x215.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-34-230x60.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-34-350x91.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-34-480x125.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-34.png 1796w\" sizes=\"(max-width: 616px) 100vw, 616px\" \/><\/figure><\/div>\n\n\n\n<p>2. \u5728 Person \u4f7f\u7528 @PropertySource \u6ce8\u89e3\u6307\u5411 person.properties\uff0c\u4ee3\u7801\u5982\u4e0b\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package net.biancheng.www.bean;\nimport org.springframework.beans.factory.annotation.Value;\nimport org.springframework.boot.context.properties.ConfigurationProperties;\nimport org.springframework.context.annotation.PropertySource;\nimport org.springframework.stereotype.Component;\nimport java.util.Date;\nimport java.util.List;\nimport java.util.Map;\n@PropertySource(value = \"classpath:person.properties\")\/\/\u6307\u5411\u5bf9\u5e94\u7684\u914d\u7f6e\u6587\u4ef6\n@Component\n@ConfigurationProperties(prefix = \"person\")\npublic class Person {\n    private String lastName;\n    private Integer age;\n    private Boolean boss;\n    private Date birth;\n    private Map&lt;String, Object> maps;\n    private List&lt;Object> lists;\n    private Dog dog;\n    public Person() {\n    }\n    public String getLastName() {\n        return lastName;\n    }\n    public void setLastName(String lastName) {\n        this.lastName = lastName;\n    }\n    public Integer getAge() {\n        return age;\n    }\n    public void setAge(Integer age) {\n        this.age = age;\n    }\n    public Boolean getBoss() {\n        return boss;\n    }\n    public void setBoss(Boolean boss) {\n        this.boss = boss;\n    }\n    public Date getBirth() {\n        return birth;\n    }\n    public void setBirth(Date birth) {\n        this.birth = birth;\n    }\n    public Map&lt;String, Object> getMaps() {\n        return maps;\n    }\n    public void setMaps(Map&lt;String, Object> maps) {\n        this.maps = maps;\n    }\n    public List&lt;Object> getLists() {\n        return lists;\n    }\n    public void setLists(List&lt;Object> lists) {\n        this.lists = lists;\n    }\n    public Dog getDog() {\n        return dog;\n    }\n    public void setDog(Dog dog) {\n        this.dog = dog;\n    }\n    public Person(String lastName, Integer age, Boolean boss, Date birth, Map&lt;String, Object> maps, List&lt;Object> lists, Dog dog) {\n        this.lastName = lastName;\n        this.age = age;\n        this.boss = boss;\n        this.birth = birth;\n        this.maps = maps;\n        this.lists = lists;\n        this.dog = dog;\n    }\n    @Override\n    public String toString() {\n        return \"Person{\" +\n                \"lastName='\" + lastName + '\\'' +\n                \", age=\" + age +\n                \", boss=\" + boss +\n                \", birth=\" + birth +\n                \", maps=\" + maps +\n                \", lists=\" + lists +\n                \", dog=\" + dog +\n                '}';\n    }\n}\n<\/pre>\n\n\n\n<p>3. \u91cd\u542f\u9879\u76ee\uff0c\u4f7f\u7528\u6d4f\u89c8\u5668\u8bbf\u95ee \u201chttp:\/\/localhost:8081\/hello\u201d\uff0c\u7ed3\u679c\u5982\u4e0b\u56fe\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2021\/12\/image-35-1024x144.png\" alt=\"\" class=\"wp-image-4262\" width=\"609\" height=\"86\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-35-1024x144.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-35-300x42.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-35-768x108.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-35-1536x217.png 1536w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-35-830x117.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-35-230x32.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-35-350x49.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-35-480x68.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2021\/12\/image-35.png 1802w\" sizes=\"(max-width: 609px) 100vw, 609px\" \/><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6240\u8c13\u201c\u914d\u7f6e\u7ed1\u5b9a\u201d\u5c31\u662f\u628a\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u503c\u4e0e JavaBean \u4e2d\u5bf9\u5e94\u7684\u5c5e\u6027\u8fdb\u884c\u7ed1\u5b9a\u3002\u901a\u5e38\uff0c\u6211\u4eec\u4f1a\u628a\u4e00\u4e9b\u914d\u7f6e\u4fe1\u606f\uff08\u4f8b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4252"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4252"}],"version-history":[{"count":5,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4252\/revisions"}],"predecessor-version":[{"id":4263,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4252\/revisions\/4263"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}