I18N工具类
public class I18N {
private static ApplicationContext ctx = BeanContext.ctx;
private static ReloadableResourceBundleMessageSource messageSource;
public static String getMessage(String key, Object... msgParam) {
if(ctx == null){
return key;
}
if(messageSource != null){
messageSource.getMessage(key, msgParam, key, Locale.CHINA);
}
Map<String, ReloadableResourceBundleMessageSource> map = ctx.getBeansOfType(ReloadableResourceBundleMessageSource.class);
if(map.size() == 0){
return key;
}
String beanKey = map.keySet().toArray(new String[]{})[0];
messageSource = map.get(beanKey);
return messageSource.getMessage(key, msgParam, key, Locale.CHINA);
}
}
BeanContext类
public class BeanContext implements ApplicationContextAware {
public static ApplicationContext ctx;
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
BeanContext.ctx = context;
}
public static Object getBean(String beanName) {
return ctx.getBean(beanName);
}
public static <T> T getBean(Class<T> clazz) {
return ctx.getBean(clazz);
}
public static <T> Map<String, T> getBeansOfType(Class<T> type) {
Map<String, T> map = ctx.getBeansOfType(type);
if (map == null) {
map = new HashMap<String, T>();
}
return map;
}
}