Spring framework 从2.0版本开始引入了xml schema,我们可以使用spring实现的各种schema来定义和扩展bean。
如以下例子:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- bean declarations here -->
</beans>
这个xml引入了http://www.springframework.org/schema/beans为缺省的namespace,的确我们经常用的就是定义bean,xml又定义了这个namespace的schema的location,这是一个url(http://www.springframework.org/schema/beans/spring-beans-2.5.xsd),如果用浏览器访问这个URL,的确可以得到这个schema。
但是,spring容器在启动时是从这个URL得到schema来验证这个xml文件的吗?
不是,你把网络断掉,spring一样运行的很好。
原来,在spring.jar中有个目录为META-INF,下面有个文件为spring.schemas,打开这个文件一看内容就知道,它把URL映射到了classpath。
例如第一行:
http/://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd
这个文件告诉spring容器到某个classpath下去寻找schema文件,而这些文件的确就在spring.jar的相应目录下。
如以下例子:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- bean declarations here -->
</beans>
这个xml引入了http://www.springframework.org/schema/beans为缺省的namespace,的确我们经常用的就是定义bean,xml又定义了这个namespace的schema的location,这是一个url(http://www.springframework.org/schema/beans/spring-beans-2.5.xsd),如果用浏览器访问这个URL,的确可以得到这个schema。
但是,spring容器在启动时是从这个URL得到schema来验证这个xml文件的吗?
不是,你把网络断掉,spring一样运行的很好。
原来,在spring.jar中有个目录为META-INF,下面有个文件为spring.schemas,打开这个文件一看内容就知道,它把URL映射到了classpath。
例如第一行:
http/://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd
这个文件告诉spring容器到某个classpath下去寻找schema文件,而这些文件的确就在spring.jar的相应目录下。