Spring中加载ApplicationContext.xml的几种方式
- 直接创建ApplicationContext上下文对象加载配置文件
ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");
ac.getBean():
- 创建类ContextLoaderListener实现ServletContextListener,加载配置文件。
public class ContextLoaderListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
//将spring的应用上下文对象存储到最大域ServletContext中
ServletContext servletContext = servletContextEvent.getServletContext();
ApplicationContext ac=new ClassPathXmlApplicationContext("ApplicationContext.xml");
servletContext.setAttribute("ac"