一痹籍、提倡的初始化方法:
《1》在獨(dú)立應(yīng)用程序中擎析,獲取ApplicationContext:
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.close();//釋放資源
《2》在web環(huán)境中罚拟,獲取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);
二付魔、不提倡的方法:(這種寫法不僅僅耗內(nèi)存生百,占資源,而且如果數(shù)據(jù)庫(kù)連接太多项棠,很容易造成系統(tǒng)運(yùn)行的緩慢甚至stop1ā)
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");