详解contextConfigLocation

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

[html]  view plain copy
  1. <context-param>  
  2.         <param-name>contextConfigLocation</param-name>  
  3.         <param-value>  
  4.         /WEB-INF/conf/application-*.xml  
  5.         </param-value>  
  6.     </context-param>  
来初始化各个spring的配置文件,但是我们只是知道这段代码的功能, 并不是很清楚我们配置了这段代码之后为什么就能去初始化配置文件。当然我们还会加上:

[html]  view plain copy
  1. <listener>  
  2.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  3.     </listener>  
这一个listener,我首先就会想contextConfigLocation这个一定能在ContextLoaderListener这个类当中找到,打开了源码,这个listener是实现了ServletContextListener这个接口的,这个接口只有两个方法:

[html]  view plain copy
  1. public interface ServletContextListener  
  2.     extends EventListener  
  3. {  
  4.   
  5.     public abstract void contextInitialized(ServletContextEvent servletcontextevent);  
  6.   
  7.     public abstract void contextDestroyed(ServletContextEvent servletcontextevent);  
  8. }  
而且它是继承了EventListener这个接口的,打开这个接口的代码让我大吃一惊,里面没有方法啥都没有:

[html]  view plain copy
  1. package java.util;  
  2.   
  3.   
  4. public interface EventListener  
  5. {  
  6. }  
而且还是java.util包下的,并不是spring之中的东西。

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

[html]  view plain copy
  1. public void contextInitialized(ServletContextEvent event)  
  2.     {  
  3.         contextLoader = createContextLoader();  
  4.         contextLoader.initWebApplicationContext(event.getServletContext());  
  5.     }  
方法中有个createContextLoader方法:

[html]  view plain copy
  1. protected ContextLoader createContextLoader()  
  2.     {  
  3.         return new ContextLoader();  
  4.     }  
这个方法返回了一个ContextLoader实例,进入到ContextLoader类中,按ctrl+f来寻找contextConfigLocation,这时没有出现电脑的咚的声音,找到了它:

[html]  view plain copy
  1. protected WebApplicationContext createWebApplicationContext(ServletContext servletContext, ApplicationContext parent)  
  2.         throws BeansException  
  3.     {  
  4.         Class contextClass = determineContextClass(servletContext);  
  5.         if(!(org.springframework.web.context.ConfigurableWebApplicationContext.class).isAssignableFrom(contextClass))  
  6.         {  
  7.             throw new ApplicationContextException("Custom context class [" + contextClass.getName() + "] is not of type [" + (org.springframework.web.context.ConfigurableWebApplicationContext.class).getName() + "]");  
  8.         } else  
  9.         {  
  10.             ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext)BeanUtils.instantiateClass(contextClass);  
  11.             wac.setParent(parent);  
  12.             wac.setServletContext(servletContext);  
  13.             wac.setConfigLocation(servletContext.getInitParameter("contextConfigLocation"));  
  14.             customizeContext(servletContext, wac);  
  15.             wac.refresh();  
  16.             return wac;  
  17.         }  
  18.     }  
通过代码,ConfigurableWebApplicationContext设置了从servletContext获取到的参数的值,再进入ConfigurableWebApplicationContext的代码中,它只是一个接口,进入StaticWebApplicationContext的setConfigLocation方法:

[html]  view plain copy
  1. public void setConfigLocation(String configLocation)  
  2.     {  
  3.         if(configLocation != null)  
  4.             throw new UnsupportedOperationException("StaticWebApplicationContext does not support config locations");  
  5.         else  
  6.             return;  
  7.     }  
这个方法中很奇怪,当参数不为空就抛出异常,查看spring的文档: The  StaticWebApplicationContext  class does not support this method.说是此类不支持这个方法,这下子又卡住了。又要退回去,看这句:

[html]  view plain copy
  1. ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext)BeanUtils.instantiateClass(contextClass);  
spring使用BeanUtils来初始化contextClass这个类实例,contextClass是通过以下代码得到的:

