编程小贴士

给你的编程提供小点子


Spring如何使用占位符

一个项目肯定会多少有些配置项,例如jdbc的一些配置,Spring的占位符,简化了对配置文件的读取,用起来极其方便。

1、假设你的配置项如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
jdbc.url=jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF8
jdbc.username=user
jdbc.password=password
jdbc.validationQuery=select 1
jdbc.maxActive=20
jdbc.initialSize=1
jdbc.minIdle=1
jdbc.maxWait=60000
jdbc.timeBetweenEvictionRunsMillis=3000
jdbc.minEvictableIdleTimeMillis=300000
jdbc.testWhileIdle=true
jdbc.testOnReturn=false
jdbc.testOnBorrow=false
jdbc.defaultAutoCommit=true

写在配置文件config.properties中
2、你只要在Spring配置文件中配上如下bean

1
2
3
<bean id="propertyConfigurer">
<property name="location" value="classpath:config.properties"/>
</bean>

3、就可以在spring中自由注入你的配置项了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<bean abstract="true">
<property name="driverClassName"  value="${jdbc.driverClassName}"/>
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${jdbc.maxActive}" />
<property name="initialSize" value="${jdbc.initialSize}" />
<property name="maxWait" value="${jdbc.maxWait}" />
<property name="minIdle" value="${jdbc.minIdle}" />
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />
<property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" />
<property name="validationQuery" value="${jdbc.validationQuery}" />
<property name="testWhileIdle" value="${jdbc.testWhileIdle}" />
<property name="testOnBorrow" value="${jdbc.testOnBorrow}" />
<property name="testOnReturn" value="${jdbc.testOnReturn}" />
<property name="defaultAutoCommit" value="${jdbc.defaultAutoCommit}"/>
</bean>

4、如果你有些配置项是要加密的,在使用的时候需要解密。
这时你可以继承PropertyPlaceholderConfigurer,重写它的convertPropertyValue或者convertProperty方法进行解密

1
2
3
4
5
6
7
8
9
10
11
@Override
protected String convertPropertyValue(String originalValue)
{
return 解密(originalValue);
}
 
@Override
protected String convertProperty(String propertyName, String propertyValue)
{
return 解密(originalValue);
}

然后把id=”propertyConfigurer”的class换成你自己的class。
这样就可以在配置文件中按你的格式配置密文了。

转自 http://zhaoyanblog.com/archives/194.html

99 Responses to “ Spring如何使用占位符 ”

  1. suba me说道:

    UiBz6W me profite et quoi tokyo pas va changer que avaient ete rabattus

  2. girl drawing说道:

    Very good write-up. I certainly appreciate this site. Stick with it!

  3. Lung Health说道:

    This very blog is without a doubt educating as well as informative. I have discovered helluva helpful stuff out of this amazing blog. I ad love to go back every once in a while. Cheers!

  4. Thank you, I ave recently been searching for info about this topic for ages and yours is the best I ave discovered till now. But, what about the bottom line? Are you sure about the source?

  5. Recently, I did not give plenty of consideration to leaving suggestions on weblog web page posts and have positioned comments even significantly much less.

  6. Oluwadamilare说道:

    Of course, what a magnificent site and instructive posts, I will bookmark your site.Best Regards!

  7. regarding this website and at the moment this time I am

  8. Thanks a lot for the blog post.Much thanks again. Keep writing.

  9. The Silent Shard This will likely almost certainly be quite handy for some of your respective positions I decide to you should not only with my website but

  10. You have brought up a very great details , appreciate it for the post.

  11. You made some first rate factors there. I regarded on the web for the problem and located most people will associate with along with your website.

  12. Woking taxi说道:

    It’а†s really a great and helpful piece of information. I’а†m satisfied that you just shared this useful information with us. Please stay us informed like this. Thanks for sharing.

  13. gia sat thep说道:

    This is one awesome article. Really Cool.

  14. Yes. It should get the job done. If it doesn at send us an email.

  15. Very neat post.Thanks Again. Keep writing.

  16. Very good post !!! I certainly love this website, keep on it.Take care

  17. Really enjoyed this blog post.Really thank you! Fantastic.

  18. More hints说道:

    That is a good tip especially to those fresh to the blogosphere. Brief but very accurate information Appreciate your sharing this one. A must read article!

  19. Nwokolo说道:

    Yeah bookmaking this wasn at a bad determination outstanding post!.

  20. san pham noel说道:

    I truly appreciate this blog article.Much thanks again. Really Cool.

  21. metal recycling说道:

    Really appreciate you sharing this blog post.Really thank you! Really Great.

  22. Look advanced to more added agreeable from you! However, how could we communicate?

  23. keyword planner说道:

    Thanks, I have recently been searching for facts about this subject for ages and yours is the best I ave found so far.

  24. Thank you very much! I took it for myself too. Will be useful!!

  25. There is definately a lot to find out about this issue. I really like all of the points you made.

  26. Major thankies for the article.Much thanks again. Great.

  27. Say, you got a nice article post.Much thanks again. Cool.

  28. congtyonline.com说道:

    This is one awesome article post.Really thank you! Will read on

  29. If you ask me, in excess of a couple working together to empty desired goals, often have unlimited electric power.

  30. This particular blog is really entertaining and informative. I have picked up a lot of helpful advices out of it. I ad love to visit it again soon. Thanks!

  31. useful说道:

    Keep up the excellent piece of work, I read few blog posts on this website and I think that your site is really interesting and has bands of good info.

  32. women说道:

    This very blog is no doubt educating and also informative. I have picked helluva useful stuff out of this blog. I ad love to go back over and over again. Thanks!

  33. Really appreciate you sharing this article post.Really looking forward to read more. Much obliged.

  34. Thank you ever so for you blog.Really thank you! Really Cool.

  35. wow, awesome blog article.Really thank you! Fantastic.

  36. Really interesting blog, keep up the good work!

  37. try this说道:

    Wow! Thank you! I permanently needed to write on my blog something like that. Can I implement a fragment of your post to my site?

  38. red shoes说道:

    Received the letter. I agree to exchange the articles.

  39. School news说道:

    Thanks-a-mundo for the blog post.Really looking forward to read more. Will read on

  40. School admission说道:

    I reckon something really special in this web site.

  41. Websites we recommend Wow, amazing blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is fantastic, as well as the content!

  42. hand job说道:

    Thanks so much for the blog article. Want more.

  43. This site really has all the info I needed about this subject and didn at know who to ask.

  44. Open heavens说道:

    I think this is a real great post.Really thank you! Much obliged.

  45. you might have a fantastic weblog here! would you like to make some invite posts on my weblog?

  46. This is a beautiful shot with very good lighting.

  47. Woman of Alien Great do the job you ave carried out, this website is de facto interesting with amazing information. Time is God as method of preserving everything from occurring at once.

  48. You made some good points there. I did a search on the issue and found most individuals will approve with your blog.

  49. Some truly prime blog posts on this web site , saved to favorites.

  50. negocios说道:

    Thank you ever so for you article.Really thank you! Really Great.

  51. There is noticeably a bundle to know about this. I assume you produced particular nice points in attributes also.

  52. entclass说道:

    we prefer to honor quite a few other net web-sites on the net, even though they aren

  53. resorts说道:

    Wow, wonderful blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is great, let alone the content!. Thanks For Your article about sex.

  54. This is a topic which is close to my heart Best wishes! Where are your contact details though?

  55. Way cool! Some extremely valid points! I appreciate you writing this write-up plus the rest of the site is really good.

  56. the time to study or go to the content material or web-sites we have linked to below the

  57. niw说道:

    It as hard to come by well-informed people in this particular topic, however, you seem like you know what you are talking about! Thanks

  58. Thanks for the article post.Much thanks again. Great.

  59. know more说道:

    Of course, what a fantastic site and illuminating posts, I surely will bookmark your website.Have an awsome day!

  60. reason seemed to be on the web the simplest thing to

  61. browse说道:

    Pink your blog submit and loved it. Have you at any time imagined about visitor publishing on other associated blogs related to your site?

  62. What as up, I read your new stuff daily. Your story-telling

  63. see the website说道:

    You know that children are growing up when they start asking questions that have answers..

  64. Your means of explaining the whole thing in this post is truly nice, all be able to effortlessly understand it, Thanks a lot.

  65. visit website说道:

    Valuable information. Lucky me I found your web site by accident, and I am shocked why this accident didn at happened earlier! I bookmarked it.

  66. This blog inspired me to write my own blog.

  67. This is one awesome article post. Will read on

  68. Your article is truly informative. More than that, it??s engaging, compelling and well-written. I would desire to see even more of these types of great writing.

  69. clickhere说道:

    thanks to the author for taking his clock time on this one.

  70. mua launchpad说道:

    well happy to share my knowledge here with mates.

  71. check here说道:

    Very neat blog article.Thanks Again. Really Great.

  72. more info说道:

    whoah this weblog is excellent i love studying your articles.

  73. in the United States Fish and Wildlife Service.

  74. good morning说道:

    pretty useful material, overall I feel this is really worth a bookmark, thanks

  75. Way cool! Some extremely valid points! I appreciate you penning this article and the rest of the site is very good.

  76. Game Online说道:

    Real good info can be found on website. Even if happiness forgets you a little bit, never completely forget about it. by Donald Robert Perry Marquis.

  77. Canon 80d price说道:

    Thanks for the post.Really thank you! Really Great.

  78. Simply want to say your article is as surprising. The clarity in

  79. I think this is a real great blog.Really thank you! Want more.

  80. this website说道:

    Really appreciate you sharing this article post.Much thanks again. Really Great.

  81. clothing store说道:

    Thanks again for the article post.Really thank you! Really Cool.

  82. Thank you for another excellent post. Where else could anybody get that kind of info in such a perfect way of writing? I have a presentation next week, and I am on the look for such info.

  83. click here说道:

    This blog was how do I say it? Relevant!! Finally I ave found something which helped me. Thanks!

  84. Barcode Scanner说道:

    This excellent website certainly has all of the information I needed about this subject and didn at know who to ask.

  85. Im thankful for the article. Keep writing.

  86. for more info说道:

    Wow, marvelous blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is fantastic, let alone the content!. Thanks For Your article about sex.

  87. This blog is amazaing! I will be back for more of this !!! WOW!

  88. Im obliged for the blog post.Really thank you! Want more.

  89. This is a really good tip especially to those new to the blogosphere. Brief but very precise info Thanks for sharing this one. A must read article!

  90. Really appreciate you sharing this article post.Much thanks again. Want more.

  91. couponcodes说道:

    I’а†ve read some good stuff here. Certainly value bookmarking for revisiting. I surprise how a lot attempt you put to create such a magnificent informative web site.

  92. see pron说道:

    saejRo Wow, great post.Really thank you! Great.

  93. suba me说道:

    3L3PL2 Pretty nice post. I just stumbled upon your weblog and wished to say that I have really enjoyed browsing your blog posts. After all I will be subscribing to your feed and I hope you write again soon!

  94. situs poker说道:

    Very good info. Lucky me I found your blog by chance (stumbleupon). I ave book-marked it for later!

  95. Very good article post.Much thanks again. Much obliged.

  96. Some really prime content on this web site , saved to fav.

  97. So content to get discovered this submit.. indeed, investigation is paying off. Enjoy the blog you furnished.. Good opinions you might have here..

  98. since it provides quality contents, thanks

  99. Thanks again for the article post.Much thanks again. Really Cool.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>