cvc-complex-type.2.4.c:匹配通配符严格,但找不到元素 'mongo:mongo' 的声明。[11]

6 java spring mongodb

我的 xml 仅适用于架构的版本 。当我尝试使用最新的(当前)时,我收到以下错误:1.8spring-mongo.xsd3.0

cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'mongo:mongo'. [11] 
cvc-complex-type.3.2.2: Attribute 'mongo-ref' is not allowed to appear in element 'mongo:db-factory'. [12] 
cvc-complex-type.3.2.2: Attribute 'username' is not allowed to appear in element 'mongo:db-factory'. [12] 
cvc-complex-type.3.2.2: Attribute 'password' is not allowed to appear in element 'mongo:db-factory'. [12] 
Run Code Online (Sandbox Code Playgroud)

这是我的 xml:

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

    <mongo:mongo id="mongo" replica-set="${mongo.replicaset}" write-concern="${mongo.write_concern:REPLICAS_SAFE}"/>
    <mongo:db-factory id="DbFactory" mongo-ref="mongo" dbname="${mongo.db}" username="${mongo.username}" password="${mongo.password}"/>
    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg ref="DbFactory"/>
        <property name="writeResultChecking" value="EXCEPTION"/>
    </bean>
</beans>
Run Code Online (Sandbox Code Playgroud)

根据其他答案,我尝试将credentials属性与连接字符串一起使用,但即使这样也会被拒绝?

xer*_*593 3

您应该将 xsd 版本修复为您的匹配spring-data-mongo版本。(为了防止意外,而不是“依赖最新的”)\n所以:

\n
xsi:schemaLocation="...\n   http://www.springframework.org/schema/data/mongo\n   http://www.springframework.org/schema/data/mongo/spring-mongo.YOUR.VERSION.xsd">\n
Run Code Online (Sandbox Code Playgroud)\n
\n

要将 xml 升级到 3.0(当前最新):\n请参阅此处

\n
\n
\n

表 4. 删除的 XML 命名空间元素和属性:

\n
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n
元素属性3.x 中的替换评论
<mongo:db-factory mongo-ref="\xe2\x80\xa6\xe2\x80\x8b" /><mongo:db-factory mongo-client-ref="\xe2\x80\xa6\xe2\x80\x8b" />引用 com.mongodb.client.MongoClient。
<mongo:mongo-client credentials="\xe2\x80\xa6\xe2\x80\x8b" /><mongo:mongo-client credential="\xe2\x80\xa6\xe2\x80\x8b" />单一认证数据而不是列表。
.........
\n
\n
    \n
  1. \n
    cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element \'mongo:mongo\'. [11] \n
    Run Code Online (Sandbox Code Playgroud)\n

    使用mongo:mongo-client(从1.8变为2.0)。

    \n
  2. \n
  3. \n
    cvc-complex-type.3.2.2: Attribute \'mongo-ref\' is not allowed to appear in element \'mongo:db-factory\'. [12] \n
    Run Code Online (Sandbox Code Playgroud)\n

    因此,将其替换为mongo-client-ref(引用 1 中的客户端)。

    \n
  4. \n
  5. \n
    cvc-complex-type.3.2.2: Attribute \'username\' is not allowed to appear in element \'mongo:db-factory\'. [12] \ncvc-complex-type.3.2.2: Attribute \'password\' is not allowed to appear in element \'mongo:db-factory\'. [12] \n
    Run Code Online (Sandbox Code Playgroud)\n

    您应该使用:credential的属性mongo-client

    \n
  6. \n
\n

请同时将表 3(writeConcern现在具有不同的值)视为表 5 ( mongo-client-ref, connection-string)。

\n

对于connection-string,最好参考(当前)Mongo DB 手册

\n
\n

这是应用此答案后编辑的 xml,不确定它是否正确,但我没有收到 xml 验证错误:

\n
<?xml version="1.0" encoding="UTF-8"?>\n<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n       xmlns="http://www.springframework.org/schema/beans"\n       xmlns:mongo="http://www.springframework.org/schema/data/mongo"\n       xsi:schemaLocation="\n       http://www.springframework.org/schema/beans\n       http://www.springframework.org/schema/beans/spring-beans.xsd\n       http://www.springframework.org/schema/data/mongo\n       http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">\n\n    <mongo:mongo-client id="mongo" replica-set="${mongo.replicaset}" />\n    <mongo:db-factory id="DbFactory" mongo-client-ref="mongo" dbname="${mongo.db}" connection-string="mongodb://${mongo.username}:${mongo.password}@${mongo.db}" write-concern="${mongo.write_concern:REPLICAS_SAFE}" />\n    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">\n        <constructor-arg ref="DbFactory"/>\n        <property name="writeResultChecking" value="EXCEPTION"/>\n    </bean>\n</beans>\n
Run Code Online (Sandbox Code Playgroud)\n