*-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.
^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配置中。
个人理解,请谨慎接受,如果谬误欢迎指正。
- *-servlet.xml解析
- Servlet中web.xml的配置解析
- XML--两个系统采用servlet发送及解析XML
- servlet 解析dom4j解析xml文件 ajax省市联动
- Servlet中输出XML解析中文乱码问题解决
- AJAX用jquery解析servlet返回回来的XML 数据
- Servlet中web.xml的配置解析(续)
- Servlet中输出XML解析中文乱码问题解决
- MVC servlet 目录上下文更改 部署文件(web.xml解析)
- 部署描述文件web.xml解析(二) Servlet生命周期
- Flex与servlet数据交互3(xml解析)
- 【Spring实战】----springMVC配置文件*-servlet.xml的解析
- java EE开发之Servlet第五课:xml解析
- Servlet解析
- Servlet解析
- Servlet使用XMLConfiguration解析在线XML(或本地文件系统中的XML文件)
- web-servlet servlet源码解析
- 在Servlet应用中用XML实现数据包装和用XSLT解析数据
- 测试1224
- 欢迎使用CSDN-markdown编辑器
- 获得iphone手机动态ip地址
- 协方差的意义和计算公式
- log4J 日志信息log4j.properties配置说明
- *-servlet.xml解析
- LeetCode-13-Roman to Integer(number/模拟)-Easy
- 抽象类和接口小结
- iOS 学会使用通知(notification)
- EBS Form个性化的工作原理
- Polya定理
- 一道面试题引发的对android中context的研究(一)-SharedPreference是否可以将文件保存在SD卡上
- php项目打包
- 瀑布流