posts - 82, comments - 269, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Spring在Web中是如何载入相关的上下文环境

Posted on 2006-12-21 21:00 itspy 阅读(2916) 评论(5)  编辑  收藏 所属分类: JAVA技术

 在web.xml设置监听器:org.springframework.web.context.ContextLoaderListener
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 在此类(ContextLoaderListener)中有如下方法: 
 public void contextInitialized(ServletContextEvent event) {
  this.contextLoader = createContextLoader();
  this.contextLoader.initWebApplicationContext(event.getServletContext());
 }
 
 org.springframework.web.context.ContextLoader
 在此类中有如下变量: public static final String CONTEXT_CLASS_PARAM = "contextClass";
    public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";

 
 如下方法:initWebApplicationContext,createWebApplicationContext
 第一个方法调用了第二个方法,第二个方法则通过如下代码初始为了一个上下文环境
 String configLocation = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
 
 注:另外一点如下的DispatcherServlet设置会自动加载petstore-servlet.xml环境,原理我查了半天我也不知道怎么搞的.
 <servlet>
  <servlet-name>petstore</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>2</load-on-startup>
 </servlet>


评论

# re: Spring在Web中是如何载入相关的上下文环境  回复  更多评论   

2006-12-22 08:22 by 坏男孩
顶!??

# re: Spring在Web中是如何载入相关的上下文环境  回复  更多评论   

2006-12-22 09:34 by BeanSoft
哈哈, 把源码都查找一遍就 OK 了.

# re: Spring在Web中是如何载入相关的上下文环境  回复  更多评论   

2006-12-22 13:09 by xmlspy
另外一点如下的DispatcherServlet设置会自动加载petstore-servlet.xml环境,原理我查了半天我也不知道怎么搞的.


原因:

spring默认加载 你的工程名称-servlet.xml 文件,

# re: Spring在Web中是如何载入相关的上下文环境  回复  更多评论   

2006-12-22 19:15 by itspy
@xmlspy


spring默认加载 你的工程名称-servlet.xml 文件

这个我知道,我是想看看实现这段功能的代码,但没找到.

# re: Spring在Web中是如何载入相关的上下文环境  回复  更多评论   

2006-12-28 15:01 by baal
其实都是classload,app server会在启动时去先load这些类,凡是配置文件写全类名的都这样。哪些文件都是当参数传进去,或放在固定地方。

只有注册用户登录后才能发表评论。


网站导航: