Spring configuration file header configuration parsing

Recently, because the company's project uses springmvc, I have to learn it myself. I believe that everyone should have seen a lot of spring configuration files, so do you know what the pile of things in the header of the configuration file is? Below I will share some of my insights with you:

       First take a look at a familiar header configuration file:

[html]  view plain copy  
 
  1. <?xmlversion="1.0"encoding="UTF-8"?>    
  2. <beansxmlns="http://www.springframework.org/schema/beans"   
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:p="http://www.springframework.org/schema/p"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"  
  6.     xmlns:context="http://www.springframework.org/schema/context"  
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.                         http://www.springframework.org/schema/beans/spring-beans.xsd  
  9.                         http://www.springframework.org/schema/mvc  
  10.                         http://www.springframework.org/schema/mvc/spring-mvc.xsd  
  11.                         http://www.springframework.org/schema/context  
  12.                         http://www.springframework.org/schema/context/spring-context.xsd">  
  13.       
  14.     <!-- Define the controller scan package -->  
  15.     <context:component-scanbase-package="com.example.controller"/>    
  16.     <mvc:annotation-driven/>   
  17.     <!-- Process static resources -->  
  18.     <mvc:default-servlet-handler/>  
  19.     <!-- configure view resolver -->  
  20.     <beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">    
  21.        <propertyname="viewClass"value="org.springframework.web.servlet.view.JstlView"/>    
  22.        <propertyname="prefix"value="/WEB-INF/jsp/"/>    
  23.        <propertyname="suffix"value=".jsp"/>    
  24.     </bean>  
  25.       
  26. </beans>  

1.xmlns= http://www.springframework.org/schema/beans   and xmlns:xsi= http://www.w3.org/2001/XMLSchema-instance  is a must have same for all spring configuration files

2.xmlns:xxx This is the namespace of xml. The namespace is not expanded. The simple understanding is actually the content of which modules of spring you want to use. The first reason is xmlns="http://www.springframework .org/schema/beans is necessary because beans are the foundation of spring. If there is no such thing as a spring configuration file, there are also xmlns:p, xmlns:mvc, xmlns in this configuration file. :context These three namespaces, with which you can use functions such as <context:component /> <mvc:annotation-driven />, if there is no such namespace, use <context:component /> <mvc:annotation -driven /> These functions will report errors!

3.下面就是xsi:schemaLocation了,这个是用来做什么的呢?其实是为上面配置的命名空间指定xsd规范文件,这样你在进行下面具体配置的时候就会根据这些xsd规范文件给出相应的提示,比如说每个标签是怎么写的,都有些什么属性是都可以智能提示的,以防配置中出错而不太容易排查,在启动服务的时候也会根据xsd规范对配置进行校验。

4.配置文件中.XSD文件的uri是http://www.springframework.org/schema/.............xsd 那么问题来了,真的要到网上去找这个.XSD文件吗?当然不是(package explore view)

打开spring-webmvc-4.2.3.RELEASE.jar>>META-INF>>spring.schemas  会看到以下内容:

http\://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd=org/springframework/web/servlet/config/spring-mvc-3.0.xsd
http\://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd=org/springframework/web/servlet/config/spring-mvc-3.1.xsd
http\://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd=org/springframework/web/servlet/config/spring-mvc-3.2.xsd
http\://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd=org/springframework/web/servlet/config/spring-mvc-4.0.xsd
http\://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd=org/springframework/web/servlet/config/spring-mvc-4.1.xsd
http\://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd=org/springframework/web/servlet/config/spring-mvc-4.2.xsd
http\://www.springframework.org/schema/mvc/spring-mvc.xsd=org/springframework/web/servlet/config/spring-mvc-4.2.xsd

When you see these http://www.springframework.org/schema/.............xsd   content, everyone should understand, in fact http\://www.springframework.org/schema/ The location where mvc/spring-mvc.xsd really points is
org/springframework/web/servlet/config/spring-mvc-4.2.xsd, then open this uri to see what is there

Sure enough, the xsd file is here.... The others are also deduced by analogy

 

The language organization is a bit messy, sorry, if you look carefully, it is actually quite regular. You can also save a spring file that you have configured yourself, and then copy it directly when you use it later!

I just wrote it here, I hope it can help friends who just learned spring! !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326226052&siteId=291194637