Read and use property files in spring
Blog Category:http://wuyaweiwude.iteye.com/blog/1616803
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
- #邮件发送的相关配置
- Email.host = SMTP. 163.com
- Email.port = xxx
- Email.username = xxx
- Email.password = xxx
- 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
- <!--Spring's property loader, loads properties in the property file--
- <Bean id="Propertyconfigurer"
- class="Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- < name="Location">
- <value>/web-inf/configinfo.properties</value>
- </Property>
- <property name= "fileencoding" value="utf-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
- <!--Mail Send--
- <Bean id="MailSender"
- class="Org.springframework.mail.javamail.JavaMailSenderImpl">
- <property name="host">
- <value>${email.host}</value>
- </Property>
- <property name="Port">
- <value>${email.port}</value>
- </Property>
- <property name="username">
- <value>${email.username}</value>
- </Property>
- <property name="password">
- <value>${email.password}</value>
- </Property>
- <property name="javamailproperties">
- <props>
- <prop key="Mail.smtp.auth">true</prop>
- <prop key="Sendfrom">${email.sendfrom}</prop>
- </Props>
- </Property>
- </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
- #生成文件的保存路径
- File.savepath = d:/test/
- #生成文件的备份路径, move the corresponding file to the directory after use
- File.backuppath = D:/test bak/
the corresponding code in Configinfo.java :Java code
- @Component ("Configinfo")
- Public class Configinfo {
- @Value ("${file.savepath}")
- private String Filesavepath;
- @Value ("${file.backuppath}")
- private String Filebakpath;
- Public String Getfilesavepath () {
- return filesavepath;
- }
- Public String Getfilebakpath () {
- return filebakpath;
- }
- }
the Business class Bo uses annotations to inject Configinfo objects :Java code
- @Autowired
- Private Configinfo Configinfo;
you need to add a component scanner to the Bean.xml for automatic injection of annotation methods :XML code
- <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