您的位置:首页 > 编程语言 > Java开发

springMVC系列源码之配置文件深入——11

2014-02-24 15:58 477 查看

springMVC系列源码之配置文件深入——11

 

        摘要:上文中结合源码对springMVC初始化过程有了一定的了解、其中也涉及到了springMVC配置文件的加载、本文深入、具体的分析一下关于springMVC配置文件的一些东西。

 

1、简介

 

        springMVC默认的提供了一套配置策略、供我们快速上手、我们只需很少的配置文件即可实现springMVC的应用、但是这种配置有时候在我们需要更具体的操作某些类、方法时会有所不足、这时就要求我们对配置很熟悉、对内部构造也要有一定的理解。所以了解更详细点还是有必要的。

 

2、加载位置

 

        2.1 默认加载方式

                springMVC当然有默认的加载位置、是在/WEB-INFO 下的:servletName-servlet.xml 前面的servletName是你用来匹配下面的<servlet-name>servletName</ servlet-name >、举个例子:

web.xml中关于DispatcherServlet的配置:

<servlet>
<servlet-name>mySpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>mySpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

ServletName就是mySpringMVC、所以此时你的springMVC默认配置文件路径就是/WEB-INF/mySpringMVC-servlet.xml、否则就会报错:找不到配置文件“/WEB-INF/mySpringMVC-servlet.xml” 。

他是怎么实现这种加载的?通过源码分析:

       1、既然是配置文件、那么肯定是在初始化springMVC容器的时候加载的、根据前面一节内容、我们可以直接从FrameworkServlet的createWebApplicationContext(ApplicationContext parent)开始寻找、代码:

protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
Class<?> contextClass = getContextClass();
if (this.logger.isDebugEnabled()) {
this.logger.debug("Servlet with name '" + getServletName() +
"' will try to create custom WebApplicationContext context of class '" +
contextClass.getName() + "'" + ", using parent context [" + parent + "]");
}
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException(
"Fatal initialization error in servlet with name '" + getServletName() +
"': custom WebApplicationContext class [" + contextClass.getName() +
"] is not of type ConfigurableWebApplicationContext");
}
ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);

wac.setEnvironment(getEnvironment());
wac.setParent(parent);
wac.setConfigLocation(getContextConfigLocation());

configureAndRefreshWebApplicationContext(wac);

return wac;
}

        2、关键是红色的两句:

                第一句是当我们在web.xml文件中配置的时候配置contextConfigLocation的时候生效(后面有说明)

                第二句是当WebApplicationContext的属性设置好之后配置和refresh它。关键来了、那他是怎么样生成默认的配置路径的呢?既然上面的已经不生效、那我们只有跟进最后一行代码进入其方法中探寻了

        3、前面一大堆关于对其Id设置的代码我们可以暂时不看、wac.setNamespace(getNamespace()); 这句、跟进之后我们可以看到这个方法的说明:

/**
* Set the namespace for this web application context,
* to be used for building a default context config location.
* The root web application context does not have a namespace.
*/
void setNamespace(String namespace);

        就是为了创建默认配置文件路径而使用的方法。

        4、我们点其参数使用的方法: getNamespace(); 此方法的返回值在我们没有配置namespace的情况下返回的就是getServletName() +DEFAULT_NAMESPACE_SUFFIX "-servlet" 、前面提到过ServletName就是我们在web.xml中配置的映射到DispatcherServlet的Servlet的名字、到此我们的namespace就是mySpringMVC-servlet

        5、当我们refresh(不管WebApplicationContext是如何获取的、最后都会refresh来生成最终的WebApplicationContext)设置好属性的WebApplicationContext时、就会使用默认生成的配置文件位置、springMVC的上下文确切的讲应该是XMLWebApplicationContext、下面的方法就是其内部的方法:

protected String[] getDefaultConfigLocations() {
if (getNamespace() != null) {
return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX};
}
else {
return new String[] {DEFAULT_CONFIG_LOCATION};
}
}
也就是根据上面的namespace得到的最终默认加载文件具体路径——/WEB-INFO/mySpringMVC-servlet.xml!

 

        2.2 使用指定的文件位置、文件名:

        在web.xml中我们在配置DispatcherServlet时可以为其指定一个contextConfigLocation:这个属性是FrameworkServlet中的属性、在createWebApplicationContext(ApplicationContextparent)中会将此属性设置成ConfigurableWebApplicationContext一个属性用于加载配置文件。当我们refresh设置好属性的WebApplicationContext时、就会使用我们指定的位置的、指定名称的配置文件。
     


        2.3 springMVC-servlet.xml中配置项部分解析

            具体的都在配置中有详细的说明:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
<!-- 注解扫描包 -->
<context:component-scan base-package="com.chy.ssh.web.annotation" />

<!-- 可以配置多个、多个之间用,分开
<context:component-scan base-package="com.chy.ssh.web,com.chy.ssh.utils" />
-->

<!-- 开启注解, 即我们使用springMVC的注解模式 -->
<mvc:annotation-driven />

<!-- 手动注入上面开启注解所需要的两个必须的bean。3.2版本以后就使用下面的两个类来代替DefaultAnnotationHandlerMapping、AnnotationMethodHandlerAdapter -->
<!-- 将request中的URL映射到类级别、 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
<property name="interceptors">
<list>
<ref bean="myIterceptors1" />   <!-- 自定义的拦截器,可定义多个-->
<ref bean="myIterceptors2" />
<ref bean="myIterceptors3" />
<ref bean="myIterceptors4" />
</list>
</property>
</bean>
<!-- 将request中的URL映射到类级别下的方法级别 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="byteArray" />
<ref bean="string" />
<ref bean="resource" />
<ref bean="source" />
<ref bean="xmlAwareForm" />
<ref bean="jaxb2RootElement" />
<ref bean="jackson" />
</list>
</property>
</bean>
<bean id="byteArray" class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean id="string" class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean id="resource" class="org.springframework.http.converter.ResourceHttpMessageConverter" />
<bean id="source" class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
<bean id="xmlAwareForm" class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
<bean id="jaxb2RootElement" class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
<bean id="jackson" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

<!-- 静态资源访问 与下面的resources二选一 -->
<mvc:default-servlet-handler />

<!-- 静态资源访问 与下面的resources二选一 -->
<!-- <mvc:resources location="/css/" mapping="/css/**"/> -->
<!-- <mvc:resources location="/js/" mapping="/js/**"/> -->
<!-- <mvc:resources location="/images/" mapping="/images/**"/> -->

<!-- 拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 是从项目路径开始的只需配置URL中/springMVC_spring_hibernate后面的我们想要拦截的URL -->
<!-- 灵活性大大增强、我们可以给具体到某个Controller的某个方法配置专门的拦截器、也可以给符合我们制定的要求的request配置拦截器 -->
<!-- 下面这个path、就会拦截/springMVC_spring_hibernate/user/toUser这个URL -->
<mvc:mapping path="/user/toUser" />
<bean class="com.chy.ssh.web.annotation.interceptor.MyHandlerInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>

<!-- 视图解析器、使用指定的视图前缀、后缀、View来解析我们在Controller返回的视图名称所对应的resources、这样就避免了直接访问jsp页面
下面配置的类继承与UrlBasedViewResolver、将解析的结果作为一个View返回、 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
</bean>

<!-- 上传文件配置 、多了个多请求的处理、目的是为了支持多文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="10485760000" />
<property name="maxInMemorySize" value="40960" />
</bean>
</beans>


更多内容: springMVC系列之目录——00

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息