Details of reading and using properties properties file in Spring

  • 2020-06-07 04:35:37
  • OfStack

Read and use details of properties properties file in Spring

In actual projects, some configurable customized information is usually put into the property file (such as database connection information, mail sending configuration information, etc.) for the convenience of unified configuration management. Example places the property information to be configured in the properties file/WEB-ES6en/configInfo.properties.

Some of the configuration information (related to email delivery) :


# Configuration of mail delivery 
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 properties file information is loaded using the built-in bean. Add the following to bean.xml:

Xml code



<!-- spring Property loader, load properties Properties in file  -->
 <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>

One of the ways to use the attribute information after loading is to refer to value directly in other bean definitions according to the attribute information. For example, the configuration of bean, a mail sender, is as follows:

Xml code


<!--  Mail delivery  -->
 <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 is to get the configured attribute information in the code. An javabean: ConfigInfo.java can be defined. If there is the following information in the property file, it should be obtained and used in the code:

Java code


# The save path to the generated file 
file.savePath = D:/test/
# Create a backup path for the file, and then move the corresponding file to that directory 
file.backupPath = D:/test bak/

Corresponding code in ES43en. 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 in ES59en.xml for automatic injection of annotation mode:

Xml code


<context:component-scan base-package="com.my.model" />

The ConfigInfo class is included in the package model above.

The corresponding attribute information is obtained through get method. The advantage is that it is easy to use in the code, but the disadvantage is that if new attribute information is needed in the code, it needs to add and modify ConfigInfo.java accordingly.

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: