首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

Spring 整合 Hibernate 和 Struts2 配置文件報錯:property 元素位置錯誤

     編輯:程式設計疑難解答

錯誤信息

org.xml.sax.SAXParseException; lineNumber: 19; columnNumber: 44; 
cvc-complex-type.2.4.a: Invalid content was found starting with element 'property'. 
One of '{"https://www.springframework.org/schema/beans":import,
 "https://www.springframework.org/schema/beans":alias,
 "https://www.springframework.org/schema/beans":bean,
 WC[##other:"https://www.springframework.org/schema/beans"]}' is expected.

原因分析

<property> 元素必須包含在 <bean> 元素內部。錯誤提示表明解析器在預期 bean、import 或 alias 等頂層元素的位置發現了 property,說明 property 寫在了 beans 根節點下或 bean 終了標籤之後。

錯誤配置示例

<beans ...>
    <bean id="dataSource" class="..." />  <!-- 錯誤:自閉合標籤,無法包含 property -->
    
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />  <!-- 錯誤:游離在 bean 之外 -->
    <property name="url" value="jdbc:mysql://..." />
</beans>

正確配置結構

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

    <!-- 數據源配置 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/mydb" />
        <property name="username" value="root" />
        <property name="password" value="123456" />
    </bean>  <!-- property 必須在 bean 開始與結束標籤之間 -->

    <!-- SessionFactory 配置 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <!-- 事務管理器 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

常見錯誤模式對比

錯誤模式表現修正
bean 自閉合<bean id="x" class="..." /> 後跟 property改為雙標籤 <bean>...</bean> 包裹 property
property 寫在 beans 根下與 import/bean 平級移入對應 bean 內部
巢狀 bean 缺少父標籤內部 bean 的 property 游離確保每個 property 有最近的祖先 bean

Spring 配置文件結構規則

  • 根元素:<beans>
  • 頂層子元素:<bean>、<import>、<alias>、<context:*> 等
  • <bean> 內部可包含:<property>、<constructor-arg>、<qualifier>、<lookup-method>、<replaced-method>、內嵌 <bean> 等
  • <property> 不能 作為 <beans> 的直接子元素

調試技巧

  1. 使用 IDE(IDEA/Eclipse)的 XML 驗證功能,實時提示結構錯誤。
  2. 檢查行號(錯誤信息中的 lineNumber: 19),定位具體位置。
  3. 確認所有 <bean> 都有匹配的開始/結束標籤,無自閉合遺漏。
  4. 引入正確的 XSD 命名空間,啟用代碼補全與驗證。

總結

錯誤本質是 property 元素未包裹在對應的 bean 內部。修正方法:將所有 property 移入其所屬 bean 的開始與結束標籤之間,確保 XML 結構符合 Spring Schema 定義。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved