其他java类可通过调用getMessage()方法获取相应的message
package com.holike.lotter.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Component;
/**
* (初始想法)通过实现MessageSourceAware接口来实现一个msgSrc访问器
* (目前):通过访问本地资源文件来获取国际化信息;
* @author liweikun
*
*/
@Component
public class MsgSrcUtil implements MessageSourceAware {
protected static MessageSourceAccessor messages;
private static String MESSAGE_BASENAME = "language/message";
@Override
public void setMessageSource(MessageSource messageSource) {
messages = new MessageSourceAccessor(messageSource);
}
public static String getMessage(String key, Object[] params, Locale locale) {
String message = getStringOrNull(doGetBundle(MESSAGE_BASENAME, locale), key);
return message;
}
// 获取资源包的信息
private static String getStringOrNull(ResourceBundle bundle, String key) {
if (bundle.containsKey(key)) {
try {
return bundle.getString(key);
} catch (MissingResourceException ex) {
// Assume key not found for some other reason
// -> do NOT throw the exception to allow for checking parent
// message source.
}
}
return null;
}
// 获取资源包
private static ResourceBundle doGetBundle(String basename, Locale locale) {
return ResourceBundle.getBundle(basename, locale, new MessageSourceControl());
}
// 信息资源文件访问
private static class MessageSourceControl extends ResourceBundle.Control {
@Override
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader,
boolean reload) throws IllegalAccessException, InstantiationException, IOException {
// Special handling of default encoding
if (format.equals("java.properties")) {
String bundleName = toBundleName(baseName, locale);
final String resourceName = toResourceName(bundleName, "properties");
final ClassLoader classLoader = loader;
final boolean reloadFlag = reload;
InputStream stream;
try {
stream = AccessController.doPrivileged(new PrivilegedExceptionAction
() {
@Override
public InputStream run() throws IOException {
InputStream is = null;
if (reloadFlag) {
URL url = classLoader.getResource(resourceName);
if (url != null) {
URLConnection connection = url.openConnection();
if (connection != null) {
connection.setUseCaches(false);
is = connection.getInputStream();
}
}
} else {
is = classLoader.getResourceAsStream(resourceName);
}
return is;
}
});
} catch (PrivilegedActionException ex) {
throw (IOException) ex.getException();
}
if (stream != null) {
String encoding = Common.DEFAULT_ENCODING;
if (encoding == null) {
encoding = "ISO-8859-1";
}
try {
return loadBundle(new InputStreamReader(stream, encoding));
} finally {
stream.close();
}
} else {
return null;
}
} else {
// Delegate handling of "java.class" format to standard Control
return super.newBundle(baseName, locale, format, loader, reload);
}
}
protected ResourceBundle loadBundle(Reader reader) throws IOException {
return new PropertyResourceBundle(reader);
}
}
}