從spring容器中獲取對象工具類

工具類:

public class SpringConfigTool implements ApplicationContextAware {
	private static ApplicationContext context = null;
	private static SpringConfigTool stools = null;

    public synchronized static SpringConfigTool init() {
		if (stools == null) {
			stools = new SpringConfigTool();
		}
		return stools;
	}

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		context = applicationContext;
	}

	public synchronized static Object getBean(String beanName) {
		return context.getBean(beanName);
	}
}

調用demo:

private static ConfigItemService configItemService = null;

public static Properties getWordConfig() {
		configItemService = (ConfigItemService) SpringConfigTool.getBean("configItemService");
}		
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章