Spring MVC 上的文件 .xml 中的错误

Posted

技术标签:

【中文标题】Spring MVC 上的文件 .xml 中的错误【英文标题】:Error in file .xml on Spring MVC 【发布时间】:2015-06-26 15:28:58 【问题描述】:

我正在将 Spring MVC 用于 Web 应用程序。我最近阅读了 JPARepository,我喜欢它创建 DAO 的简单性。但是 servlet-context.xml 文件向我抛出了以下错误。这很奇怪,因为我没有更改文档配置中的任何内容:

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 36 in XML document from ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd; lineNumber: 36; columnNumber: 63; src-resolve: No se puede resolver el nombre 'repository:auditing-attributes' para un componente 'attribute group'.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)

我也放了servlet-context文件:

<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/data/jpa 
http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.2.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<context:annotation-config />
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the $webappRoot/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value="" />
</beans:bean>

<context:component-scan base-package="com.prueba.web" />


<jpa:repositories base-package="com.prueba.web.repository" />


<!-- Data Source -->
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <beans:property name="driverClassName" value="org.postgresql.Driver" />
    <beans:property name="url" value="jdbc:postgresql://localhost:5432/EjercicioArticulo" />
    <beans:property name="username" value="postgres" />
    <beans:property name="password" value="postgres" />
    <beans:property name="maxActive" value="100" /> <!-- indica el número máximo de conexiones que pueden usarse. -->
    <beans:property name="maxIdle" value="30"/> <!-- indica el límite de conexiones que debe haber disponibles en el pool.  -->
    <beans:property name="maxWait" value="10000"/> <!-- indica el tiempo en ms que esperará Tomcat a que haya una conexión libre en caso de que no hubiera ninguna libre en ese instante. -->
</beans:bean>

<!-- Entity Manager Factory -->
<beans:bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <beans:property name="dataSource" ref="dataSource" />
    <beans:property name="persistenceUnitName" value="puProyectoPrueba" />
    <beans:property name="jpaVendorAdapter">
        <beans:bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <beans:property name="showSql" value="true" />
        </beans:bean>
    </beans:property>
    <beans:property name="jpaPropertyMap">
        <beans:map>
            <beans:entry key="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <beans:entry key="hibernate.hbm2ddl.auto" value="update" />
            <beans:entry key="hibernate.format_sql" value="true" />
        </beans:map>
    </beans:property>
</beans:bean>

<!-- Support Annotation -->
<beans:bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />  
<!-- 
<beans:bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
-->

<!-- Transaction Manager -->
<tx:annotation-driven />
<beans:bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <beans:property name="entityManagerFactory" ref="entityManagerFactory"/>
</beans:bean>

【问题讨论】:

所有有这些类的jar都放了吗? 【参考方案1】:

试试这样的,

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:beans="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd 
            http://www.springframework.org/schema/data/jpa 
            http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context-3.2.xsd 
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<context:annotation-config />
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the $webappRoot/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value="" />
</bean>

<context:component-scan base-package="com.prueba.web" />


<jpa:repositories base-package="com.prueba.web.repository" />


<!-- Data Source -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://localhost:5432/EjercicioArticulo" />
    <property name="username" value="postgres" />
    <property name="password" value="postgres" />
    <property name="maxActive" value="100" /> <!-- indica el número máximo de conexiones que pueden usarse. -->
    <property name="maxIdle" value="30"/> <!-- indica el límite de conexiones que debe haber disponibles en el pool.  -->
    <property name="maxWait" value="10000"/> <!-- indica el tiempo en ms que esperará Tomcat a que haya una conexión libre en caso de que no hubiera ninguna libre en ese instante. -->
</bean>

<!-- Entity Manager Factory -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="persistenceUnitName" value="puProyectoPrueba" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
        </bean>
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <entry key="hibernate.hbm2ddl.auto" value="update" />
            <entry key="hibernate.format_sql" value="true" />
        </map>
    </property>
</bean>

<!-- Support Annotation -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />  
<!-- 
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
-->

<!-- Transaction Manager -->
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

【讨论】:

好的,谢谢,但现在 Eclipse 在标签 xsi:schemaLocation 之后显示错误。错误是这样的:在 esta 行找到多个注释:- Cvc-elt.1:找不到元素“bean”的声明。 - 找不到元素 [beans] 的 BeanDefinitionParser - 配置问题:找不到元素 [beans] 的 BeanDefinitionParser 违规资源:文件 [D:/Backup/Eclipse 工作区/工作区- ***.com/questions/13814321/…

以上是关于Spring MVC 上的文件 .xml 中的错误的主要内容,如果未能解决你的问题,请参考以下文章

spring mvc配置文件出现红叉

新建项目web.xml,spring-mvc.xml简单配置问题

Spring MVC 404错误[重复]

手写Spring MVC框架 实现简易版mvc框架

xml Spring MVC 4 XML配置文件

错误:没有找到带有 URI spring mvc 且没有 xml 的 HTTP 请求的映射