mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add PropsUtil
This commit is contained in:
parent
7cd260c1ea
commit
4c202733e5
@ -609,6 +609,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
|
|||||||
*
|
*
|
||||||
* @param m Map
|
* @param m Map
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("NullableProblems")
|
||||||
@Override
|
@Override
|
||||||
public void putAll(Map<? extends String, ? extends String> m) {
|
public void putAll(Map<? extends String, ? extends String> m) {
|
||||||
this.groupedMap.putAll(DEFAULT_GROUP, m);
|
this.groupedMap.putAll(DEFAULT_GROUP, m);
|
||||||
@ -627,6 +628,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
|
|||||||
*
|
*
|
||||||
* @return 默认分组(空分组)中的所有键列表
|
* @return 默认分组(空分组)中的所有键列表
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("NullableProblems")
|
||||||
@Override
|
@Override
|
||||||
public Set<String> keySet() {
|
public Set<String> keySet() {
|
||||||
return this.groupedMap.keySet(DEFAULT_GROUP);
|
return this.groupedMap.keySet(DEFAULT_GROUP);
|
||||||
@ -637,6 +639,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
|
|||||||
*
|
*
|
||||||
* @return 默认分组(空分组)中的所有值列表
|
* @return 默认分组(空分组)中的所有值列表
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("NullableProblems")
|
||||||
@Override
|
@Override
|
||||||
public Collection<String> values() {
|
public Collection<String> values() {
|
||||||
return this.groupedMap.values(DEFAULT_GROUP);
|
return this.groupedMap.values(DEFAULT_GROUP);
|
||||||
@ -647,6 +650,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
|
|||||||
*
|
*
|
||||||
* @return 默认分组(空分组)中的所有键值对列表
|
* @return 默认分组(空分组)中的所有键值对列表
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("NullableProblems")
|
||||||
@Override
|
@Override
|
||||||
public Set<Entry<String, String>> entrySet() {
|
public Set<Entry<String, String>> entrySet() {
|
||||||
return this.groupedMap.entrySet(DEFAULT_GROUP);
|
return this.groupedMap.entrySet(DEFAULT_GROUP);
|
||||||
@ -657,7 +661,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
|
|||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
int result = 1;
|
int result = 1;
|
||||||
result = prime * result + ((charset == null) ? 0 : charset.hashCode());
|
result = prime * result + ((charset == null) ? 0 : charset.hashCode());
|
||||||
result = prime * result + ((groupedMap == null) ? 0 : groupedMap.hashCode());
|
result = prime * result + groupedMap.hashCode();
|
||||||
result = prime * result + (isUseVariable ? 1231 : 1237);
|
result = prime * result + (isUseVariable ? 1231 : 1237);
|
||||||
result = prime * result + ((settingUrl == null) ? 0 : settingUrl.hashCode());
|
result = prime * result + ((settingUrl == null) ? 0 : settingUrl.hashCode());
|
||||||
return result;
|
return result;
|
||||||
@ -682,11 +686,7 @@ public class Setting extends AbsSetting implements Map<String, String> {
|
|||||||
} else if (false == charset.equals(other.charset)) {
|
} else if (false == charset.equals(other.charset)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (groupedMap == null) {
|
if (false == groupedMap.equals(other.groupedMap)) {
|
||||||
if (other.groupedMap != null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
} else if (false == groupedMap.equals(other.groupedMap)) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (isUseVariable != other.isUseVariable) {
|
if (isUseVariable != other.isUseVariable) {
|
||||||
|
@ -1,20 +1,22 @@
|
|||||||
package cn.hutool.setting;
|
package cn.hutool.setting;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.io.resource.NoResourceException;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
import cn.hutool.core.io.FileUtil;
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setting工具类<br>
|
* Setting工具类<br>
|
||||||
* 提供静态方法获取配置文件
|
* 提供静态方法获取配置文件
|
||||||
*
|
*
|
||||||
* @author looly
|
* @author looly
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class SettingUtil {
|
public class SettingUtil {
|
||||||
/** 配置文件缓存 */
|
/**
|
||||||
|
* 配置文件缓存
|
||||||
|
*/
|
||||||
private static Map<String, Setting> settingMap = new ConcurrentHashMap<>();
|
private static Map<String, Setting> settingMap = new ConcurrentHashMap<>();
|
||||||
private static final Object lock = new Object();
|
private static final Object lock = new Object();
|
||||||
|
|
||||||
@ -43,4 +45,25 @@ public class SettingUtil {
|
|||||||
}
|
}
|
||||||
return setting;
|
return setting;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取给定路径找到的第一个配置文件<br>
|
||||||
|
* * name可以为不包括扩展名的文件名(默认.setting为结尾),也可以是文件名全称
|
||||||
|
*
|
||||||
|
* @param names 文件名,如果没有扩展名,默认为.setting
|
||||||
|
*
|
||||||
|
* @return 当前环境下配置文件
|
||||||
|
* @since 5.1.3
|
||||||
|
*/
|
||||||
|
public static Setting getFirstFound(String... names) {
|
||||||
|
Setting setting;
|
||||||
|
for (String name : names) {
|
||||||
|
try {
|
||||||
|
return get(name);
|
||||||
|
} catch (NoResourceException e) {
|
||||||
|
//ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,11 @@ public final class Props extends Properties implements BasicTypeGetter<String>,
|
|||||||
private static final long serialVersionUID = 1935981579709590740L;
|
private static final long serialVersionUID = 1935981579709590740L;
|
||||||
private final static Log log = LogFactory.get();
|
private final static Log log = LogFactory.get();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认配置文件扩展名
|
||||||
|
*/
|
||||||
|
public final static String EXT_NAME = "properties";
|
||||||
|
|
||||||
// ----------------------------------------------------------------------- 私有属性 start
|
// ----------------------------------------------------------------------- 私有属性 start
|
||||||
/** 属性文件的URL */
|
/** 属性文件的URL */
|
||||||
private URL propertiesFileUrl;
|
private URL propertiesFileUrl;
|
||||||
|
@ -0,0 +1,70 @@
|
|||||||
|
package cn.hutool.setting.dialect;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.io.resource.NoResourceException;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Props工具类<br>
|
||||||
|
* 提供静态方法获取配置文件
|
||||||
|
*
|
||||||
|
* @author looly
|
||||||
|
* @since 5.1.3
|
||||||
|
*/
|
||||||
|
public class PropsUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置文件缓存
|
||||||
|
*/
|
||||||
|
private static Map<String, Props> propsMap = new ConcurrentHashMap<>();
|
||||||
|
private static final Object lock = new Object();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前环境下的配置文件<br>
|
||||||
|
* name可以为不包括扩展名的文件名(默认.properties),也可以是文件名全称
|
||||||
|
*
|
||||||
|
* @param name 文件名,如果没有扩展名,默认为.properties
|
||||||
|
* @return 当前环境下配置文件
|
||||||
|
*/
|
||||||
|
public static Props get(String name) {
|
||||||
|
Props props = propsMap.get(name);
|
||||||
|
if (null == props) {
|
||||||
|
synchronized (lock) {
|
||||||
|
props = propsMap.get(name);
|
||||||
|
if (null == props) {
|
||||||
|
String filePath = name;
|
||||||
|
String extName = FileUtil.extName(filePath);
|
||||||
|
if (StrUtil.isEmpty(extName)) {
|
||||||
|
filePath = filePath + "." + Props.EXT_NAME;
|
||||||
|
}
|
||||||
|
props = new Props(filePath);
|
||||||
|
propsMap.put(name, props);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return props;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取给定路径找到的第一个配置文件<br>
|
||||||
|
* * name可以为不包括扩展名的文件名(默认.setting为结尾),也可以是文件名全称
|
||||||
|
*
|
||||||
|
* @param names 文件名,如果没有扩展名,默认为.setting
|
||||||
|
*
|
||||||
|
* @return 当前环境下配置文件
|
||||||
|
*/
|
||||||
|
public static Props getFirstFound(String... names) {
|
||||||
|
Props props;
|
||||||
|
for (String name : names) {
|
||||||
|
try {
|
||||||
|
return get(name);
|
||||||
|
} catch (NoResourceException e) {
|
||||||
|
//ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package cn.hutool.setting.test;
|
||||||
|
|
||||||
|
import cn.hutool.setting.dialect.PropsUtil;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class PropsUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getTest() {
|
||||||
|
String driver = PropsUtil.get("test").getStr("driver");
|
||||||
|
Assert.assertEquals("com.mysql.jdbc.Driver", driver);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getFirstFoundTest() {
|
||||||
|
String driver = Objects.requireNonNull(PropsUtil.getFirstFound("test2", "test")).getStr("driver");
|
||||||
|
Assert.assertEquals("com.mysql.jdbc.Driver", driver);
|
||||||
|
}
|
||||||
|
}
|
@ -12,4 +12,10 @@ public class SettingUtilTest {
|
|||||||
String driver = SettingUtil.get("test").get("demo", "driver");
|
String driver = SettingUtil.get("test").get("demo", "driver");
|
||||||
Assert.assertEquals("com.mysql.jdbc.Driver", driver);
|
Assert.assertEquals("com.mysql.jdbc.Driver", driver);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getFirstFoundTest() {
|
||||||
|
String driver = SettingUtil.getFirstFound("test2", "test").get("demo", "driver");
|
||||||
|
Assert.assertEquals("com.mysql.jdbc.Driver", driver);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user