找不到 XML 模式命名空间 Application-Context 的 Spring NamespaceHandler
Posted
技术标签:
【中文标题】找不到 XML 模式命名空间 Application-Context 的 Spring NamespaceHandler【英文标题】:Unable to locate Spring NamespaceHandler for XML schema namespace Application-Context 【发布时间】:2015-03-01 22:33:36 【问题描述】:我正在使用一些 Spring 库开发一个 GWT Web 应用程序。我使用 Maven 来包含我的依赖项。
这是我的 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>be.jon</groupId>
<artifactId>bibliosgwt</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>GWT Maven Archetype</name>
<properties>
<!-- Convenience property to set the GWT version -->
<gwtVersion>2.7.0</gwtVersion>
<!-- GWT needs at least java 1.6 -->
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt</artifactId>
<version>$gwtVersion</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-servlet</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-user</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-dev</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>com.jolbox</groupId>
<artifactId>bonecp-spring</artifactId>
<version>0.8.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
// DEPENDENCY PROBLEM
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.3.RELEASE</version>
</dependency>
//
</dependencies>
<build>
<!-- Output classes directly into the webapp, so that IDEs and "mvn process-classes"
update them in DevMode -->
<outputDirectory>$project.build.directory/$project.build.finalName/WEBINF/classes</outputDirectory>
<plugins>
<!-- GWT Maven Plugin -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>2.7.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test</goal>
<!-- <goal>generateAsync</goal> -->
</goals>
</execution>
</executions>
<!-- Plugin configuration. There are many available options, see gwt-maven-plugin
documentation at codehaus.org -->
<configuration>
<runTarget>bibliosgwt.html</runTarget>
<modules>
<module>be.jon.bibliosgwt.bibliosgwt</module>
</modules>
</configuration>
</plugin>
</plugins>
</build>
</project>
这是我的 application-context.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring- beans.xsd">
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- Tell hibernate to use our given datasource -->
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.mysqlDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<value>be.jon.bibliosgwt.shared.Entities</value>
</property>
</bean>
<jpa:repositories base-package="be.jon.biliosgwt.server.repositories"/>
<!-- Spring bean configuration. Tell Spring to bounce off BoneCP -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource" ref="mainDataSource"/>
</bean>
<!-- BoneCP configuration -->
<bean id="mainDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql:/localhost:3306/bibliosgwt" />
<property name="username" value="root" />
<property name="password" value="" />
<property name="idleConnectionTestPeriod" value="60" />
<property name="idleMaxAge" value="240" />
<property name="maxConnectionsPerPartition" value="60" />
<property name="minConnectionsPerPartition" value="20" />
<property name="partitionCount" value="3" />
<property name="acquireIncrement" value="10" />
<property name="statementsCacheSize" value="50" />
<property name="releaseHelperThreads" value="3" />
</bean>
<bean id="greetService" class="be.jon.bibliosgwt.server.GreetingServiceImpl"></bean>
一切都很好,但是当我包含 Spring-Web 依赖项时。我在启动项目时遇到问题。 出现以下异常:
[WARN] Failed startup of context c.g.g.d.s.j.WebAppContextWithReload/,file:/D:/Users/Yeap/workspace-sts
3.6.2.RELEASE/testproject/target/testproject-0.0.1-SNAPSHOT/,D:\Users\Yeap\workspace-sts 3.6.2.RELEASE\testproject\target\testproject-0.0.1-SNAPSHOT org.springframework.beans.factory.parsing.BeanDefinitionParsingException: 配置问题:找不到 Spring NamespaceHandler for XML 模式命名空间 [http://www.springframework.org/schema/data/jpa] 违规资源:ServletContext 资源 [/WEB-INF/application-context.xml]
我不明白问题出在哪里...我想到了 SchemaLocation 命名空间问题,但我尝试了所有版本的可能性,没有一个是好的。
【问题讨论】:
听起来很奇怪。该错误表示jpa依赖有问题但是在添加web依赖时弹出。 在 DevMode 的 webapp 类加载器中可能存在错误(假设您在这里使用mvn gwt:run
);建议您对客户端和服务器端代码使用不同的 Maven 模块,并使用 mvn jetty:run
或 mvn tomcat7:run
启动您的 webapp(以及 noserver
模式下的 DevMode)
【参考方案1】:
我可以看到一些相互矛盾的事情
-
混合弹簧版本 4.1.4 和 4.1.3
Spring Data JPA依赖4.0.7版本的spring
您的 beans 命名空间包含空格/制表符?
使用无版本架构声明。
要管理您的依赖关系,并防止混合版本,建议使用 Spring Framework BOM,就像您使用 GWT 中的 BOM 一样。这将为您省去一些麻烦。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt</artifactId>
<version>$gwtVersion</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>4.1.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.7.1.RELEASE</version>
</dependency>
// Other dependencies
</dependencies>
接下来你的命名空间声明有点错误,建议使用版本较少的 XSD。
xsi:schemaLocation="
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
【讨论】:
以上是关于找不到 XML 模式命名空间 Application-Context 的 Spring NamespaceHandler的主要内容,如果未能解决你的问题,请参考以下文章
找不到 XML 模式命名空间 Application-Context 的 Spring NamespaceHandler
找不到 XML 模式命名空间的 Spring NamespaceHandler [http://www.springframework.org/schema/webflow]
Spring 3.0 -- 无法为 XML 模式命名空间上下文找到 Spring NamespaceHandler
我在Tomcat部署项目,报500错误,说命名空间找不到,我运行正常的项目呀