首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Spring XML 配置報錯:The matching wildcard is strict... 根因與 30 秒修復

     編輯:Java 綜合教學

錯誤現象

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:
The matching wildcard is strict, but no declaration can be found for element 'aop:config'.

根因:schemaLocation 順序顛倒

Spring XML 配置文件頭部的 xsi:schemaLocation 屬性要求:**命名空間 URI 在前,XSD 文件路徑在後**,且成對出現。順序錯誤導致 Spring 無法正確解析命名空間對應的 Schema,進而報「找不到元素聲明」。

錯誤示例

<beans ...
    xsi:schemaLocation="
        https://www.springframework.org/schema/aop/spring-aop.xsd 
        https://www.springframework.org/schema/aop
        ... ">

此處將 XSD 路徑寫在了 URI 前面。

正確寫法

<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="https://www.springframework.org/schema/aop"
       xsi:schemaLocation="
           https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
           https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
           https://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd
           https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <aop:config>...</aop:config>
</beans>

通用模板(複製即用)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="https://www.springframework.org/schema/context"
       xmlns:aop="https://www.springframework.org/schema/aop"
       xmlns:tx="https://www.springframework.org/schema/tx"
       xsi:schemaLocation="
           https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
           https://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
           https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
           https://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- Bean 定義 -->
</beans>

避坑檢查清單

  1. 每對 URI XSD 換行書寫,便於排查。
  2. 引入的命名空間前綴(xmlns:aop)必須與使用的標籤前綴(aop:config)一致。
  3. XSD 版本號可省略(如 spring-aop.xsd),Spring 會根據當前 Jar 版本自動解析;寫死版本(如 spring-aop-4.3.xsd)升級時易報錯。
  4. IDE(IntelliJ/Eclipse)可自動校驗 XML,報紅處按 Alt+Enter 可快速修復命名空間導入。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved