From 09908bbe9264453d0faf8388d20fd6ea7205689a Mon Sep 17 00:00:00 2001 From: xuxueli <931591021@qq.com> Date: Wed, 12 Jul 2017 22:05:44 +0800 Subject: [PATCH] =?UTF-8?q?properties=20=E5=8A=A0=E8=BD=BD=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../job/admin/core/util/PropertiesUtil.java | 41 +++++-------------- 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/PropertiesUtil.java b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/PropertiesUtil.java index e463e938..ed80e708 100644 --- a/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/PropertiesUtil.java +++ b/xxl-job-admin/src/main/java/com/xxl/job/admin/core/util/PropertiesUtil.java @@ -2,11 +2,12 @@ package com.xxl.job.admin.core.util; import org.slf4j.Logger; 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.InputStreamReader; -import java.net.URL; import java.util.Properties; /** @@ -16,37 +17,17 @@ import java.util.Properties; public class PropertiesUtil { private static Logger logger = LoggerFactory.getLogger(PropertiesUtil.class); private static final String file_name = "xxl-job-admin.properties"; - - /** - * load properties - * @param propertyFileName - * @return - */ - public static Properties loadProperties(String propertyFileName) { - Properties prop = new Properties(); - InputStreamReader in = null; + + + public static String getString(String key) { + Properties prop = null; try { - URL url = null; - ClassLoader loder = Thread.currentThread().getContextClassLoader(); - url = loder.getResource(propertyFileName); - in = new InputStreamReader(new FileInputStream(url.getPath()), "UTF-8"); - prop.load(in); + Resource resource = new ClassPathResource(file_name); + EncodedResource encodedResource = new EncodedResource(resource,"UTF-8"); + prop = PropertiesLoaderUtils.loadProperties(encodedResource); } catch (IOException 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) { return prop.getProperty(key); }