ResourceBundle用法

遇到的问题:java.util.MissingResourceException: Can’t find bundle for base name

这是原文:

solve java.util.MissingResourceException: Can’t find bundle for base name com…config, locale zh_CN
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:836)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:805)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:576)

You know java is looking for a properties file in a specific locale. You may be baffled why java keeps complaining it can’t find a properties file that is right there. A few things to keep in mind when debugging this type of errors:

These resource properties files are loaded by classloader, similar to java classes. So you need to include them in your runtime classpath.
These resources have fully-qualified-resource-name, similar to a fully-qualified-class-name, excerpt you can’t import a resource into your java source file. Why? because its name takes the form of a string.
ResourceBundle.getBundle(“config”) tells the classloader to load a resource named “config” with default package (that is, no package). It does NOT mean a resource in the current package that has the referencing class.
ResourceBundle.getBundle(“com.cheng.scrap.config”) tells the classloader to load a resource named “config” with package “com.cheng.scrap.” Its fully-qualified-resource-name is “com.cheng.scrap.config”

总结:
因为properties 在运行时环境下,被classloader 以类似与java class的方式加载,

ResourceBundle会从classpath下寻找。
(注意不需要.properties 来结尾,例如加载config.properties 只需要这么用:ResourceBundle.getBundle(configPath)即可)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值