{"id":2035,"date":"2017-10-13T16:43:59","date_gmt":"2017-10-13T12:18:16","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2035"},"modified":"2017-10-13T16:46:35","modified_gmt":"2017-10-13T08:46:35","slug":"%e9%9d%a0%e6%8e%92%e7%85%a7%e7%89%87%e5%92%8c%e6%8b%8d%e8%a7%86%e9%a2%91%e5%81%9a%e5%88%b0%e6%9c%88%e5%85%a510%e4%b8%87-18","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2035","title":{"rendered":"Spring\u4e2dPropertyPlaceholderConfigurer\u7c7b\u7684\u7528\u6cd5\u6559\u7a0b"},"content":{"rendered":"<p>\n\t\u6211\u4eec\u90fd\u77e5\u9053&nbsp;PropertyPlaceholderConfigurer \u662f\u7528\u6765\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u7684\u3002\u4e00\u822c\u6211\u4eec\u53ea\u9700\u7b80\u5355\u7684\u914d\u7f6e\u5b83\u5373\u53ef\uff0c\u5f88\u5c11\u6709\u5bf9\u5b83\u8fdb\u884c\u91cd\u65b0\u6539\u9020\u7684\u3002\u672c\u6587\u5c06\u5217\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff0c\u5982\u4f55\u901a\u8fc7&nbsp;PropertyPlaceholderConfigurer \u6765\u8bfb\u53d6\u52a0\u89e3\u5bc6\u7684\u914d\u7f6e\u4fe1\u606f\u3002\n<\/p>\n<p>\n\tPropertyPlaceholderConfigurer\u662f\u4e2abean\u5de5\u5382\u540e\u7f6e\u5904\u7406\u5668\u7684\u5b9e\u73b0\uff0c\u4e5f\u5c31\u662f BeanFactoryPostProcessor\u63a5\u53e3\u7684\u4e00\u4e2a\u5b9e\u73b0\u3002PropertyPlaceholderConfigurer\u53ef\u4ee5\u5c06\u4e0a\u4e0b\u6587\uff08\u914d\u7f6e\u6587 \u4ef6\uff09\u4e2d\u7684\u5c5e\u6027\u503c\u653e\u5728\u53e6\u4e00\u4e2a\u5355\u72ec\u7684\u6807\u51c6java Properties\u6587\u4ef6\u4e2d\u53bb\u3002\u5728XML\u6587\u4ef6\u4e2d\u7528${key}\u66ff\u6362\u6307\u5b9a\u7684properties\u6587\u4ef6\u4e2d\u7684\u503c\u3002\u8fd9\u6837\u7684\u8bdd\uff0c\u53ea\u9700\u8981\u5bf9properties\u6587\u4ef6\u8fdb \u884c\u4fee\u6539\uff0c\u800c\u4e0d\u7528\u5bf9xml\u914d\u7f6e\u6587\u4ef6\u8fdb\u884c\u4fee\u6539\u3002\n<\/p>\n<p>\n\t\u901a\u5e38\u6211\u7684\u914d\u7f6e\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;bean id=&quot;propertyConfigurer&quot; class=&quot;org.springframework.beans.factory.config.PropertyPlaceholderConfigurer&quot;&gt;\r\n\t&lt;property name=&quot;locations&quot;&gt;\r\n\t\t&lt;list&gt;\r\n\t\t\t&lt;value&gt;classpath:jdbc.properties&lt;\/value&gt;\r\n\t\t\t&lt;value&gt;classpath:redis.properties&lt;\/value&gt;\r\n\t\t&lt;\/list&gt;\r\n\t&lt;\/property&gt;\r\n\t&lt;property name=&quot;fileEncoding&quot;&gt;\r\n\t\t&lt;value&gt;UTF-8&lt;\/value&gt;\r\n\t&lt;\/property&gt;\r\n&lt;\/bean&gt;<\/pre>\n<p>\n\t\u73b0\u5728\u60f3\u4e00\u4e0b\uff0c\u5982\u679c\u6211\u4eec\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u5bf9\u67d0\u4e9b\u5c5e\u6027\u8fdb\u884c\u4e86\u52a0\u5bc6\uff0c\u8fd9\u65f6\u518d\u4f7f\u7528&nbsp;PropertyPlaceholderConfigurer \u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u6211\u4eec\u60f3\u8981\u52a0\u5bc6\u524d\u7684\u5185\u5bb9\u8be5\u600e\u4e48\u529e\uff1f\n<\/p>\n<p>\n\t\u7b54\u6848\u5c31\u662f\u91cd\u5199&nbsp;PropertyPlaceholderConfigurer\u3002\u9664\u4e86\u6570\u636e\u5e93\u7684\u914d\u7f6e\u4fe1\u606f\u6211\u4eec\u653e\u5728\u914d\u7f6e\u6587\u4ef6\uff0c\u7136\u540e\u53ef\u4ee5\u901a\u8fc7 druid \u8fdb\u884c\u52a0\u89e3\u5bc6\u3002\u4f46\u662f\u914d\u7f6e\u7684\u90ae\u7bb1\u4fe1\u606f\u5462\uff1f\n<\/p>\n<p>\n\t\u8fd9\u65f6\u91cd\u5199\u5b83\u5c31\u663e\u5f97\u5f88\u6709\u5fc5\u8981\u3002PropertyPlaceholderConfigurer\u8d77\u7684\u4f5c\u7528\u5c31\u662f\u5c06\u5360\u4f4d\u7b26\u6307\u5411\u7684\u6570\u636e\u5e93\u914d\u7f6e\u4fe1\u606f\u653e\u5728bean\u4e2d\u5b9a\u4e49\u7684\u5de5\u5177\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u6765\u770b\u4e00\u4e2a\u901a\u8fc7 PropertyPlaceholderConfigurer\u8bfb\u53d6\u52a0\u89e3\u5bc6\u914d\u7f6e\u6587\u4ef6\u7684\u6848\u4f8b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npackage com.xttblog.plugin;\r\nimport com.zheng.common.util.AESUtil;\r\nimport org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;\r\n\/\/\u652f\u6301\u52a0\u5bc6\u914d\u7f6e\u6587\u4ef6\u63d2\u4ef6\r\npublic class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {\r\n\tprivate String[] propertyNames = {\r\n\t\t&quot;master.jdbc.password&quot;, &quot;slave.jdbc.password&quot;, &quot;generator.jdbc.password&quot;, &quot;master.redis.password&quot;\r\n\t};\r\n\t\/\/\u89e3\u5bc6\u6307\u5b9apropertyName\u7684\u52a0\u5bc6\u5c5e\u6027\u503c\r\n\t@Override\r\n\tprotected String convertProperty(String propertyName, String propertyValue) {\r\n\t\tfor (String p : propertyNames) {\r\n\t\t\tif (p.equalsIgnoreCase(propertyName)) {\r\n\t\t\t\treturn AESUtil.AESDecode(propertyValue);\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn super.convertProperty(propertyName, propertyValue);\r\n\t}\r\n}<\/pre>\n<p>\n\t\u6211\u4eec\u53ea\u9700\u91cd\u5199 PropertyPlaceholderConfigurer \u7c7b\u7684&nbsp;convertProperty \u65b9\u6cd5\u5373\u53ef\uff0c\u7136\u540e\u5728\u8be5\u65b9\u6cd5\u4e2d\u5b9e\u73b0\u89e3\u5bc6\u5de5\u4f5c\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=2035\">Spring\u4e2dPropertyPlaceholderConfigurer\u7c7b\u7684\u7528\u6cd5\u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u90fd\u77e5\u9053&nbsp;PropertyPlaceholderConfigurer \u662f\u7528\u6765\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u7684\u3002\u4e00\u822c\u6211 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[69,551],"class_list":["post-2035","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-spring"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2035"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2035\/revisions"}],"predecessor-version":[{"id":2158,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2035\/revisions\/2158"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}