Spring+SpringMVC+Hibernate整合。
关于Spring配置文件的加载问题
要新建一个ServletContextListener在ServletContext初始化时获取ApplicationContext。
public void contextInitialized(ServletContextEvent arg0) {
ServletContext sct = arg0.getServletContext();
String congif = sct.getInitParameter("contextConfigLocation");
ApplicationContext act = new ClassPathXmlApplicationContext(congif);
sct.setAttribute("ApplicationContext", act);
}
然后在Controller中获取这个ApplicationContext
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
ServletContext servletContext = webApplicationContext.getServletContext();
ApplicationContext act = (ApplicationContext) servletContext.getAttribute("ApplicationContext");
OperationDb operationDb = (OperationDb) act.getBean("operationDb");
网友评论