不灭的焱

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2021-02-13 21:23:17  修改时间:2024-03-26 23:52:05  分类:Java框架/系统  编辑

《Spring v4.3.x官方文档》Spring的schema模板大全


在Spring的配置中,总能看见如下的代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"

       xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
">

    <!-- 开启Bean注解注册 -->
    <context:annotation-config/>
    <context:component-scan base-package="com.jianbao.aspect"/>

    <!-- 开启AOP注解注册 -->
    <aop:aspectj-autoproxy/>

</beans>

注意:如果配置了<context:component-scan/>元素,就可以删除<context:annotation-config/>元素,参考:Spring的“自动装配Bean”与“自动检测Bean”

头文件说明:

  • 1.beans—— xml文件的根节点
  • 2.xmlns——是XML NameSpace的缩写,因为XML文件的标签名称都是自定义的,自己写的和其他人定义的标签很有可能会重复命名,而功能却不一样,所以需要加上一个namespace来区分这个xml文件和其他的xml文件,类似于java中的package。
  • 3.xmlns:xsi ——是指xml文件遵守xml规范,xsi全名:xml schema instance,是指具体用到的schema资源文件里定义的元素所准守的规范。即http://www.w3.org/2001/XMLSchema-instance这个文件里定义的元素遵守什么标准
  • 4.xsi:schemaLocation——是指本文档里的xml元素所遵守的规范,这些规范都是由官方制定的,可以进你写的网址里面看版本的变动。xsd的网址还可以帮助你判断使用的代码是否合法。

比如我们访问一下 http://www.springframework.org/schema/aop 可以看到如下内容:(这里我们指定遵循的规范为默认的规范spring-aop.xsd

示例代码配置说明:

在示例代码中配置内容如下

<!-- 开启Bean注解注册 -->
<context:annotation-config />
<context:component-scan base-package="com.jianbao.aspect.usage2" />

<!-- 开启AOP注解注册 -->
<aop:aspectj-autoproxy />

代码里面用到了 <aop>的标签就要先引入

xmlns:aop="http://www.springframework.org/schema/aop"

然后需要遵守的代码规范为

http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd

更多Schema模板参考: Spring的schema模板

 

 

参考:https://blog.csdn.net/u011479200/article/details/91863818