English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Detailed Explanation of Reading and Using properties File in Spring
In actual projects, it is usually the practice to place some configurable custom information in property files (such as database connection information, email sending configuration information, etc.), which facilitates unified configuration management. For example, the properties information to be configured is placed in the property file/WEB-INF/in configInfo.properties.
Among some of the configuration information (related to email sending):
# Configuration related to email sending email.host = smtp.163.com email.port = xxx email.username = xxx email.password = xxx email.sendFrom = [email protected]
When the Spring container starts, use the built-in bean to load the attribute file information, and add the following in bean.xml:
Xml code
<!-- Spring's attribute loader, loading attributes from properties files --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>/WEB-INF/configInfo.properties</value> </property> <property name="fileEncoding" value="utf-8" /> </bean>
After the attribute information is loaded, one usage method is to directly refer to the value according to the key of the attribute information in other bean definitions, for example, the configuration of the email sender bean is as follows:
Xml code
<!-- Email Sending --> <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 usage method is to obtain the configuration attribute information in the code, you can define a javabean: ConfigInfo.java, and inject the attribute information needed in the code using annotations; if there is the following information in the property file that needs to be obtained and used in the code:
Java code
#Generate the save path of the file file.savePath = D:/test/ #Generate the backup path of the file, move the corresponding file to this directory after use file.backupPath = D:/test bak/
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; } }
Use annotations to inject ConfigInfo objects in the business class bo:
Java code
@Autowired private ConfigInfo configInfo;
You need to add a component scanner in bean.xml, which is used for automatic injection in annotation mode:
Xml code
<context:component-scan base-package="com.my.model" />
(The above package model contains the ConfigInfo class).
Obtain the corresponding attribute information through the get method. The advantage is that it is convenient to use in the code, but the disadvantage is that if new attribute information needs to be used in the code, corresponding additions and modifications need to be made to ConfigInfo.java.
Thank you for reading, I hope it can help everyone. Thank you for your support to our website!