关于ApplicationContext的初始化

一、提倡的初始化方法:

《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");

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容