@Component public class SpringUtil implements ApplicationContextAware { /** * 当前IOC */ private static ApplicationContext applicationContext; /** * 设置当前上下文环境,此方法由spring自动装配 */ @Override public void setApplicationContext(ApplicationContext arg0) throws BeansException { SpringUtil.applicationContext = arg0; } public static ApplicationContext getApplicationContext() { return applicationContext; } /** * 从当前IOC获取bean * @param id bean的id * @return */ public static Object getObject(String id) { return applicationContext.getBean(id); } /** * 从当前IOC获取bean * @param id bean的id * @return * @return * @return */ public static <T> T getObject(String id,Class<T> c) { return applicationContext.getBean(id,c); } /** * 通过类型获取第一个实例 */ public static <T> T getBeanByType(Class<T> c) { return applicationContext.getBean(c); } }
Spring IOC上下文工具类
最新推荐文章于 2022-09-14 10:48:47 发布