快捷搜索:  as  test  1111  test aNd 8=8  test++aNd+8=8  as++aNd+8=8  as aNd 8=8

亚博老虎机平台靠谱吗_酒文化网进入



主题:

先懂得下两个所谓的常识点:ResourceBundle 和 MessageFormat

在项目里用的轻车熟路的properites文件,大年夜多要用到这两个类吧。

java.util.ResourceBundle

java.text.MessageFormat

1,ResourceBundle解析资本文件分两步:1加载资本文件,2获取资本文件中的信息

// 加载资本文件

ResourceBundle resource = ResourceBundle.getBundle("messages");// 获取资本文件中的信息

String driverName = resource.getString("database.driver");

ResourceBundle支持多国说话:先把文件名取成类似这样myres_zh_CN.properties

然后:

Locale locale1 = new Locale("zh", "CN");

ResourceBundle resb1 = ResourceBundle.getBundle("myres", locale1);resb1.getString("aaa");

2,MessageFormat用来款式化一个消息(字符串嘛)

直接网上类似代码:

String pig = "{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}{16}";

Object[] array = new Object[]{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q"};String value = MessageFormat.format(pig, array);

System.out.println(value);//最遣散果是:ABCDEFGHIJKLMNOPQ

3,结合在一路就可以实现,将properites文件解析出想要的消息体,然后款式化后给上一层措施用。

myResources.properties:

database.driver=com.mysql.jdbc.Drvier

database.url=jdbc:mysql://localhost:3306:testdatabase.user={0}

database.pass={0}

例:

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[]{"te亚博老虎机平台靠谱吗st"});

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 barfObject nonNullArgs[] = args;

for (int i=0; iif (args[i] == null) {

if (nonNullArgs==args) nonNullArgs=(Object[])args.clone();nonNullArgs[i] = "nu亚博老虎机平台靠谱吗ll";

}}

// 款式化,便是把一些变更的参数插入到value这个string中去,款式化成一个新的终极的stringiString = MessageFormat.format(value, nonNullArgs);

} catch (IllegalArgumentException iae) {StringBuffer buf = new StringBuffer();

buf.append(value);for (int i = 0; i

buf.append(" arg[" + i + 亚博老虎机平台靠谱吗"]=" + args[i]);}

iString = buf.toString();}

return iString;}

// 下面四个getString措施,终极都必要调用getString(String key, Object[] args)

public String getString(String key, Object arg) {Object[] args = new Object[] {arg};

return getString(key, args);}

public String getString(String key, Object arg1, Object arg2) {

Object[] args = new Object[] {arg1, arg2};return getString(key, args);

}

public String getString(String key, Object arg1, Object arg2,Object arg3) {

Object[] args = new Object[] {arg1, arg2, arg3};return getString(key, args);

}

public String getString(String key, Object arg1, Object arg2,Object arg3, Object arg4) {

Object[] args = new Object[] {arg1, arg2, arg3, arg4};return getString(key, args);

}

// Hashtable掩护全部tomcat的StringManagerprivate static Hashtable managers = new Hashtable();

// 包管一个包一个StringManager,私有化构造函数+Hashtable掩护实现(值得进修)

// 从而避免大年夜量的StringManager实例化和销毁的操作,终究写日志属于对照频繁的操作。public synchronized static StringManager getManager(String packageName) {

// 用一个Hashtable来治理节制,包管每个包供给一个StringManagerStringManager mgr = (StringManager)managers.get(packageName);

// 属于这个包的Manager有了吗if (mgr == null) {

mgr = new StringManager(packageName);// 实例化好后,把它放进Hashtable里去,下次就不用实例化了

managers.put(packageName, mgr);}

return mgr;亚博老虎机平台靠谱吗}

}

免责声明:以上内容源自网络,版权归原作者所有,如有侵犯您的原创版权请告知,我们将尽快删除相关内容。

您可能还会对下面的文章感兴趣: