首页 > 编程语言 > 详细

spring不支持静态变量的注入解决方案

时间:2016-06-30 14:41:55      阅读:151      评论:0      收藏:0      [点我收藏+]

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;
    }

}

spring不支持静态变量的注入解决方案

原文:http://11494630.blog.51cto.com/11484630/1794495

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!