Java Spring WEB应用实例化如何实现
1.前面讲解的都是通过直接读取配置文件,进行的实例化ApplicationContext
AbstractApplicationContextapp=newClassPathXmlApplicationContext("beans.xml");
下面讲解直接通过配置文件进行初始化。
2.web.xml
contextConfigLocation classpath:beans.xml org.springframework.web.context.ContextLoaderListener
这样,ApplicationContext便已经实例化了,默认就直接加载了beans.xml里面的内容。
来看看底层的代码,类ContextLoaderListener中有个初始化方法
publicvoidcontextInitialized(ServletContextEventevent){
this.contextLoader=createContextLoader();
if(this.contextLoader==null){
this.contextLoader=this;
}
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
进入initWebApplicationContext方法:
ApplicationContextparent=loadParentContext(servletContext); //Storecontextinlocalinstancevariable,toguaranteethat //itisavailableonServletContextshutdown. this.context=createWebApplicationContext(servletContext,parent);
这句也就是容器加载的结果。
1和2一个是java代码一个是xml代码,不过实现的效果都是一样的。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短