Spring + Maven:匹配通配符是严格的,但没有声明可以找到元素'jdbc:embedded-database'

问题描述:

我目前正在尝试构建一个使用Spring,Hibernate和Maven(运行在tomcat上) 。它工作得很好,除了我不能让我的嵌入式数据库工作。我希望你能帮助我。Spring + Maven:匹配通配符是严格的,但没有声明可以找到元素'jdbc:embedded-database'

我总是面临着这样的错误,当我部署web应用到Tomcat:

匹配通配符是严格的,但没有声明可以发现元素的JDBC:嵌入式数据库'

在我的调查中,我了解到这条消息指向缺少的库。因此我添加了我的pom.xml,其中我添加了artifact spring-jdbc。

你能帮我找到错误吗?非常感谢!


这是我的弹簧配置文件,该web应用程序的初始化过程中导致该错误:

<?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:tx="http://www.springframework.org/schema/tx" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
    http://www.springframework.org/schema/jdbc 
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd"> 
    <bean id="sessionFactory" class= 
    "org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="embeddedDatasource" /> 
     <property name="packagesToScan" value="org.rest" /> 
     <property name="hibernateProperties"> 
      <value> 
       hibernate.dialect=org.hibernate.dialect.MySQL5Dialect 
       hibernate.hbm2ddl.auto=update 
       hibernate.show_sql=false 
      </value> 
     </property> 
    </bean> 

    <jdbc:embedded-database id="embeddedDatasource" type="HSQL"/> 

    <bean id="txManager" class= 
     "org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 
    <tx:annotation-driven transaction-manager="txManager" /> 
</beans> 

这是我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    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>com.marcus</groupId> 
    <artifactId>maven-webapp-archetype</artifactId> 
    <packaging>war</packaging> 
    <version>0.0.1-SNAPSHOT</version> 
    <name>maven-webapp-archetype Maven Webapp</name> 
    <url>http://maven.apache.org</url> 
    <dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>servlet-api</artifactId> 
     <version>3.0-alpha-1</version> 
     <scope>provided</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-orm</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    <dependency> 
     <groupId>javax.servlet.jsp.jstl</groupId> 
     <artifactId>javax.servlet.jsp.jstl-api</artifactId> 
     <version>1.2.1</version> 
    </dependency> 
    <dependency> 
     <groupId>jstl</groupId> 
     <artifactId>jstl</artifactId> 
     <version>1.2</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>3.6.10.Final</version> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jdbc</artifactId> 
     <version>3.1.1.RELEASE</version> 
    </dependency> 
    </dependencies> 
    <build> 
    <finalName>maven-webapp-archetype</finalName> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.tomcat.maven</groupId> 
      <artifactId>tomcat6-maven-plugin</artifactId> 
      <version>2.0-beta-1</version> 
      <configuration> 
      <url>http://localhost:8080/manager/html</url> 
      <server>tomcat7</server> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.tomcat.maven</groupId> 
      <artifactId>tomcat7-maven-plugin</artifactId> 
      <version>2.0-beta-1</version> 
      <configuration> 
       <url>http://localhost:8080/manager/html</url> 
       <server>tomcat7</server> 
      </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

这条线在你的Spring上下文文件:

xmlns:jdbc="http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd" 

应改为:

xmlns:jdbc="http://www.springframework.org/schema/jdbc" 

不知道你正在使用的IDE但也有一些(的IntelliJ举例来说)这将被标记为错误,节省了大量的头痛!

+0

谢谢!这解决了它。 我正在使用eclipse。想想我应该看看为什么eclipse不会将其显示为错误。 这是我的第一个问题在stackoverflow,所以我不能投票你的答案? – mavilein 2012-04-10 17:53:16

+0

没问题:)欢迎来到SO。你需要让你的代表** 15 **投票答案,确保你很快就会到场。 – darrengorman 2012-04-10 17:57:26

对我来说是增加的xmlns:JDBC和XSI:的schemaLocation

<beans .... 

    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 

    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    ..... 
    http://www.springframework.org/schema/jdbc 
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd" 
    default-lazy-init="true"> 

还添加

+0

我同意,需要在xsi:schemaLocation中引用xsd,将它添加到xmlns是不够的。 – 2015-03-25 03:40:14

+0

我将http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd添加到我的schemaLocation中。如果你没有使用3.0版本,这很有用。 – 2017-02-24 14:13:21