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 |
<beans><bean>、<import>、<alias>、<context:*> 等<bean> 內部可包含:<property>、<constructor-arg>、<qualifier>、<lookup-method>、<replaced-method>、內嵌 <bean> 等<property> 不能 作為 <beans> 的直接子元素lineNumber: 19),定位具體位置。<bean> 都有匹配的開始/結束標籤,無自閉合遺漏。錯誤本質是 property 元素未包裹在對應的 bean 內部。修正方法:將所有 property 移入其所屬 bean 的開始與結束標籤之間,確保 XML 結構符合 Spring Schema 定義。