[html]  view plain copy
  1. protected Class determineContextClass(ServletContext servletContext)  
  2.         throws ApplicationContextException  
  3.     {  
  4.         String contextClassName = servletContext.getInitParameter("contextClass");  
  5.         if(contextClassName != null)  
  6.             try  
  7.             {  
  8.                 return ClassUtils.forName(contextClassName);  
  9.             }  
  10.             catch(ClassNotFoundException ex)  
  11.             {  
  12.                 throw new ApplicationContextException("Failed to load custom context class [" + contextClassName + "]", ex);  
  13.             }  
  14.         contextClassName = defaultStrategies.getProperty((org.springframework.web.context.WebApplicationContext.class).getName());  
  15.         try  
  16.         {  
  17.             return ClassUtils.forName(contextClassName, (org.springframework.web.context.ContextLoader.class).getClassLoader());  
  18.         }  
  19.         catch(ClassNotFoundException ex)  
  20.         {  
  21.             throw new ApplicationContextException("Failed to load default context class [" + contextClassName + "]", ex);  
  22.         }  
  23.     }  
这里使用了反射,再来看BeanUtils的instantiateClass方法:

[html]  view plain copy
  1. return instantiateClass(clazz.getDeclaredConstructor((Class[])null), null);  

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

[html]  view plain copy
  1. ReflectionUtils.makeAccessible(ctor);  
  2.             return ctor.newInstance(args);  
ctor是通过反射得到的contextClass的构造方法,args是构造方法当中的参数。这里为null,说明new了contextClass的无参构造方法。

这时又要退回到determineContextClass 这个方法中,我们主要看:

[html]  view plain copy
  1. contextClassName = defaultStrategies.getProperty((org.springframework.web.context.WebApplicationContext.class).getName());  
