*-servlet.xml解析

来源:互联网 发布:iphone设计软件 编辑:程序博客网 时间:2024/06/05 07:01

简单的*-servlet.xml文件如下

<beans xmlns="http://www.springframework.org/schema/beans"^M
        xmlns:context="http://www.springframework.org/schema/context"^M
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"^M
        xmlns:mvc="http://www.springframework.org/schema/mvc"^M
        xsi:schemaLocation="^M                                《=====所有namespace对应的xsd的位置
        http://www.springframework.org/schema/beans     ^M                                                                                                                                                
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd^M                                                                                                                                
        http://www.springframework.org/schema/mvc ^M                                                                                                                                                      
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd^M                                                                                                                                    
        http://www.springframework.org/schema/context ^M                                                                                                                                                  
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">^M
^M
        <context:component-scan base-package="com.mkyong.web" />^M  《==配置context namespace下的component-scan元素,具体作用可以

通过上面context对应的xsd来查询。对应文档中component-scan的说明如下:

scans the classpath for annotated components that will be auto-registered as
Spring beans. By default, the Spring-provided @Component, @Repository,
@Service, and @Controller stereotypes will be detected.

可以看出,此声明的作用是查找annotation base的组件声明。

base-package字段的说明如下:

The comma-separated list of packages to scan for annotated components.

因此,整句声明告诉spring在com.mkyong.web包中自动注册spring相关的组件。

^M
        <bean^M
                class="org.springframework.web.servlet.view.InternalResourceViewResolver">^M
                <property name="prefix">^M
                        <value>/WEB-INF/views/jsp/</value>^M
                </property>^M
                <property name="suffix">^M
                        <value>.jsp</value>^M
                </property>^M
        </bean>^M         《====声明了一个bean,具体语法在spring-beans-3.2.xsd.

首先bean的class属性的说明文档如下:

The fully qualified name of the bean's class, except if it serves only
        as a parent definition for child bean definitions.

指明此bean是哪个类型。后面两个property指定此bean需要注入的属性。此bean支持的注入

属性可以查询对应class的api,看支持哪些set get操作,以及对应属性的说明。说明如下:

  • public void setPrefix(String prefix)
    Set the prefix that gets prepended to view names when building a URL.
可以看出,prefix属性的作用是注入ViewResolver生成URL时,viewname前拼接的部分。
^M
        <mvc:resources mapping="/resources/**" location="/resources/" />^M 《== 声明了mvcnamespace的

resources,根据mvc namespace对应的xsd中说明解析如下:

    <xsd:element name="resources">
                <xsd:annotation>
<xsd:documentation
                                source="java:org.springframework.web.servlet.resource.ResourceHttpRequestHandler"><![CDATA[
        Configures a handler for serving static resources such as images, js, and, css files with cache headers optimized for efficient
        loading in a web browser. Allows resources to be served out of any path that is reachable via Spring's Resource handling.
                        ]]></xsd:documentation>  可以看出resources元素的作用是配置一个静态资源处理的handler。

mapping:The URL mapping pattern, within the current Servlet context, to use for serving resources from this handler, such as "/resources/**"

指定servlet context中,哪些资源通过此handler处理。

location:  <xsd:documentation><![CDATA[
The resource location from which to serve static content, specified at a Spring Resource pattern.
        Each location must point to a valid directory. Multiple locations may be specified as a comma-separated list,
        and the locations will be checked for a given resource in the order specified. For example, a value of
        "/, classpath:/META-INF/public-web-resources/" will allow resources to be served both from the web app
        root and from any JAR on the classpath  that contains a /META-INF/public-web-resources/ directory,
        with resources in the web app root taking precedence.
                                        ]]></xsd:documentation>

指定资源存储的位置。
 ^M
    <mvc:annotation-driven />^M  《== 声明annotaion-driven元素

<xsd:element name="annotation-driven">
                <xsd:annotation>
<xsd:documentation source="java:org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><![CDATA[
Configures the annotation-driven Spring MVC Controller programming model.
Note that, with Spring 3.0, this tag works in Servlet MVC only!
]]></xsd:documentation>

作用是声明此mvc controller是注解驱动的。
^M
</beans>


通过上面的分析可以看出:

此servlet.xml用于配置对应servlet工作需要知会spring 框架的一些信息:

1、需要从com.mkyong.web包中搜集此所有的注解,寻找到本servlet的组件。

2、本servlet返回的view name通过org.springframework.web.servlet.view.InternalResourceViewResolver 类的实例来生成

对应的View,view name和view url的关系是前面加/WEB-INF/views/jsp/ 后面加.jsp

3、servlet中所有/resources/ 开头的资源都在/resources目录下

4、本servlet使用注解驱动模式


至此,一个servlet正常工作需要的信息已经齐备。如果需要扩展功能,可以根据对应的xsd自己寻找合适的组件并添加到xml配置中。


个人理解,请谨慎接受,如果谬误欢迎指正。

0 0
原创粉丝点击