一、提倡的初始化方法:
《1》在独立应用程序中,获取ApplicationContext:
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.close();//释放资源
《2》在web环境中,获取ApplicationContext:
A)ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
B)String contextpath = "org.springframework.web.context.WebApplicationContext.ROOT";
WebApplicationContext context = request.getSession().getServletContext().getAttribute(contextpath);
二、不提倡的方法:(这种写法不仅仅耗内存,占资源,而且如果数据库连接太多,很容易造成系统运行的缓慢甚至stop!)
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
本文详细介绍了如何在独立应用程序和Web环境中获取ApplicationContext,并对比了推荐方法与不推荐的方法,旨在优化资源管理和提高系统性能。
3430

被折叠的 条评论
为什么被折叠?



