怎么獲取Spring的ApplicationContext?
方法一:在初始化時保存ApplicationContext對象 ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");ac.getBean("beanId"); 這種方式適用于采用Spring框架的獨(dú)立應(yīng)用程序,需要程序通過配置文件手工初始化Spring的情況。 方法二:通過Spring提供的工具類獲取ApplicationContext對象 import org.springframework.web.context.support.WebApplicationContextUtils;ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc);ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc);ac1.getBean("beanId");ac2.getBean("beanId");