properties 加载方式优化

v1.8.2
xuxueli 7 years ago
parent cde0561b50
commit 09908bbe92

@ -2,11 +2,12 @@ package com.xxl.job.admin.core.util;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Properties; import java.util.Properties;
/** /**
@ -16,37 +17,17 @@ import java.util.Properties;
public class PropertiesUtil { public class PropertiesUtil {
private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class); private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
private static final String file_name = "xxl-job-admin.properties"; private static final String file_name = "xxl-job-admin.properties";
/**
* load properties public static String getString(String key) {
* @param propertyFileName Properties prop = null;
* @return
*/
public static Properties loadProperties(String propertyFileName) {
Properties prop = new Properties();
InputStreamReader in = null;
try { try {
URL url = null; Resource resource = new ClassPathResource(file_name);
ClassLoader loder = Thread.currentThread().getContextClassLoader(); EncodedResource encodedResource = new EncodedResource(resource,"UTF-8");
url = loder.getResource(propertyFileName); prop = PropertiesLoaderUtils.loadProperties(encodedResource);
in = new InputStreamReader(new FileInputStream(url.getPath()), "UTF-8");
prop.load(in);
} catch (IOException e) { } catch (IOException e) {
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
} }
return prop;
}
public static String getString(String key) {
Properties prop = loadProperties(file_name);
if (prop!=null) { if (prop!=null) {
return prop.getProperty(key); return prop.getProperty(key);
} }

Loading…
Cancel
Save