ApplicationContext上下文

一、提倡的初始化方法:
《1》在独立应用程序中,获取ApplicationContext:
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.close();//释放资源
《2》在web环境中,获取ApplicationContext:
1:ServletContext servletContext = request.getSession().getServletContext();              
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
2:String contextpath = "org.springframework.web.context.WebApplicationContext.ROOT";
WebApplicationContext context = request.getSession().getServletContext().getAttribute(contextpath);   
二、不提倡的方法:(这种写法不仅仅耗内存,占资源,而且如果数据库连接太多,很容易造成系统运行的缓慢甚至stop!)
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

 

package ioc.testing;
//import省略  
public class TesMain {  
public static void main(String[] args) {  
AbstractApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");   
         ac.refresh();//触发ContextRefreshedEvent事件              
         ac.start(); //触发ContextStartedEvent事件  
         ac.stop();  //触发ContextStoppedEvent事件          
         ac.close(); //关闭容器,触发ContextClosedEvent事件  
     }  
}  

 

你可能感兴趣的:(ApplicationContext上下文)