12

我目前正在尝试构建一个使用 Spring、Hibernate 和 Maven(在 tomcat 上运行)的小 webapp。它工作得很好,除了我无法让我的嵌入式数据库工作。我希望你能帮助我。

当我将 webapp 部署到 Tomcat 时,我总是面临这个错误:

匹配的通配符是严格的,但找不到元素“jdbc:embedded-database”的声明

在我的调查中,我了解到此消息指向丢失的库。因此我添加了我的 pom.xml,我在其中添加了工件 spring-jdbc。

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


这是我的 spring-configuration-file,它在 webapp 初始化期间导致错误:

<?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>
4

2 回答 2

15

Spring 上下文文件中的这一行:

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

应改为:

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

不确定您使用的是什么 IDE,但是对于某些(例如 IntelliJ),这将被标记为错误并省去很多麻烦!

于 2012-04-10T17:42:28.903 回答
12

对我来说是添加 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">

还添加

于 2013-01-30T09:09:40.327 回答