关于spring的xml文件中的xmlns,xsi,schemaLocation

关于spring xml文件中的xmlns,xsi:schemaLocation

首先我们看到的一个spring的配置文件大概如下面这个样子:

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" //这表示默认命名空间
    xmlns:hdp="http://www.springframework.org/schema/hadoop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/hadoop 
        http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">

    <bean>...</bean>
    ...
</beans>

首先我们解释一下这几个缩写到底是什么意思:

  1. xmlns:全名是xml namespace,也即是为当前的这个xml指定命名空间。

  2. xmlns:xsi:是指当前xml文件所要遵循的标签规范.

  3. 如上hdp, xsi, aop, cache, context, mvc...都是当前xml要使用到的一个标签,后面就是指定标签所要遵循的规范。

  4. xsi:schemaLocation:指定的命名空间对应的验证文件,用来定义xml schema的地址,也就是xml书写时需要遵循的语法,用于声明了目标命名空间的模式文档。。两部分组成,前面部分就是命名空间的名字,后面是xsd(xmlschema)的地址,也是就表示把定义这个命名空间的schema文件给引用进来,好让开发工具能够解析和验证你的xml文件是否符合语法规范。等于声明了目标命名空间的模式文档。

在idea中如何添加spring配置文件:

  1. 引入相关的spring框架依赖。

  2. 在需要添加xml配置文件的文件夹上点击右键,选择new -> XML Configuration File -> Spring Config。

  3. 在xml文件中输入相应的标签,标签对应的命名空间和规范会自动引入进来。

相关推荐
MacroZheng3 分钟前
还在用WebSocket实现即时通讯?试试MQTT吧,真香!
java·spring boot·后端
稚辉君.MCA_P8_Java18 分钟前
豆包 Java的23种设计模式
java·linux·jvm·设计模式·kubernetes
tanyongxi6618 分钟前
C++ 特殊类设计与单例模式解析
java·开发语言·数据结构·c++·算法·单例模式
遗憾皆是温柔21 分钟前
24. 什么是不可变对象,好处是什么
java·开发语言·面试·学习方法
midsummer_woo26 分钟前
基于springboot的IT技术交流和分享平台的设计与实现(源码+论文)
java·spring boot·后端
Peter(阿斯拉)37 分钟前
[Java性能优化]_[时间优化]_[字符串拼接的多种方法性能分析]
java·性能优化·stringbuilder·string·字符串拼接·stringbuffer·时间优化
qq_三哥啊1 小时前
【IDEA】设置Debug调试时调试器不进入特定类(Spring框架、Mybatis框架)
spring·intellij-idea·mybatis
水痕012 小时前
gin结合minio来做文件存储
java·eureka·gin
别惹CC2 小时前
Spring AI 进阶之路01:三步将 AI 整合进 Spring Boot
人工智能·spring boot·spring
寒士obj2 小时前
Spring事物
java·spring