關於ApplicationContext的初始化

1、提倡的初始化方法:
《1》在獨立應用程序中,獲取ApplicationContext:
          AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
         context.close();//釋放資源
《2》在web環境中,獲取ApplicationContext:

      A)ServletContext servletContext = request.getSession().getServletContext();               mysql

         ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext); web

      B)String contextpath = "org.springframework.web.context.WebApplicationContext.ROOT";spring

         WebApplicationContext context = request.getSession().getServletContext().getAttribute(contextpath);   sql


2、不提倡的方法:(這種寫法不單單耗內存,佔資源,並且若是數據庫鏈接太多,很容易形成系統運行的緩慢甚至stop!)數據庫

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");app

相關文章
相關標籤/搜索