 
		 
		{"id":368,"date":"2021-01-06T17:15:34","date_gmt":"2021-01-06T09:15:34","guid":{"rendered":"http:\/\/www.softeng.cn\/?p=368"},"modified":"2021-01-06T17:19:42","modified_gmt":"2021-01-06T09:19:42","slug":"%e3%80%8ab%e7%ab%99-spring%e6%ba%90%e7%a0%81%e8%a7%a3%e6%9e%90%e3%80%8b%e5%ad%a6%e4%b9%a0%e7%ac%94%e8%ae%b0%ef%bc%88%e4%b8%89%ef%bc%89-%e5%b1%9e%e6%80%a7%e8%b5%8b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/www.softeng.cn\/?p=368","title":{"rendered":"\u300aB\u7ad9-Spring\u6e90\u7801\u89e3\u6790\u300b\u5b66\u4e60\u7b14\u8bb0\uff08\u4e09\uff09\u2014\u2014\u5c5e\u6027\u8d4b\u503c"},"content":{"rendered":"<p>\u89c6\u9891\u5730\u5740\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.bilibili.com\/video\/BV1oW41167AV\">https:\/\/www.bilibili.com\/video\/BV1oW41167AV<\/a><br \/>\n\u5bf9\u5e94\u4ee3\u7801Git\u5e93\u5730\u5740\uff1a<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/github.com\/whh306318848\/spring-annotation.git\">https:\/\/github.com\/whh306318848\/spring-annotation.git<\/a><\/p>\n<ol>\n<li>\u4f7f\u7528@Value\u6ce8\u89e3\u5bf9Bean\u7684\u5c5e\u6027\u8fdb\u884c\u8d4b\u503c\uff1a<br \/>\n1.1. \u4f7f\u7528\u57fa\u672c\u6570\u503c\u8fdb\u884c\u8d4b\u503c<br \/>\n1.2. \u53ef\u4ee5\u4f7f\u7528Spring\u8868\u8fbe\u5f0fSpEL\uff08\u5373#{}\uff09\u8fdb\u884c\u8d4b\u503c<br \/>\n1.3. \u53ef\u4ee5\u4f7f\u7528${}\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u6216\u73af\u5883\u53d8\u91cf\u4e2d\u7684\u503c<\/li>\n<li>\u5982\u679c\u662f\u4f7f\u7528xml\u914d\u7f6e\u6587\u4ef6\u4e3aBean\u8d4b\u503c\u7684\u65b9\u5f0f\uff0c\u5728xml\u6587\u4ef6\u4e2d\u5f15\u7528\u5916\u90e8\u914d\u7f6e\u6587\u4ef6\u9700\u8981\u5148\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u52a0\u5165&#8221;context&#8221;\u540d\u5b57\u7a7a\u95f4\uff0c\u7136\u540e\u4f7f\u7528&#8221;context:property-placeholder&#8221;\u6807\u7b7e\u7684location\u5c5e\u6027\u6307\u5b9a\u8981\u5bfc\u5165\u7684\u5916\u90e8\u914d\u7f6e\u6587\u4ef6<\/li>\n<\/ol>\n<pre data-language=XML><code class=\"language-markup line-numbers\">&lt;context:property-placeholder location=\"classpath:person.properties\" \/&gt;\n<\/code><\/pre>\n<ol start=\"3\">\n<li>\u4f7f\u7528\u6ce8\u89e3\u65b9\u5f0f\u7684\u8fc7\u7a0b\u4e0e\u4f7f\u7528xml\u914d\u7f6e\u6587\u4ef6\u7c7b\u4f3c<br \/>\n3.1. \u9996\u5148\uff0c\u9700\u8981\u5728\u914d\u7f6e\u7c7b\u4e0a\u901a\u8fc7@PropertySource\u6ce8\u89e3\u5bfc\u5165\u914d\u7f6e\u6587\u4ef6\uff0c\u8be5\u6ce8\u89e3\u7684value\u5c5e\u6027\u7528\u4e8e\u6307\u5b9a\u914d\u7f6e\u6587\u4ef6\u7684\u8def\u5f84\uff0c\u662f\u4e00\u4e2aString\u6570\u7ec4\uff0c\u53ef\u4ee5\u4ece\u7c7b\u8def\u5f84\uff08classpath\uff09\u5f00\u59cb\uff0c\u4e5f\u53ef\u4ee5\u4ece\u6587\u4ef6\u8def\u5f84\uff08file\uff09\u5f00\u59cb<br \/>\n3.2. \u7136\u540e\uff0c\u5728Bean\u7c7b\u4e2d\u4f7f\u7528${}\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u503c<\/li>\n<\/ol>\n<pre><code class=\"language-java line-numbers\">\/\/ \u4f7f\u7528@PropertySource\u8bfb\u53d6\u5916\u90e8\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684key\/value\u4fdd\u5b58\u5230\u8fd0\u884c\u7684\u73af\u5883\u53d8\u91cf\u4e2d\uff0c\u52a0\u8f7d\u5b8c\u5916\u90e8\u7684\u914d\u7f6e\u6587\u4ef6\u4ee5\u540e\uff0c\u4f7f\u7528${}\u53bb\u9664\u914d\u7f6e\u6587\u4ef6\u7684\u503c\n@PropertySource(value = {\"classpath:\/person.properties\"})\n@Configuration\npublic class MainConfigOfPropertyValues {\n\n    @Bean\n    public Person person() {\n        return new Person();\n    }\n}\n<\/code><\/pre>\n<pre><code class=\"language-java line-numbers\">public class Person {\n    \/\/ \u4f7f\u7528@Value\u8d4b\u503c\uff1a\n    \/\/ 1\u3001\u57fa\u672c\u6570\u503c\n    \/\/ 2\u3001\u53ef\u4ee5\u5199Spring\u8868\u8fbe\u5f0fSpEL\uff1a#{}\n    \/\/ 3\u3001\u53ef\u4ee5\u4f7f\u7528<span class=\"katex math inline\">{}\u53d6\u914d\u7f6e\u6587\u4ef6\u3010.properties\u3011\u4e2d\u7684\u503c\uff08\u5728\u8fd0\u884c\u73af\u5883\u53d8\u91cf\u4e2d\u7684\u503c\uff09\n    @Value(\"\u5f20\u4e09\")\n    private String name;\n    @Value(\"#{20-2}\")\n    private Integer age;\n    @Value(\"<\/span>{person.nickName}\")\n    private String nickName;\n\n    public Person(String name, Integer age) {\n        this.name = name;\n        this.age = age;\n    }\n\n    public Person() {\n    }\n\n    public String getName() {\n        return name;\n    }\n\n    public void setName(String name) {\n        this.name = name;\n    }\n\n    public Integer getAge() {\n        return age;\n    }\n\n    public void setAge(Integer age) {\n        this.age = age;\n    }\n\n    public String getNickName() {\n        return nickName;\n    }\n\n    public void setNickName(String nickName) {\n        this.nickName = nickName;\n    }\n\n    @Override\n    public String toString() {\n        return \"Person{\" +\n                \"name='\" + name + '\\'' +\n                \", age=\" + age +\n                \", nickName='\" + nickName + '\\'' +\n                '}';\n    }\n}\n<\/code><\/pre>\n<p>3.3\u3002 @PropertySource\u6ce8\u89e3\u7684\u4f5c\u7528\u662f\u8bfb\u53d6\u5916\u90e8\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684key\/value\u4fdd\u5b58\u5230\u8fd0\u884c\u7684\u73af\u5883\u53d8\u91cf\u4e2d\uff0c\u6240\u4ee5\u9664\u4e86\u5728Bean\u7c7b\u4e2d\u4f7f\u7528${}\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u7684\u503c\u4e4b\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528ConfigurableEnvironment\u7684getProperty\u65b9\u6cd5\u8bfb\u53d6<\/p>\n<pre><code class=\"language-java line-numbers\">ConfigurableEnvironment environment = applicationContext.getEnvironment();\nString property = environment.getProperty(\"person.nickName\");\nSystem.out.println(property);\n<\/code><\/pre>\n<ol start=\"4\">\n<li>@PropertySource\u6ce8\u89e3\u662f\u4e00\u4e2a\u53ef\u91cd\u590d\u7ec4\u4ef6\uff0c\u5982\u679c\u9700\u8981\u5f15\u5165\u591a\u4e2a\u914d\u7f6e\u6587\u4ef6\uff0c\u6709\u4e24\u79cd\u65b9\u6cd5\uff0c\u5206\u522b\u662f\uff1a<br \/>\n4.1. \u4f7f\u7528@PropertySource\u6ce8\u89e3\uff0c\u5728\u5176value\u5c5e\u6027\u4e2d\u586b\u5165\u914d\u7f6e\u6587\u4ef6\u8def\u5f84\u6570\u7ec4\uff1b<br \/>\n4.2. \u4f7f\u7528@PropertySources\u6ce8\u89e3\uff0c\u5176\u503c\u662f@PropertySource\u6570\u7ec4<\/li>\n<\/ol>\n  \n  \n  ","protected":false},"excerpt":{"rendered":"<p>\u89c6\u9891\u5730\u5740\uff1ahttps:\/\/www.bilibili.com\/video\/BV1oW41167AV \u5bf9\u5e94\u4ee3\u7801Gi&hellip;<a href=\"https:\/\/www.softeng.cn\/?p=368\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">\u300aB\u7ad9-Spring\u6e90\u7801\u89e3\u6790\u300b\u5b66\u4e60\u7b14\u8bb0\uff08\u4e09\uff09\u2014\u2014\u5c5e\u6027\u8d4b\u503c<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[35,40,36],"class_list":["post-368","post","type-post","status-publish","format-standard","hentry","category-32","tag-spring","tag-40","tag-36"],"_links":{"self":[{"href":"https:\/\/www.softeng.cn\/index.php?rest_route=\/wp\/v2\/posts\/368"}],"collection":[{"href":"https:\/\/www.softeng.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.softeng.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.softeng.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.softeng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=368"}],"version-history":[{"count":2,"href":"https:\/\/www.softeng.cn\/index.php?rest_route=\/wp\/v2\/posts\/368\/revisions"}],"predecessor-version":[{"id":372,"href":"https:\/\/www.softeng.cn\/index.php?rest_route=\/wp\/v2\/posts\/368\/revisions\/372"}],"wp:attachment":[{"href":"https:\/\/www.softeng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.softeng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.softeng.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}