`
suoyihen
  • 浏览: 1362728 次
文章分类
社区版块
存档分类
最新评论

详解contextConfigLocation

 
阅读更多

spring的应用初始化流程一直没有搞明白,刚刚又碰到了相关的问题。决定得好好看看这个流程。我们在开发spring的项目当中基本上都会在web.xml通过:

来初始化各个spring的配置文件,但是我们只是知道这段代码的功能, 并不是很清楚我们配置了这段代码之后为什么就能去初始化配置文件。当然我们还会加上:

这一个listener,我首先就会想contextConfigLocation这个一定能在ContextLoaderListener这个类当中找到,打开了源码,这个listener是实现了ServletContextListener这个接口的,这个接口只有两个方法:

而且它是继承了EventListener这个接口的,打开这个接口的代码让我大吃一惊,里面没有方法啥都没有:

而且还是java.util包下的,并不是spring之中的东西。

这样找了之后没有找到,往回退到ContextLoaderListener这个类的方法上,contextInitialized方法是用来初始化上下文的:

方法中有个createContextLoader方法:

这个方法返回了一个ContextLoader实例,进入到ContextLoader类中,按ctrl+f来寻找contextConfigLocation,这时没有出现电脑的咚的声音,找到了它:

通过代码,ConfigurableWebApplicationContext设置了从servletContext获取到的参数的值,再进入ConfigurableWebApplicationContext的代码中,它只是一个接口,进入StaticWebApplicationContext的setConfigLocation方法:

这个方法中很奇怪,当参数不为空就抛出异常,查看spring的文档:TheStaticWebApplicationContextclass does not support this method.说是此类不支持这个方法,这下子又卡住了。又要退回去,看这句:

spring使用BeanUtils来初始化contextClass这个类实例,contextClass是通过以下代码得到的:

这里使用了反射,再来看BeanUtils的instantiateClass方法:

通过反射得到contextClass的构造方法。下面是instantiateClass方法的重载,主要是下面两句代码:

ctor是通过反射得到的contextClass的构造方法,args是构造方法当中的参数。这里为null,说明new了contextClass的无参构造方法。

这时又要退回到determineContextClass 这个方法中,我们主要看:这句代码,我们可以猜它是通过Properties的getProperty方法得到WebApplicationContext 的实例,这时我们又到了WebApplicationContext 这个接口当中,这个接口继承了ApplicationContext这个接口,我们都知道我们进行spring开发都会通过Application ctx=new FileSystemXmlApplicationContext("beans.xml");或ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");ServletContextservletContext =request.getSession().getServletContext();ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);这三种方法获得一个ApplicationContext,然后就可以对配置文件当中的bean进行操作了。所以这里我们基本上已经搞清楚初始化spring配置文件的流程了。

总结:通过查看这几个类的源代码,java的反射使用范围之广再次体现出来。如看了之后觉得有错误或者不同意见,欢迎提出来,我也是第一次才研究这个问题。














分享到:
评论

相关推荐

    Springmvc-annotation_contextConfigLocation

    SpringMVC配置控制器有两种方式,常用的是通过WEB-INF下的[servlet-name]-servlet.xml,本项目采用的是另外一种通过配置配置文件路径的方式来实现Springmvc的配置。

    JAVA web.xml配置详解

    <param-name>contextConfigLocation <!-- 设定Context名称的值 --> <param-value>/WEB-INF/spring-config.xml <!-- 备注:此所设定的参数,在JSP网页中可以使用下列方法来取得:${initParam.param_name} 若...

    xfire的使用详解

    <param-name>contextConfigLocation <param-value>classpath:spring/app.xml,classpath:org/codehaus/xfire/spring/xfire.xml <!--配置xfire ws--> <servlet-name>XFireServlet <servlet-class>org....

    web.xml中如何设置配置文件的加载路径实例详解

    web.xml中如何设置配置文件的加载路径实例详解 web应用程序通过Tomcat等容器启动时,会首先加载web.xml文件,通常我们工程中的各种配置文件,如日志、数据库、spring的文件等都在此时被加载,下面是两种常用的配置...

    MyContextLoaderPlugIn.jar

    使用SSH构建项目的时候遇到了contextConfigLocation 和Spring中OpenSessionInView由于org.springframework.web.struts.ContextLoaderPlugIn中保存同一个对象的名不同导致openSessionInView失效 稍微修改后在struts-...

    ssh框架的搭建

    <param-name>contextConfigLocation /WEB-INF/applicationContext-*.xml,/WEB-INF/action-servlet.xml <param-name>log4jConfigLocation <param-value>/WEB-INF/log4j.properties <!-- Listener ...

    spring和hibernate整合

    <param-name>contextConfigLocation <param-value>/WEB-INF/spring/*.bean.xml org.springframework.web.context.ContextLoaderListener <listener> <listener-class>org.springframework....

    基于java的企业级应用开发:Spring的核心容器.ppt

    Web服务器实例化ApplicationContext容器时,通常会使用ContextLoaderListener来实现,此种方式只需要在web.xml中添加如下代码: <context-param> <param-name>contextConfigLocation</param-name> <param-value> ...

    SpringMVC源码剖析(三)- DispatcherServlet的初始化流程1

    声明和设置contextConfigLocation参数,但是将其声明为Resource类型,同样能够成功获取。励读者们继承HttpServletBean写个测

    基于java的企业级应用开发:Spring MVC的核心类和注解.ppt

    * * * * Spring MVC的核心类和...<param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <se

    springmvc小项目

    springmvc比较全面的教程,聚集了一般项目中会用到的注释控制之流。在web.xml中切换contextConfigLocation文件可以访问不同控制器。

    一个简单的Acegi入门实例

    <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/acegi-config.xml</param-value> </context-param> <!-- Acegi 的 Filter Chain 代理 --> <filter> <filter-name>

    企业人力资源管理项目SSH+EXT+MySQL+MD5

    file-list标签中加入一下代码 struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter struts2 /* org.springframework.web.context.ContextLoaderListener contextConfigLocation ...

    Spring的web配置

    springmvc的资源配置:Spring 刷新Introspector防止内存泄露,Spring MVC配置;Character Encoding ... <param-name>contextConfigLocation <param-value>classpath*:applicationContext-*.xml </context-param>

    maven项目整合ssh

    <listener-class>org.springframework.web.context.ContextLoaderListener </listener> ...<param-name>contextConfigLocation <param-value>classpath:applicationContext.xml </context-param>

    ssh分模块maven项目

    <listener-class>org.springframework.web.context.ContextLoaderListener </listener> ...<param-name>contextConfigLocation <param-value>classpath:applicationContext.xml </context-param>

    DOS命令使用方法(超全).

    <param-name>contextConfigLocation <param-value>classpath*:applicationContext-*.xml <!-- 这是spring 监听 --> <listener-class>org.springframework.web.context.ContextLoaderListener <!...

    CXF WebService整合Spring示例工程代码demo

    <param-name>contextConfigLocation <param-value>classpath*:applicationContext-server.xml <listener-class>org.springframework.web.util.IntrospectorCleanupListener <servlet-name>...

    Spring MVC Demo

    <param-name>contextConfigLocation <param-value>classpath:applicationContext-*.xml <!--查找这里可以配置多个,用逗号分割或者用通配符*也就是第二行 <param-value>/WEB-INF/mvc.xml,/WEB-INF/mvc1.xml ...

    spring-simple-web:使用 Spring Framework 的简单 Web (WAR) 项目

    Spring Web 侦听器使用web.xml的contextConfigLocation上下文参数进行初始化。 此设置的默认值为WEB-INF/applicationContext.xml 。 我们已将此值定义为classpath:applicationContext.xml 。 这个文件代表了 Spring...

Global site tag (gtag.js) - Google Analytics