Read and use property files in spring

Source: Internet
Author: User

Read and use property files in spring Blog Category:http://wuyaweiwude.iteye.com/blog/1616803
    • Spring
Springproperties property Profiles in the actual project, it is common to put some configurable custom information into the properties file (such as database connection information, mail send configuration information, etc.), which facilitates unified configuration management. For example, place the property information you want to configure in the properties file/web-inf/configinfo.properties.
Some of these configuration information (mail sending related) : Java code   
    1. #邮件发送的相关配置
    2. Email.host = SMTP. 163.com
    3. Email.port = xxx
    4. Email.username = xxx
    5. Email.password = xxx
    6. Email.sendfrom = xxx@163.com
when the spring container starts, the property file information is loaded using the built-in bean, which is added as follows in Bean.xml:XML code
  1. <!--Spring's property loader, loads properties in the property file--
  2. <Bean id="Propertyconfigurer"
  3. class="Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  4. < name="Location">
  5. <value>/web-inf/configinfo.properties</value>
  6. </Property>
  7. <property name= "fileencoding" value="utf-8" />
  8. </Bean>
one way to use property information after loading is to refer to value directly from the key of the attribute information in other bean definitions, such as the configuration of the Mail Sender bean:XML code
  1. <!--Mail Send--
  2. <Bean id="MailSender"
  3. class="Org.springframework.mail.javamail.JavaMailSenderImpl">
  4. <property name="host">
  5. <value>${email.host}</value>
  6. </Property>
  7. <property name="Port">
  8. <value>${email.port}</value>
  9. </Property>
  10. <property name="username">
  11. <value>${email.username}</value>
  12. </Property>
  13. <property name="password">
  14. <value>${email.password}</value>
  15. </Property>
  16. <property name="javamailproperties">
  17. <props>
  18. <prop key="Mail.smtp.auth">true</prop>
  19. <prop key="Sendfrom">${email.sendfrom}</prop>
  20. </Props>
  21. </Property>
  22. </Bean>
another way to use this is to get the configured property information in your code, define a Javabean:ConfigInfo.java, use annotations to inject the attribute information you need to use in your code, and use the following information in your code, as in the properties file: Java code   
    1. #生成文件的保存路径
    2. File.savepath = d:/test/
    3. #生成文件的备份路径, move the corresponding file to the directory after use
    4. File.backuppath = D:/test bak/
the corresponding code in Configinfo.java :Java code
  1. @Component ("Configinfo")
  2. Public class Configinfo {
  3. @Value ("${file.savepath}")
  4. private String Filesavepath;
  5. @Value ("${file.backuppath}")
  6. private String Filebakpath;
  7. Public String Getfilesavepath () {
  8. return filesavepath;
  9. }
  10. Public String Getfilebakpath () {
  11. return filebakpath;
  12. }
  13. }
the Business class Bo uses annotations to inject Configinfo objects :Java code  
    1. @Autowired
    2. Private Configinfo Configinfo;
you need to add a component scanner to the Bean.xml for automatic injection of annotation methods :XML code  
    1. <context:component-scan base-package="Com.my.model" />
(The Configinfo class is included in the package model above).
Obtain the corresponding property information through the Get method, the advantage is that the code is easy to use, the disadvantage is that if you need new attribute information in the code, the Configinfo.java should be added to the corresponding changes.

Read and use property files in spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.