Use the global class to optimize the code and facilitate maintenance by loading the properties file and reading the relevant configuration.

In java projects, we often need some configuration parameters for business processing, interface calls, etc., such as Tencent corporate mailbox, the key to be used and other relatively fixed parameters, although the parameters do not change often, but if they need to be changed, You need to search in the classes one by one, which greatly affects the maintenance speed and efficiency.

Therefore, a global class Global and a resource file loading class PropertiesLoader are written to read and write the configuration.

PropertiesLoader resource file loads classes

package com.crawler.common.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

/**
 * Resource file loading class
 * @author siqiangming on April 16, 2018 at 3:43:26 PM
 */
public class PropertiesLoader {
	protected static Log logger = LogFactory.getLog(PropertiesLoader.class);
	private static ResourceLoader resourceLoader = new DefaultResourceLoader();
	private final Properties properties;
	
	/**
	 * Constructor, pass the resource file path when initializing the object, and load the resource file
	 * @param resourcesLocations
	 */
	public PropertiesLoader(String... resourcesLocations) {
		properties = loadProperties(resourcesLocations);
	}
	public Properties getProperties() {
		return properties;
	}
	/**
	 * Take out the property, take the system property first, if the system does not have it, go to the resource file to take it, if it can't take it, the return is empty
	 * @author siqiangming on April 17, 2018 at 9:30:55 AM
	 * @param key property name
	 * @return property value
	 */
	private String getValue(String key){
		logger.debug("获取value: " + key);
		String systemProperty = System.getProperty(key);
		if (systemProperty != null) {
			return systemProperty;
		}
		if (properties.containsKey(key)) {
			return properties.getProperty(key);
		}
		return "";
	}
	/**
	 * Take out the property, throw an exception if you can't get it
	 * @author siqiangming on April 17, 2018 at 9:35:42 AM
	 * @param key property name
	 * @return property value
	 */
	public String getProperty(String key){
		logger.debug("Get property: " + key);
		String value = getValue(key);
		if (value == null) {
			throw new NoSuchElementException();
		}
		return value;
	}
	/**
	 * Take out the property, if you can't get it, return the default value
	 * @author siqiangming on April 17, 2018 at 9:38:06 AM
	 * @param key property name
	 * @param defaultValue default value
	 * @return property value
	 */
	public String getProperty(String key, String defaultValue){
		String value = getValue(key);
		return value != null ? value : defaultValue;
	}
	/**
	 * Load the resource file, the file path uses the spring resource format
	 * @author siqiangming on April 17, 2018 at 9:24:46 AM
	 * @param resourcesLocations resource file path
	 * @return resource property set
	 */
	private Properties loadProperties(String... resourcesLocations) {
		Properties prop = new Properties();
		for (String location : resourcesLocations) {
			InputStream in = null;
			try {
				Resource resource = resourceLoader.getResource(location);
				in = resource.getInputStream();
				prop.load(in);
			} catch (IOException e) {
				logger.info("Could not load properties from path:" + location + ", " + e.getMessage());
			} finally {
				if(in != null){
					try {
						in.close();
					} catch (IOException e) {
						logger.info("Could not close InputStream: " + e.getMessage());
					}
				}
			}
		}
		return prop;
	}
}
Global global class
package com.crawler.common.config;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.crawler.common.utils.PropertiesLoader;
import com.google.common.collect.Maps;

/**
 * global class
 * @author siqiangming on April 17, 2018 at 9:04:22 AM
 */
public class Global{
	protected static Log logger = LogFactory.getLog(Global.class);
	/**
	 * global map
	 */
	private static Map<String, String> map = Maps.newHashMap();
	/**
	 * Resource file loader
	 */
	private static PropertiesLoader loader = new PropertiesLoader("crawler.properties");
	
	/**
	 * Get the configuration, if not in the global map, put it into the map after getting it
	 * @author siqiangming on April 17, 2018 at 9:41:23 AM
	 * @param key property name
	 * @return property value
	 */
	public static String getConfig(String key){
		String value = map.get(key);
		if (value == null) {
			value = loader.getProperty(key);
			map.put(key, value != null ? value : "");
		}
		return value;
	}
}

Now we just need to do the following configuration in the resource file

##mysql database setting
jdbc.type=mysql
jdbc.driver=com.mysql.jdbc.Driver
#Official environment database address
jdbc.url=jdbc:mysql://192.168.32.175:3306/crawler?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
#Formal environment database password
jdbc.password=***

#pool settings
jdbc.pool.init=1
jdbc.pool.minIdle=3
jdbc.pool.maxActive=20
#jdbc.testSql=SELECT 'x'
jdbc.testSql=SELECT 'x' FROM DUAL

##Product information settings
productName=Beijing Peach Blossom Island Crawler Tool
copyrightStartYear=2018
copyrightYear=2022
poweredBy=Beijing Peach Blossom Island Information Technology Co., Ltd.
version=V1.0

##View file storage path
web.view.prefix=/WEB-INF/views/
web.view.suffix=.jsp

##Aggregate data configuration
#Identify image verification code application configuration
juhe.appKey = ****************************
juhe.codeType=1004
juhe.url=http://op.juhe.cn/vercode/index
juhe.connectTime=30000
juhe.socketTime = 30000

Now we can directly read the configuration using the following method

	@Test
	public void getConfig(){
		String time = Global.getConfig("juhe.connectTime");
		System.out.println("time: "+time);
	}

result

time: 30000


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324597338&siteId=291194637