Spring uses code to read properties file instance parsing

  • 2020-12-18 01:50:59
  • OfStack

Sometimes we need to read the properties configuration file directly in Spring code, so how do we do that? Now let's look at the details.

As we all know, Spring can read the values in properties @Value, just by configuring them in a configuration file

org.springframework.beans.factory.config.PropertyPlaceholderConfigurer


<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
      <value>classpath:config.properties</value>
    </property>
  </bean>

So when you need to use these to get the properties median, you can use them like this


  @Value("${sql.name}")
  private String sqlName;

But there is a problem. Every time I use a value in the configuration file, I declare a local variable. There is no code way to directly read the values in the configuration file.

The answer is to rewrite PropertyPlaceholderConfigurer


public class PropertyPlaceholder extends PropertyPlaceholderConfigurer {

  private static Map<String,String> propertyMap;

  @Override
  protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
    super.processProperties(beanFactoryToProcess, props);
    propertyMap = new HashMap<String, String>();
    for (Object key : props.keySet()) {
      String keyStr = key.toString();
      String value = props.getProperty(keyStr);
      propertyMap.put(keyStr, value);
    }
  }

  //static method for accessing context properties
  public static Object getProperty(String name) {
    return propertyMap.get(name);
  }
}

In the configuration file, replace PropertyPlaceholderConfigurer with the above class


 <bean id="propertyConfigurer" class="com.gyoung.mybatis.util.PropertyPlaceholder">
    <property name="location">
      <value>classpath:config.properties</value>
    </property>
  </bean>

This can be obtained programmatically in code


 PropertyPlaceholder.getProperty("sql.name");

If there are multiple profiles, configure the locations attribute


<bean id="propertyConfigurer"
     class="com.gyoung.mybatis.util.PropertyPlaceholder">
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="locations">
      <list>
        <value>file:./jdbc.properties</value>
        <value>file:./module.config.properties</value>
        <value>classpath:jdbc.properties</value>
        <value>classpath*:*.config.properties</value>
      </list>
    </property>
  </bean>

conclusion

That's it for this article about Spring using code to read properties file instance parsing, and I hope you found it helpful. Those who are interested can continue to see this site:

Spring Instantiates bean process parsing and complete code examples

The Spring factory method creates (instantiates) the bean instance code

If there is any deficiency, please let me know. Thank you for your support!


Related articles: