public class ResourceBundleTest {
public static void main(String[] args) {
// 指明包路径和文件名即可
ResourceBundle resource = ResourceBundle.getBundle("code.stu.ResourceBundle.myResources");
String driverName = resource.getString("database.driver");
String url = resource.getString("database.url");
Object[] array1 = new Object[]{"root"};
Object[] array2 = new Object[]{"test"};
// 取得字符串,直接格式化
String user = MessageFormat.format(resource.getString("database.user"), new Object[]{"root"});
String pass = MessageFormat.format(resource.getString("database.pass"), new Object[]{"test"});
System.out.println(driverName + url + user + pass);//结果:com.mysql.jdbc.Drvierjdbc:mysql://localhost:3306:testroottest
}
} StringManager
在tomcat里,把错误日志信息的字符串写在properites文件里,如此一来,打印日志的事情就可以通过上面的两个类来解决了。
StringManager是管理打印日志的类,Tomcat的设计是,对每一个包提供自己的properites文件,也就是说,每一个包的日志信息只需要去各自包的properites文件里去找就可以了,然后Tomcat为每一个包提供一个StringManager实例,相当于一个包一个单例的效果(值得学习下)。各自的StringManager实例来管理各自包下的日志打印。
源码如下:
package org.apache.catalina.util;
import java.text.MessageFormat;
import java.util.Hashtable;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.net.URLClassLoader;
public class StringManager {
// ResourceBundle用于读取properties文件
private ResourceBundle bundle;
private static org.apache.juli.logging.Log log=
org.apache.juli.logging.LogFactory.getLog( StringManager.class );
// 私有的构造方法能够保证外界无法实例化自己,这也是单例实现的关键步骤
private StringManager(String packageName) {
// properties文件所在的package+“.LocalStrings”
// 所有tomcat的日志使用的properties文件都依照这个形式来命名的
String bundleName = packageName + ".LocalStrings";
try {
// 根据bundleName取得解析资源文件的实例
bundle = ResourceBundle.getBundle(bundleName);
return;
} catch( MissingResourceException ex ) {// 好吧,异常就先不管了。
// Try from the current loader ( that's the case for trusted apps )
ClassLoader cl=Thread.currentThread().getContextClassLoader();
if( cl != null ) {
try {
bundle=ResourceBundle.getBundle(bundleName, Locale.getDefault(), cl);
return;
} catch(MissingResourceException ex2) {
}
}
if( cl==null )
cl=this.getClass().getClassLoader();
if (log.isDebugEnabled())
log.debug("Can't find resource " + bundleName +
" " + cl);
if( cl instanceof URLClassLoader ) {
if (log.isDebugEnabled())
log.debug( ((URLClassLoader)cl).getURLs());
}
}
}
/**
* Get a string from the underlying resource bundle.
*
* @param key The resource name
*/
public String getString(String key) {
return MessageFormat.format(getStringInternal(key), (Object [])null);
}
protected String getStringInternal(String key) {
// key 还是要保证不是null
if (key == null) {
String msg = "key is null";
throw new NullPointerException(msg);
}
// 返回string
String str = null;
if( bundle==null )
return key;
try {
// 资源文件里去查有没有对应的内容
str = bundle.getString(key);
} catch (MissingResourceException mre) {
str = "Cannot find message associated with key '" + key + "'";
}
return str;
}
public String getString(String key, Object[] args) {
String iString = null;
String value = getStringInternal(key);
// this check for the runtime exception is some pre 1.1.6
// VM's don't do an automatic toString() on the passed in
// objects and barf out
try {
// ensure the arguments are not null so pre 1.2 VM's don't barf
Object nonNullArgs[] = args;
for (int i=0; i