gpt4 book ai didi

java - XML 中的 spring 配置文件错误

转载 作者:行者123 更新时间:2023-11-30 02:52:19 25 4
gpt4 key购买 nike

我在 mvc-dispatcher-servlet.xml 中使用 spring 配置文件。但出现错误:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'bean'. One of '{"http://www.springframework.org/schema/beans":beans}' is expected.

我没有在配置文件中添加其他bean,因为我希望在两个配置文件中都使用这些bean。需要帮助解决错误。下面是我的 XML,错误出现在行 - bean id="transactionManager" 类=“org.springframework.orm.hibernate5.HibernateTransactionManager”> :

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

<!-- Specifying base package of the Components like Controller, Service,
DAO -->
<context:component-scan base-package="com.mycompany.saas.*" />

<!-- Getting Database properties -->
<context:property-placeholder location="classpath:db.properties" />
<!-- Getting Configuration properties -->
<!-- <context:property-placeholder location="classpath:config.properties"
/> -->

<mvc:annotation-driven />
<beans profile="default">
<!-- DataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${database.driverClass}" />
<property name="jdbcUrl" value="${database.url}" />
<property name="user" value="${database.username}" />
<property name="password" value="${database.password}" />
<property name="acquireIncrement" value="${connection.acquireIncrement}" />
<property name="minPoolSize" value="${connection.minPoolSize}" />
<property name="maxPoolSize" value="${connection.maxPoolSize}" />
<property name="maxIdleTime" value="${connection.maxIdleTime}" />
</bean>

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<!-- <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> -->
<prop key="hibernate.default_schema">${hibernate.default_schema}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.connection.shutdown">true</prop>

</props>
</property>
<property name="packagesToScan" value="com.mycompany.saas.model"></property>
</bean>
</beans>

<beans profile="test">
<!-- DataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${database.driverClass}" />
<property name="jdbcUrl" value="${database.url}" />
<property name="user" value="${database.username}" />
<!-- <property name="password" value="${database.password}" /> -->
<property name="acquireIncrement" value="${connection.acquireIncrement}" />
<property name="minPoolSize" value="${connection.minPoolSize}" />
<property name="maxPoolSize" value="${connection.maxPoolSize}" />
<property name="maxIdleTime" value="${connection.maxIdleTime}" />
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.connection.shutdown">true</prop>

</props>
</property>
<property name="packagesToScan" value="com.mycompany.saas.model"></property>
</bean>
</beans>

<!-- Transaction -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="applicationContextProvder" class="com.mycompany.saas.util.ApplicationContextProvider" />
<bean id="mcSaasUserDAO" class="com.mycompany.saas.dao.mcSaasUserDAOImpl"></bean>
<bean id="mcUserService" class="com.mycompany.saas.service.mcUserServiceImpl"></bean>
<bean id="emailContentGenerator" class="com.mycompany.saas.notifier.mcEmailContentGenerator">
<constructor-arg value="classpath:/mail.html" />
</bean>



</beans>

最佳答案

读取 XSD 文件后发现第一级 <bean>...</bean>元素(即 transactionManager 和后续 beans)必须位于嵌套 <beans profile="xxx">...</beans> 之前元素。

这是 XSD 的片段

<xsd:element name="beans"><xsd:annotation><xsd:documentation>
...
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="description" minOccurs="0"/>
<xsd:choice minOccurs="0" maxOccurs="unbounded">
<xsd:element ref="import"/>
<xsd:element ref="alias"/>
<xsd:element ref="bean"/>
<xsd:any namespace="##other" processContents="strict" minOccurs="0" maxOccurs="unbounded"/>
</xsd:choice>
<xsd:element ref="beans" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
...

这表示 beans元素必须是包含 description 的序列(在别处定义),后跟零个或多个可以是 import 中任意一个的元素, aliasbean (也在其他地方以任何顺序定义),后跟零个或多个 beans元素(递归定义)。

关于java - XML 中的 spring 配置文件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38238740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com