Spring XML 配置和命名空间

发布于 2024-12-10 12:49:58 字数 1222 浏览 0 评论 0原文

我正在尝试配置 Spring 配置(xml)文件,并尝试为我本地项目中的有效架构文件添加命名空间。原因是我没有“真实”URL(Web 服务器等),因此我尝试仅使用我将在 WEB-INF/ 下提供的本地架构:

MyProject/
    src/
        java/
        config/
            MySchema.xsd
            spring-config.xml
    build.xml

在我的 spring-config.xml 中:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:ms="http://myproject.org/MySchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

项目正在构建并正在构建部署正常,但是当我去运行它时,我得到:

org.xml.sax.SAXException: Failed to locate XSD resource '/META-INF/MySchema.xsd' on 
classpath. Namespace: 'http://myproject.org/MySchema.xsd'.

最终我希望将 spring-config.xmlMySchema.xsd 存储在 WEB-INF 下/,所以我不确定为什么Spring要查看META-INF/...有什么想法吗?

URL http://myproject.org/MySchema.xsd 不是真正的 URL,只是为架构提供了自己的命名空间。提前致谢!

I'm trying to configure a Spring config (xml) file and am trying to add a namespace for a valid schema file that I have living locally inside my project. The reason is that I don't have a "real" URL (web server, etc.), so I'm trying to just use a local schema that I will provide under WEB-INF/:

MyProject/
    src/
        java/
        config/
            MySchema.xsd
            spring-config.xml
    build.xml

Inside my spring-config.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:ms="http://myproject.org/MySchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
   http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

Project is building & deploying fine, but when I go to run it I get:

org.xml.sax.SAXException: Failed to locate XSD resource '/META-INF/MySchema.xsd' on 
classpath. Namespace: 'http://myproject.org/MySchema.xsd'.

Ultimately I'd like both spring-config.xml and MySchema.xsd stored under WEB-INF/, so I'm not sure why Spring is looking inside META-INF/... any ideas?

The URL http://myproject.org/MySchema.xsd is not a real URL, just giving the schema its own namespace. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余生再见 2024-12-17 12:49:59

您需要定义 spring.schemas 和 spring.handlers 吗?
在此处查看更多信息: http://www.theserverside.com /news/1364131/Authoring-Custom-Namespaces-in-Spring-20

Do you need define spring.schemas and spring.handlers?
See more here: http://www.theserverside.com/news/1364131/Authoring-Custom-Namespaces-in-Spring-20

很糊涂小朋友 2024-12-17 12:49:59

您还需要为您的 http://www.w3.org/TR/xmlschema-0/#schemaLocation" rel="nofollow">架构位置 定义/myproject.org/MySchema.xsd 命名空间。从 W3C 来看,架构位置:

由一对或多对 URI 引用组成,以空格分隔。每对的第一个成员是命名空间名称,对的第二个成员是描述在哪里找到该命名空间的适当模式文档的提示。这些提示的存在并不要求处理者获取或使用所引用的模式文档,并且处理者可以自由地使用通过任何合适的方式获得的其他模式,或者根本不使用模式。

我将使用不同的命名空间名称,以免与命名空间架构文档提示混淆。也许可以使用架构位置,例如 http://myproject.org/my-schema config/MySchema.xsd 。

You also need to define the schema location for your http://myproject.org/MySchema.xsd namespace. From W3C, the schema location:

consists of one or more pairs of URI references, separated by white space. The first member of each pair is a namespace name, and the second member of the pair is a hint describing where to find an appropriate schema document for that namespace. The presence of these hints does not require the processor to obtain or use the cited schema documents, and the processor is free to use other schemas obtained by any suitable means, or to use no schema at all.

I would use a different namespace name so as not to be confused with the namespace schema document hint. Maybe use a schema location such as http://myproject.org/my-schema config/MySchema.xsd instead.

冷心人i 2024-12-17 12:49:58

http://myproject.org/MySchema 没有“定义”(我不知道正确的术语) xsi:schemaLocation 属性中的 .xsd。尝试这样的事情:

xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://myproject.org/MySchema.xsd location/of/MySchema.xsd">

There is no "definition" (I don't know the correct term) of the http://myproject.org/MySchema.xsd in the xsi:schemaLocation attribute. Try something like this:

xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://myproject.org/MySchema.xsd location/of/MySchema.xsd">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文