public final class ResourceBundleUtil extends Object
ResourceBundle
工具类.
该类专注于解析配置文件,至于解析到的结果你可以使用 ConvertUtil
来进行转换成你需要的类型
如果一个项目中同时存在Message.properties、Message_zh_CN.properties、Message_zh_ CN.class 3个类型的文件,那最终使用的是哪一个?
只会使用一个,按照优先级使用.
顺序为Message_zh_CN.class、Message_zh_CN.properties、Message.properties.
解析原理,参见:
"java.util.ResourceBundle#loadBundle(CacheKey, List, Control, boolean)"
ResourceBundle.Control.newBundle(String, Locale, String, ClassLoader, boolean)
MessageFormatUtil.format(String, Object...)
,
ResourceBundle
,
PropertyResourceBundle
,
ListResourceBundle
,
"org.springframework.core.io.support.LocalizedResourceHelper"Modifier and Type | Method and Description |
---|---|
static ResourceBundle |
getResourceBundle(InputStream inputStream)
获得ResourceBundle(
PropertyResourceBundle ),新增这个方法的初衷是为了能读取任意的资源(包括本地file等). |
static ResourceBundle |
getResourceBundle(String baseName)
|
static ResourceBundle |
getResourceBundle(String baseName,
Locale locale)
|
static String |
getValue(ResourceBundle resourceBundle,
String key,
Object... arguments)
获取
resourceBundle 配置文件指定 key 键的值. |
static <T> T |
toAliasBean(ResourceBundle resourceBundle,
Class<T> aliasBeanClass)
将
resourceBundle 转换成aliasBean . |
static Map<String,String> |
toMap(ResourceBundle resourceBundle)
将
resourceBundle 转成map. |
static Properties |
toProperties(ResourceBundle resourceBundle)
将
resourceBundle 转成Properties. |
public static String getValue(ResourceBundle resourceBundle, String key, Object... arguments)
resourceBundle
配置文件指定 key
键的值.
- 支持配置文件含参数信息
arguments
,使用MessageFormatUtil.format(String, Object...)
来解析
如果有配置文件 messages\feilong-core-test.properties,内容如下:
test.arguments=my name is {0},age is {1}此时调用方法:
ResourceBundle resourceBundle = ResourceBundle.getBundle("messages/feilong-core-test"); ResourceBundleUtil.getValueWithArguments(resourceBundle, "test.arguments", "feilong", "18");返回:my name is feilong,age is 18
resourceBundle
- the resource bundlekey
- Properties配置文件键名arguments
- 此处可以传递Object[]数组过来StringUtils.EMPTY
NullPointerException
- 如果 resourceBundle
或者 key
是nullIllegalArgumentException
- 如果 key
是blank,抛出 IllegalArgumentException
ResourceBundle.getString(String)
,
MessageFormatUtil.format(String, Object...)
public static Map<String,String> toMap(ResourceBundle resourceBundle)
resourceBundle
转成map.
- JDK默认使用的是
PropertyResourceBundle
,内部是使用 hashmap来存储数据的,
本方法出于log以及使用方便,返回的是 TreeMap
在 classpath messages 目录下面有 memcached.properties,内容如下:
# 注意此处 ip出现 - 横杆 仅作测试使用 memcached.serverlist=172.20.3-1.23:11211,172.20.31.22:11211 memcached.poolname=sidsock2 #单位分钟 memcached.expiretime=180 memcached.serverweight=2 memcached.initconnection=10 memcached.minconnection=5 memcached.maxconnection=250 #设置主线程睡眠时间,每30秒苏醒一次,维持连接池大小 memcached.maintSleep=30 #关闭套接字缓存 memcached.nagle=false #连接建立后的超时时间 memcached.socketto=3000 memcached.alivecheck=false此时你可以如此调用代码:Map返回:<String, String>
map = toMap(getResourceBundle("messages/memcached")); LOGGER.debug(JsonUtil.format(map));{ "memcached.alivecheck": "false", "memcached.expiretime": "180", "memcached.initconnection": "10", "memcached.maintSleep": "30", "memcached.maxconnection": "250", "memcached.minconnection": "5", "memcached.nagle": "false", "memcached.poolname": "sidsock2", "memcached.serverlist": "172.20.3-1.23:11211,172.20.31.22:11211", "memcached.serverweight": "2", "memcached.socketto": "3000" }
resourceBundle
- the resource bundleresourceBundle
是null,抛出 NullPointerException
resourceBundle
没有key,则返回Collections.emptyMap()
TreeMap
public static Properties toProperties(ResourceBundle resourceBundle)
resourceBundle
转成Properties.
在 classpath messages 目录下面有 memcached.properties,内容如下:
# 注意此处 ip出现 - 横杆 仅作测试使用 memcached.serverlist=172.20.3-1.23:11211,172.20.31.22:11211 memcached.poolname=sidsock2 #单位分钟 memcached.expiretime=180 memcached.serverweight=2 memcached.initconnection=10 memcached.minconnection=5 memcached.maxconnection=250 #设置主线程睡眠时间,每30秒苏醒一次,维持连接池大小 memcached.maintSleep=30 #关闭套接字缓存 memcached.nagle=false #连接建立后的超时时间 memcached.socketto=3000 memcached.alivecheck=false此时你可以如此调用代码:Properties properties = ResourceBundleUtil.toProperties("messages.memcached"); LOGGER.debug(JsonUtil.format(properties));返回:{ "memcached.serverlist": "172.20.3-1.23:11211,172.20.31.22:11211", "memcached.maxconnection": "250", "memcached.socketto": "3000", "memcached.initconnection": "10", "memcached.nagle": "false", "memcached.expiretime": "180", "memcached.maintSleep": "30", "memcached.alivecheck": "false", "memcached.serverweight": "2", "memcached.poolname": "sidsock2", "memcached.minconnection": "5" }
resourceBundle
- the resource bundleresourceBundle
没有key value,则返回 new Properties
Properties
NullPointerException
- 如果 resourceBundle
是nulltoMap(ResourceBundle)
,
ConvertUtil.toProperties(Map)
public static <T> T toAliasBean(ResourceBundle resourceBundle, Class<T> aliasBeanClass)
resourceBundle
转换成aliasBean
.
在 classpath messages 目录下面有 memcached.properties,内容如下:
# 注意此处 ip出现 - 横杆 仅作测试使用 memcached.serverlist=172.20.3-1.23:11211,172.20.31.22:11211 memcached.poolname=sidsock2 #单位分钟 memcached.expiretime=180 memcached.serverweight=2 memcached.initconnection=10 memcached.minconnection=5 memcached.maxconnection=250 #设置主线程睡眠时间,每30秒苏醒一次,维持连接池大小 memcached.maintSleep=30 #关闭套接字缓存 memcached.nagle=false #连接建立后的超时时间 memcached.socketto=3000 memcached.alivecheck=false有以下aliasBean信息:
public class DangaMemCachedConfig{ //** The serverlist. @Alias(name = "memcached.serverlist",sampleValue = "172.20.31.23:11211,172.20.31.22:11211") private String[] serverList; //@Alias(name = "memcached.poolname",sampleValue = "sidsock2") private String poolName; //** The expire time 单位分钟. @Alias(name = "memcached.expiretime",sampleValue = "180") private Integer expireTime; //** 权重. @Alias(name = "memcached.serverweight",sampleValue = "2,1") private Integer[] weight; //** The init connection. @Alias(name = "memcached.initconnection",sampleValue = "10") private Integer initConnection; //** The min connection. @Alias(name = "memcached.minconnection",sampleValue = "5") private Integer minConnection; //** The max connection. @Alias(name = "memcached.maxconnection",sampleValue = "250") private Integer maxConnection; //** 设置主线程睡眠时间,每30秒苏醒一次,维持连接池大小. @Alias(name = "memcached.maintSleep",sampleValue = "30") private Integer maintSleep; //** 关闭套接字缓存. @Alias(name = "memcached.nagle",sampleValue = "false") private Boolean nagle; //** 连接建立后的超时时间. @Alias(name = "memcached.socketto",sampleValue = "3000") private Integer socketTo; //** The alive check. @Alias(name = "memcached.alivecheck",sampleValue = "false") private Boolean aliveCheck; //setter getter 略 }此时你可以如此调用代码:DangaMemCachedConfig dangaMemCachedConfig = ResourceBundleUtil.toAliasBean("messages.memcached", DangaMemCachedConfig.class); LOGGER.debug(JsonUtil.format(dangaMemCachedConfig));返回:{ "maxConnection": 250, "expireTime": 180, "serverList": [ "172.20.3-1.23", "11211", "172.20.31.22", "11211" ], "weight": [2], "nagle": false, "initConnection": 10, "aliveCheck": false, "poolName": "sidsock2", "maintSleep": 30, "socketTo": 3000, "minConnection": 5 }你会发现类型会自动转换,虽然properties里面存储key和value都是string,但是使用该方法,可以自动类型转换,转成bean里面声明的类型
但是同时,你也会发现,上面的 serverList 期望值是 ["172.20.3-1.23:11211","172.20.31.22:11211"],但是和你的期望值不符合,
因为,ArrayConverter
默认允许的字符 allowedChars 只有'.', '-'
,其他都会被做成分隔符你需要如此这般:
ArrayConverter arrayConverter = new ArrayConverter(String[].class, new StringConverter(), 2); char[] allowedChars = { ':' }; arrayConverter.setAllowedChars(allowedChars); ConvertUtils.register(arrayConverter, String[].class); DangaMemCachedConfig dangaMemCachedConfig = ResourceBundleUtil.toAliasBean("messages.memcached", DangaMemCachedConfig.class); LOGGER.debug(JsonUtil.format(dangaMemCachedConfig));返回:{ "maxConnection": 250, "expireTime": 180, "serverList": [ "172.20.3-1.23:11211", "172.20.31.22:11211" ], "weight": [2], "nagle": false, "initConnection": 10, "aliveCheck": false, "poolName": "sidsock2", "maintSleep": 30, "socketTo": 3000, "minConnection": 5 }
T
- the generic typeresourceBundle
- the resource bundlealiasBeanClass
- the alias bean classNullPointerException
- 如果 resourceBundle
或者 aliasBean
是nullBeanUtil.populateAliasBean(Object, Map)
public static ResourceBundle getResourceBundle(String baseName)
Locale.getDefault()
获得ResourceBundle
.
场景: 如项目的classpath下面有 messages/feilong-core-test.properties,内容如下
config_test_array=5,8,7,6 test.arguments=my name is {0},age is {1}你可以使用以下代码来读取内容:
ResourceBundle resourceBundle = getResourceBundle("messages/feilong-core-test"); LOGGER.debug(JsonUtil.format(toMap(resourceBundle)));返回:{ "config_test_array": "5,8,7,6", "test.arguments": "my name is {0},age is {1}", }
baseName
- 一个完全限定类名,配置文件的包+类全名,比如 message.feilong-core-test (不要尾缀);baseName
里面没有任何内容,返回不是null的 ResourceBundle
NullPointerException
- 如果 baseName
是nullIllegalArgumentException
- 如果 baseName
是 blankMissingResourceException
- 如果资源文件 baseName
不存在Locale.getDefault()
,
getResourceBundle(String, Locale)
public static ResourceBundle getResourceBundle(String baseName, Locale locale)
baseName
和 locale
获得ResourceBundle
.
场景: 比如在 classpath 下面有 messages\feilong-archetypes_en.properties 和 messages\feilong-archetypes_zh_CN.properties 两个配置文件,内容如下
messages\feilong-archetypes_en.properties
feilong-archetypes.welcome=欢迎(简体)messages\feilong-archetypes_zh_CN.properties
feilong-archetypes.welcome=欢迎(简体)此时,我要读取 英文的配置文件,你可以这么写
ResourceBundle resourceBundle = getResourceBundle("messages/feilong-archetypes", Locale.ENGLISH); Map返回:<String, String>
map = toMap(resourceBundle); LOGGER.debug(JsonUtil.format(map));{"feilong-archetypes.welcome": "welcome(english)"}
baseName
- 一个完全限定类名,配置文件的包+类全名,比如 message.feilong-core-test (不要尾缀);locale
- the locale for which a resource bundle is desired,如果是null,将使用 Locale.getDefault()
baseName
里面没有任何内容,返回不是null的 ResourceBundle
Locale.getDefault()
来获取NullPointerException
- 如果 baseName
是nullIllegalArgumentException
- 如果 baseName
是 blankMissingResourceException
- 如果资源文件 baseName
不存在ResourceBundle.getBundle(String, Locale)
public static ResourceBundle getResourceBundle(InputStream inputStream)
PropertyResourceBundle
),新增这个方法的初衷是为了能读取任意的资源(包括本地file等).
场景: 有配置文件在 E:\\DataCommon\\Files\\Java\\config\\mail-read.properties (通常敏感的配置文件不会随着项目走),现在需要读取里面的信息
ResourceBundle resourceBundle = getResourceBundle( FileUtil.getFileInputStream("E:\\DataCommon\\Files\\Java\\config\\mail-read.properties")); LOGGER.debug(JsonUtil.format(toMap(resourceBundle)));返回:{ "incoming.imap.hostname": "imap.exmail.qq.com", "incoming.pop.hostname": "pop.exmail.qq.com", }
inputStream
- the input streaminputStream
是null,抛出 NullPointerException
PropertyResourceBundle.PropertyResourceBundle(InputStream)
PropertyResourceBundle.PropertyResourceBundle(InputStream)
Copyright © 2008-2019 by feilong