{"id":1386,"date":"2016-07-09T17:07:32","date_gmt":"2016-07-09T09:07:32","guid":{"rendered":"http:\/\/coderbee.net\/?p=1386"},"modified":"2016-07-09T17:08:47","modified_gmt":"2016-07-09T09:08:47","slug":"spring-%e5%af%bc%e5%85%a5%e8%b5%84%e6%ba%90%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/java\/20160709\/1386","title":{"rendered":"Spring \u5bfc\u5165\u8d44\u6e90\u6587\u4ef6"},"content":{"rendered":"<p>Spring \u6709\u591a\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5bfc\u5165\u8d44\u6e90\u6587\u4ef6\uff0c\u53ef\u4ee5\u628a\u8fd9\u4e9b\u5c5e\u6027\u6587\u4ef6\u91cc\u7684\u5c5e\u6027\u503c\u6ce8\u5165\u5230 bean \u7684\u5c5e\u6027\u4e0a\u3002<\/p>\n<h2>1. \u901a\u8fc7 <code>&lt;context:property-placeholder&gt;<\/code> \u6807\u7b7e\u5bfc\u5165<\/h2>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u5bfc\u5165\u7684\u6240\u6709\u5c5e\u6027\u5728\u540c\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\u4e0b\uff0c\u5982\u679c\u591a\u4e2a\u5c5e\u6027\u6587\u4ef6\u91cc\u6709\u76f8\u540c\u7684\u5c5e\u6027\u540d\uff0c\u4ee5\u5148\u5bfc\u5165\u7684\u5c5e\u6027\u6587\u4ef6\u7684\u5c5e\u6027\u4e3a\u51c6\uff0c\u4e0d\u4f1a\u51fa\u73b0\u8986\u76d6\u3002\u53ef\u4ee5\u901a\u8fc7 <code>@Value(\"${propName}\")<\/code> \u7684\u65b9\u5f0f\u6ce8\u5165\u5230 bean \u7684\u5c5e\u6027\u4e0a\u3002<\/p>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u80fd\u5bf9\u5c5e\u6027\u6587\u4ef6\u6307\u5b9a id\uff0c\u6ca1\u6cd5\u5f15\u7528\u6307\u5b9a\u5c5e\u6027\u6587\u4ef6\u7684\u5c5e\u6027\uff0c\u5982\u679c\u6709\u540c\u540d\u7684\u5c5e\u6027\uff0c\u5f15\u7528\u5230\u7684\u603b\u662f\u7b2c\u4e00\u4e2a\u5bfc\u5165\u7684\u5c5e\u6027\u3002<\/p>\n<p>\u914d\u7f6e\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"xml\">&lt;!-- \u5bfc\u5165\u5916\u90e8\u7684\u8d44\u6e90\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7 @Value(\"${propName}\") \u7684\u65b9\u5f0f\u6ce8\u5165\u5230 bean \u7684\u5c5e\u6027\u4e0a \u3002 --&gt;\n&lt;context:property-placeholder\n    location=\"classpath:\/learn\/properties\/1.properties\"\n    ignore-unresolvable=\"true\" \/&gt;\n&lt;!-- \u8981\u4f7f\u7528 context:property-placeholder \u5bfc\u5165\u591a\u4e2a\u8d44\u6e90\u6587\u4ef6\uff0c\u5fc5\u987b\u6307\u5b9a ignore-unresolvable=\"true\" --&gt;\n&lt;!-- \u591a\u4e2a\u5c5e\u6027\u6587\u4ef6\u4e2d\uff0c\u5c5e\u6027\u540d\u76f8\u540c\u7684\uff0c\u4ee5\u7b2c\u4e00\u4e2a\u52a0\u8f7d\u7684\u4e3a\u51c6\uff0c\u4e0d\u4f1a\u51fa\u73b0\u540e\u9762\u52a0\u8f7d\u7684\u8986\u76d6\u524d\u9762\u7684 --&gt;\n&lt;context:property-placeholder location=\"learn\/properties\/2.properties\"\n    ignore-unresolvable=\"true\" \/&gt;\n<\/code><\/pre>\n<p><!--more--><\/p>\n<h2>2. \u901a\u8fc7 <code>&lt;util:properties&gt;<\/code> \u6807\u7b7e\u5bfc\u5165<\/h2>\n<p><code>&lt;util:properties&gt;<\/code> \u6807\u7b7e\u5bf9\u5e94\u7684\u662f\u4e00\u4e2a <code>java.util.Properties<\/code> \u5b9e\u4f8b\uff0c\u53ef\u4ee5\u6307\u5b9a bean \u7684 id\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5f15\u7528\u67d0\u4e2a\u5c5e\u6027\u6587\u4ef6\u7684\u5c5e\u6027\uff0c\u800c\u4e0d\u7ba1\u662f\u5426\u6709\u591a\u4e2a\u5c5e\u6027\u6709\u540c\u540d\u7684\u5c5e\u6027\u3002<\/p>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u5bfc\u5165\u7684\uff0c\u6ce8\u5165\u5230 bean \u7684\u5c5e\u6027\u4e0a\uff0c\u9700\u8981\u7528 Spring \u7684\u8868\u8fbe\u5f0f\u8bed\u8a00\uff0c\u5f62\u5982 <code>@Value(\"#{propertiesBeanId['propName']}\")<\/code>\u3002<\/p>\n<pre><code class=\"xml\">&lt;!-- \u8fd9\u79cd\u52a0\u8f7d\u65b9\u5f0f\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u901a\u8fc7 @Value \u6ce8\u89e3\u8fdb\u884c\u6ce8\u5165, \u53ef\u4ee5\u5c06\u914d\u7f6e\u6574\u4f53\u8d4b\u7ed9 Properties \u7c7b\u578b\u7684\u7c7b\u53d8\u91cf --&gt;\n&lt;!-- &lt;util:properties\/&gt; \u6807\u7b7e\u53ea\u80fd\u52a0\u8f7d\u4e00\u4e2a\u6587\u4ef6\uff0c\u5f53\u591a\u4e2a\u5c5e\u6027\u6587\u4ef6\u9700\u8981\u88ab\u52a0\u8f7d\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2a\u8be5\u6807\u7b7e --&gt;\n&lt;util:properties id=\"remoteSettings\"\n    location=\"classpath:\/learn\/properties\/resources.properties\" \/&gt;\n\n&lt;!-- \u914d\u7f6e\u591a\u4e2a\u5c5e\u6027\u6587\u4ef6 --&gt;\n&lt;bean id=\"settings\"\n    class=\"org.springframework.beans.factory.config.PropertiesFactoryBean\"&gt;\n    &lt;property name=\"locations\"&gt;\n        &lt;list&gt;\n            &lt;value&gt;classpath:\/learn\/properties\/resources.properties&lt;\/value&gt;\n            &lt;value&gt;classpath:\/learn\/properties\/more.properties&lt;\/value&gt;\n            &lt;!-- &lt;value&gt;file:\/path\/to\/properties\/file&lt;\/value&gt; --&gt;\n        &lt;\/list&gt;\n    &lt;\/property&gt;\n&lt;\/bean&gt;\n<\/code><\/pre>\n<h2>3. \u6ce8\u5165\u5c5e\u6027\u5230 bean \u7684\u53d8\u91cf\u4e0a<\/h2>\n<p>\u5bf9\u4e8e\u901a\u8fc7 <code>&lt;util:properties&gt;<\/code> \u6807\u7b7e\u5bfc\u5165\u7684\u914d\u7f6e\uff0c\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u6574\u4f53\u6ce8\u5165\u5230 <code>java.util.Properties<\/code> \u7c7b\u578b\u7684\u53d8\u91cf\u4e0a\u3002<\/p>\n<pre><code class=\"java\">public class Resources {\n    @Value(\"#{remoteSettings['upstream.host']}\")\n    private String host;\n\n    @Value(\"#{remoteSettings['upstream.port']}\")\n    private int port;\n\n    @Value(\"#{remoteSettings}\")\n    private Properties remoteSettings;\n\n    @Autowired\n    @Qualifier(\"settings\")\n    private Properties settings;\n\n    @Value(\"${1_properties_prop1}\")\n    private String prop1_1;\n\n    @Value(\"${1_properties_prop2}\")\n    private String prop1_2;\n\n    @Value(\"${2_properties_prop2}\")\n    private String prop2_2;\n\n    public String getProp2_2() {\n        return prop2_2;\n    }\n\n    public String getProp1_1() {\n        return prop1_1;\n    }\n\n    public String getProp1_2() {\n        return prop1_2;\n    }\n\n    public String getHost() {\n        return host;\n    }\n\n    public int getPort() {\n        return port;\n    }\n\n    public String getPropValue(String propName) {\n        return remoteSettings.getProperty(propName);\n    }\n\n    public String getMorePropValue(String propName) {\n        return settings.getProperty(propName);\n    }\n}\n<\/code><\/pre>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring \u6709\u591a\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5bfc\u5165\u8d44\u6e90\u6587\u4ef6\uff0c\u53ef\u4ee5\u628a\u8fd9\u4e9b\u5c5e\u6027\u6587\u4ef6\u91cc\u7684\u5c5e\u6027\u503c\u6ce8\u5165\u5230 b &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/java\/20160709\/1386\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[172,248],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1386"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1386"}],"version-history":[{"count":2,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1386\/revisions"}],"predecessor-version":[{"id":1388,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1386\/revisions\/1388"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}