这句代码,我们可以猜它是通过Properties的getProperty方法得到 WebApplicationContext 的实例,这时我们又到了WebApplicationContext 这个接口当中,这个接口继承了ApplicationContext这个接口,我们都知道我们进行spring开发都会通过Application ctx=new FileSystemXmlApplicationContext("beans.xml");或ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");或ServletContext servletContext = request.getSession().getServletContext();ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);这三种方法获得一个ApplicationContext,然后就可以对配置文件当中的bean进行操作了。所以这里我们基本上已经搞清楚初始化spring配置文件的流程了。

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


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: SpringMVC是一个基于MVC架构的Web框架,它可以帮助我们快速地开发Web应用程序。在使用SpringMVC时,我们需要在web.xml文件中进行配置。下面是SpringMVC web.xml配置的详解: 1. 配置DispatcherServlet 在web.xml文件中,我们需要配置一个Servlet来处理所有的请求。这个Servlet就是DispatcherServlet。我们需要在web.xml文件中添加以下配置: ``` <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> ``` 在这个配置中,我们指定了Servlet的名称为dispatcherServlet,Servlet的类为org.springframework.web.servlet.DispatcherServlet。我们还指定了一个初始化参数contextConfigLocation,它指定了SpringMVC的配置文件的位置。在这个例子中,配置文件的位置为/WEB-INF/springmvc-servlet.xml。最后,我们还指定了Servlet的启动顺序为1。 2. 配置Servlet映射 在web.xml文件中,我们还需要配置Servlet的映射。我们需要将所有的请求都映射到DispatcherServlet上。我们需要在web.xml文件中添加以下配置: ``` <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ``` 在这个配置中,我们将Servlet的名称设置为dispatcherServlet,将URL的模式设置为/。这样,所有的请求都会被映射到DispatcherServlet上。 3. 配置字符编码过滤器 在web.xml文件中,我们还需要配置一个字符编码过滤器。这个过滤器可以确保请求和响应的字符编码都是正确的。我们需要在web.xml文件中添加以下配置: ``` <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ``` 在这个配置中,我们指定了一个字符编码过滤器。我们将过滤器的名称设置为encodingFilter,将过滤器的类设置为org.springframework.web.filter.CharacterEncodingFilter。我们还指定了字符编码为UTF-8,并强制使用UTF-8编码。最后,我们将过滤器映射到所有的URL上。 4. 配置静态资源处理器 在web.xml文件中,我们还需要配置一个静态资源处理器。这个处理器可以帮助我们处理静态资源,如CSS、JavaScript和图片等。我们需要在web.xml文件中添加以下配置: ``` <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> <url-pattern>*.js</url-pattern> <url-pattern>*.png</url-pattern> <url-pattern>*.jpg</url-pattern> <url-pattern>*.gif</url-pattern> </servlet-mapping> ``` 在这个配置中,我们将所有的静态资源的URL模式都映射到默认的Servlet上。这样,当我们访问静态资源时,就可以直接访问它们了。 以上就是SpringMVC web.xml配置的详解。通过这些配置,我们可以快速地搭建一个SpringMVC应用程序。 ### 回答2: Spring MVC是一种常见的Java Web开发框架,供开发人员快速构建Web应用程序。在使用Spring MVC框架进行开发时,需要在Web.xml文件中进行一些配置来确保正确运行。下面是一个关于Spring MVC Web.xml配置的详细解释。 1. 配置DispatcherServlet 在Web.xml文件中,需要配置DispatcherServlet,这是Spring MVC中的核心Servlet,它接收所有来自客户端的请求,然后将请求分派到相应的控制器上。配置DispatcherServlet时,需要指定Servlet名称、Servlet类、URL模式。 <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 2. 配置Spring ContextLoaderListener Spring MVC框架需要依赖Spring Framework,因此需要在Web.xml中配置一个Spring ContextLoaderListener来加载Spring上下文。Spring ContextLoaderListener会在Web应用程序启动时自动加载Spring配置文件,并将所有Spring bean加载到应用程序上下文中。 <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 3. 配置字符集编码过滤器 为了确保应用程序能够处理国际化的文本消息,需要配置一个字符集编码过滤器,将接收到的请求和响应都设置为指定的字符集编码。 <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 4. 配置Spring MVC资源处理器 Spring MVC框架在处理Web请求时,需要使用资源处理器来加载和处理请求中使用的所有静态文件,如CSS、JavaScript、图片等。可以通过以下方式配置资源处理器: <mvc:resources mapping="/resources/**" location="/resources/" /> 5. 配置SpringMVC的视图解析器 Spring MVC框架使用视图解析器来获取请求处理器返回的视图名称,并将其解析为可执行的视图。通过以下方式配置视图解析器: <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> 以上是关于Spring MVC Web.xml配置的详细解释。这些配置项将确保Spring MVC应用程序正常运行并且能够接收和响应客户端的请求。当然,Web.xml配置也有一些其他的配置项,如果您需要自定义一些配置,可以参考相关的文档或向社区寻求帮助。 ### 回答3: Spring MVC是一种基于Java的Web框架,可以用于快速开发Web应用程序。在Web应用程序中,Web.xml是配置文件,用于描述Web应用程序的URL映射和其他关键元素。在SpringMVC中,Web.xml也需要进行配置,以确保SpringMVC能够正确运行。下面我们来详细介绍SpringMVC中Web.xml的配置。 分为以下几个方面进行介绍: 一、基本介绍: SpringMVC中的Web.xml文件主要用于注册SpringMVC模块,并将请求路径与对应的Controller进行映射。Web.xml文件配置了Servlet,Filter,Listener等元素,这些元素都会被由Servlet容器Tomcat读取并执行。 二、配置DispatcherServlet: 处理所有类型请求,即所有请求都交由DispatcherServlet进行处理。 DispatcherServlet有两个参数: 1、contextConfigLocation:指定Spring的上下文配置文件,比如applicationContext.xml等。 2、springmvc:指定Spring MVC 配置文件的路径,比如springmvc-servlet.xml等。 <!-- SpringMVC 配置 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 三、配置CharacterEncodingFilter: CharsetEncodingFilter是在Spring MVC中用于对请求进行编码和解码的过滤器,它可以使用init-param来设置特定的编码。 <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 四、配置Spring IoC容器: 我们可以使用ContextLoaderListener来配置Spring IoC容器。 <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> 五、配置spring-security: 创建spring-security.xml文件用于配置spring-security. <context:annotation-config/> <context:component-scan base-package="com.spring.bean"/> <http> <intercept-url pattern="/**" access="ROLE_USER" /> <access-denied-handler error-page="/403" /> <csrf disabled="true"/> </http> <authentication-manager> <authentication-provider user-service-ref="myUserDetailsService" /> </authentication-manager> 总结: Web.xml文件配置了Servlet,Filter,Listener等元素,这些元素都会被由Servlet容器Tomcat读取并执行。主要是通过配置DispatcherServlet、CharsetEncodingFilter、Spring IoC容器、spring-security等,来实现SpringMVC的配置。除此之外,Web.xml的配置还有很多,以上仅是几个常用的配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值