public class BeanHolder implements ApplicationContextAware { private static ApplicationContext context; private static HashMap<String, Object> beansCache = new HashMap<String, Object>(); @Override public void setApplicationContext(ApplicationContext context) throws BeansException { BeanHolder.context = context; } public static ApplicationContext getContext() { return BeanHolder.context; } public static Object getBean(String beanName) { notEmpty(beanName, "bean name is required"); checkState(context != null, "spring application context is not injected"); Object beanInstance = beansCache.get(beanName); if (null == beanInstance) { beanInstance = context.getBean(beanName); beansCache.put(beanName, beanInstance); } return beanInstance; } }
bean 获取入口
最新推荐文章于 2024-07-07 21:31:35 发布