您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
19-Spring相应API1(绝对路径加载Spring配置文件及解决无法读取方案文档 'httpwww.sspringframework.orgschemabeansspring-bean
发布时间:2024-12-20 18:45:39编辑:雪饮阅读()
-
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
像是这样,这是前番用的加载Spring Config配置文件的方法,那么这种是从resources目录下面加载的。
还有一种是从绝对路径加载的,例如
ApplicationContext app=new FileSystemXmlApplicationContext("D:\\os\\untitled\\src\\main\\resources\\applicationContext.xml");
不过这里首先得说另外一个问题,上篇中用的
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
这种方式实现的加载Spring Config配置文件的。
这次竟然加载失败,报错关键部位如:

org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 136; schema_reference.4: 无法读取方案文档 'http://www.sspringframework.org/schema/beans/spring-beans.xsd', 原因为 1) 无法找到文档; 2) 无法读取文档; 3) 文档的根元素不是 <xsd:schema>。
那么我的解决方案是下载http://www.sspringframework.org/schema/beans/spring-beans.xsd
到你的resources目录并将配置文件中http://www.sspringframework.org/schema/beans/spring-beans.xsd
替换为spring-beans.xsd
但这样又会导致一个新问题,就是当你用FileSystemXmlApplicationContext这种方式加载配置文件的时候仍然是要报错的,那么假如你还是以如:
ApplicationContext app=new FileSystemXmlApplicationContext("D:\\os\\untitled\\src\\main\\resources\\applicationContext.xml");
这种方式加载配置文件,那么此时Spring Config主配置文件中你可以这样
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans D:\os\untitled\src\main\resources\spring-beans.xsd" >
<import resource="applicationContext-User.xml" />
</beans>
也就是说xsd也指定绝对路径。。。
虽然这样不是很优雅,但是确实解决了问题。
不过一般来说,这里确实很少用绝对路径方式加载配置文件的,有这种情况下,这种解决方案应该也是可以选择的。
那么还有一个就是上面这个xsd你可能无法直接下载,因为有可能被cloudflare拦截了。
你需要访问http://www.springframework.org/schema/beans/这个链接,从这个链接里面找到你要下载的这个xsd文件下载下来。
关键字词:无法读取方案文档,spring-beans.xsd
相关文章
-
无相关信息