Use the spring factory to read the property configuration file sample code

  • 2020-12-19 20:59:54
  • OfStack

This article describes two ways that Spring can read an property configuration file, and then looks at the details.

1. Read through Spring factory

Example:


public class PropertyConfig {
	private static AbstractBeanFactory beanFactory = null;
	private static final Map<String,String> cache = new oncurrentHashMap<>();
	@Inject  
	  public PropertyConfig(AbstractBeanFactory beanFactory) {
		this.beanFactory = beanFactory;
	}
	/**   
   *  According to the key Gets the configuration file Value  
   * @param key   * @return   
   */
	public static String getProperty(String key) {
		String propValue = "";
		if(cache.containsKey(key)){
			propValue = cache.get(key);
		} else {
			try {
				propValue = beanFactory.resolveEmbeddedValue("${" + key.trim() + "}");
				cache.put(key,propValue);
			}
			catch (IllegalArgumentException ex) {
				ex.printStackTrace();
			}
		}
		return propValue;
	}
}

Spring xml configuration


<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
  <property name="ignoreResourceNotFound" value="true"/>
  <property name="locations">
    <list>
      <value>classpath:props/${property-path}.properties</value>
      <value>classpath:important.properties</value>
    </list>
  </property>
</bean>

Use in projects


String maxTimeInSecondsProp = PropertyConfig.getProperty("maxTimeInSeconds");

2. Directly use spirng program code to read the configuration file method of the project


import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.core.io.FileSystemResource;
 
public class Test {
  /**
   * @param args
   */
  public static void main( String[] args ) {
    String configFile = "D:/test/application.properties";
    // If the profile is in classpath It can be used in the directory ClassPathResource object 
    //Resource resource = new ClassPathResource("/application.properties");
    Resource resource = new FileSystemResource( configFile );
    try {
      Properties property = PropertiesLoaderUtils.loadProperties(resource);
      String driver = property.getProperty("jdbc.driver");
      String url = property.getProperty("jdbc.url");
      String userName = property.getProperty("jdbc.username");
      String password = property.getProperty("jdbc.password");
    }
    catch (IOException e1) {
      //log.error("read config file failed", e1);
    }
  }
}

ClassPathResource objects can be used if the configuration file is in the classpath directory


Resource resource = new ClassPathResource("/application.properties");

conclusion

That's the end of this article's sample code for reading the property configuration file using the spring factory, and I hope you found it helpful. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: