Tomcat - 上下文初始化失败

Posted

技术标签:

【中文标题】Tomcat - 上下文初始化失败【英文标题】:Tomcat - Context initialization failed 【发布时间】:2015-09-09 16:09:04 【问题描述】:

我正在使用 Spring STS 和 Spring Roo。我正在使用this 教程对数据库进行逆向工程。但是当我尝试在服务器(Pivotal tc 服务器,这是一个 tomcat 服务器)上运行时,我收到以下错误。

       2015-06-23 11:37:48,509 [localhost-startStop-1] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to read candidate component class: URL [jar:file:/C:/Users/subhransu.mishra/Desktop/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/tomcat-8.0.20.B.RELEASE/lib/tcServer3.jar!/com/springsource/tcserver/oracle/OracleDataSourceWrapper.class]; nested exception is java.io.FileNotFoundException: class path resource [oracle/jdbc/pool/OracleDataSourceFactory.class] cannot be opened because it does not exist
    Offending resource: file [C:\Users\subhransu.mishra\Desktop\sts-bundle\pivotal-tc-server-developer-3.1.0.RELEASE\base-instance\wtpwebapps\GOPHERGUI\WEB-INF\classes\META-INF\spring\applicationContext-jpa.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/C:/Users/subhransu.mishra/Desktop/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/tomcat-8.0.20.B.RELEASE/lib/tcServer3.jar!/com/springsource/tcserver/oracle/OracleDataSourceWrapper.class]; nested exception is java.io.FileNotFoundException: class path resource [oracle/jdbc/pool/OracleDataSourceFactory.class] cannot be opened because it does not exist
   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)

    Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/C:/Users/subhransu.mishra/Desktop/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/tomcat-8.0.20.B.RELEASE/lib/tcServer3.jar!/com/springsource/tcserver/oracle/OracleDataSourceWrapper.class]; nested exception is java.io.FileNotFoundException: class path resource [oracle/jdbc/pool/OracleDataSourceFactory.class] cannot be opened because it does not exist
        at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:290)
        at org.springframework.data.repository.config.RepositoryConfigurationSourceSupport.getCandidates(RepositoryConfigurationSourceSupport.java:68)
        at org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport.getRepositoryConfigurations(RepositoryConfigurationExtensionSupport.java:53)
        at org.springframework.data.repository.config.RepositoryBeanDefinitionParser.parse(RepositoryBeanDefinitionParser.java:69)
        ... 34 more
    Caused by: java.io.FileNotFoundException: class path resource [oracle/jdbc/pool/OracleDataSourceFactory.class] cannot be opened because it does not exist

POM文件:(更新:添加ojdbc,更新mysql驱动到5.1.35)

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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</groupId>
    <artifactId>GOPHERWEB</artifactId>
    <packaging>war</packaging>
    <version>0.1.0.BUILD-SNAPSHOT</version>
    <name>GOPHERWEB</name>
    <properties>
        <aspectj.version>1.8.1</aspectj.version>
        <java.version>7</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <roo.version>1.3.1.RC1</roo.version>
        <slf4j.version>1.7.5</slf4j.version>
        <spring.version>3.2.6.RELEASE</spring.version>
    </properties>
    <repositories>
    <repository>
      <id>codelds</id>
      <url>https://code.lds.org/nexus/content/groups/main-repo</url>
    </repository>
        <repository>
            <id>spring-maven-release</id>
            <name>Spring Maven Release Repository</name>
            <url>http://maven.springframework.org/release</url>
        </repository>
        <repository>
            <id>spring-maven-milestone</id>
            <name>Spring Maven Milestone Repository</name>
            <url>http://maven.springframework.org/milestone</url>
        </repository>
        <repository>
            <id>spring-roo-repository</id>
            <name>Spring Roo Repository</name>
            <url>http://spring-roo-repository.springsource.org/release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-maven-release</id>
            <name>Spring Maven Release Repository</name>
            <url>http://maven.springframework.org/release</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-maven-milestone</id>
            <name>Spring Maven Milestone Repository</name>
            <url>http://maven.springframework.org/milestone</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-roo-repository</id>
            <name>Spring Roo Repository</name>
            <url>http://spring-roo-repository.springsource.org/release</url>
        </pluginRepository>
    </pluginRepositories>
    <dependencies>
        <!-- General dependencies for standard applications -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>$slf4j.version</version>
        </dependency>

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>$slf4j.version</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>$slf4j.version</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>$aspectj.version</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>$aspectj.version</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>net.sf.flexjson</groupId>
            <artifactId>flexjson</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.1</version>
        </dependency>
        <!-- ROO dependencies -->
        <dependency>
            <groupId>org.springframework.roo</groupId>
            <artifactId>org.springframework.roo.annotations</artifactId>
            <version>$roo.version</version>
            <scope>provided</scope>
        </dependency>
        <!-- Spring dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>$spring.version</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>$spring.version</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.6.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.6.Final</version>
            <exclusions>
                <exclusion>
                    <groupId>cglib</groupId>
                    <artifactId>cglib</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>dom4j</groupId>
                    <artifactId>dom4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.3.2.Final</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.0.0.GA</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>commons-pool</groupId>
            <artifactId>commons-pool</artifactId>
            <version>1.5.6</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>xml-apis</groupId>
                    <artifactId>xml-apis</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.4.3.RELEASE</version>
        </dependency>
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>$spring.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.webflow</groupId>
            <artifactId>spring-js-resources</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-digester</groupId>
            <artifactId>commons-digester</artifactId>
            <version>2.1</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>jstl-impl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>el-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>1.6</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.5</version>
        </dependency>
    <dependency>
            <groupId>org.apache.tiles</groupId>
            <artifactId>tiles-jsp</artifactId>
            <version>2.2.2</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.2</version>
                <!-- <configuration> <webXml>target/web.xml</webXml> </configuration> -->
                <configuration>
                    <webResources>
                        <resource>
                            <excludes>
                                <exclude>dbre.xml</exclude>
                            </excludes>
                            <directory>src/main/resources</directory>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>$java.version</source>
                    <target>$java.version</target>
                    <encoding>$project.build.sourceEncoding</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.4</version>
                <dependencies>
                    <!-- NB: You must use Maven 2.0.9 or above or these are ignored (see MNG-2972) -->
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>$aspectj.version</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>$aspectj.version</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>compile</goal>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <outxml>true</outxml>
                    <aspectLibraries>
                        <aspectLibrary>
                            <groupId>org.springframework</groupId>
                            <artifactId>spring-aspects</artifactId>
                        </aspectLibrary>
                    </aspectLibraries>
                    <source>$java.version</source>
                    <target>$java.version</target>
                    <!--
                      Aspects in src/main/java and src/main/aspects are added as default in the 
                      compile goal.
                      Aspects in src/test/java and src/test/aspects are added as default in the 
                      test-compile goal.
                      Aspects in src/main/java are added in the test-compile goal 
                      if weaveWithAspectsInMainSourceFolder is set to true
                    -->
                    <weaveWithAspectsInMainSourceFolder>false</weaveWithAspectsInMainSourceFolder>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <encoding>$project.build.sourceEncoding</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12</version>
                <configuration>
                    <printSummary>false</printSummary>
                    <redirectTestOutputToFile>true</redirectTestOutputToFile>
                    <excludes>
                        <exclude>**/*_Roo_*</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.7</version>
            </plugin>
            <!-- IDE -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.7</version>
                <!-- Note 2.8 does not work with AspectJ aspect path -->
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                    <wtpversion>2.0</wtpversion>
                    <additionalBuildcommands>
                        <buildCommand>
                            <name>org.eclipse.ajdt.core.ajbuilder</name>
                            <arguments>
                                <aspectPath>org.springframework.aspects</aspectPath>
                            </arguments>
                        </buildCommand>
                        <buildCommand>
                            <name>org.springframework.ide.eclipse.core.springbuilder</name>
                        </buildCommand>
                    </additionalBuildcommands>
                    <additionalProjectnatures>
                        <projectnature>org.eclipse.ajdt.ui.ajnature</projectnature>
                        <projectnature>com.springsource.sts.roo.core.nature</projectnature>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-idea-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <dependenciesAsLibraries>true</dependenciesAsLibraries>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.4.v20120524</version>
                <configuration>
                    <webAppConfig>
                        <contextPath>/$project.name</contextPath>
                    </webAppConfig>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Persistence.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
            <!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database -->
            <property name="hibernate.hbm2ddl.auto" value="none"/>
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultNamingStrategy"/>
            <property name="hibernate.connection.charSet" value="UTF-8"/>
            <!-- Uncomment the following two properties for JBoss only -->
            <!-- property name="hibernate.validator.apply_to_ddl" value="false" /-->
            <!-- property name="hibernate.validator.autoregister_listeners" value="false" /-->
        </properties>
    </persistence-unit>
</persistence>

更新

添加 ojdbc maven 依赖项后出现新错误。现在它显示mysql错误。但是有mysql依赖。

2015-06-30 13:15:11,216 [localhost-startStop-1] ERROR org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to read candidate component class: URL [jar:file:/C:/Users/subhransu.mishra/Downloads/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/base-instance/wtpwebapps/GOPHERWEB/WEB-INF/lib/mysql-connector-java-5.1.35.jar!/com/mysql/fabric/hibernate/FabricMultiTenantConnectionProvider.class]; nested exception is java.io.FileNotFoundException: class path resource [org/hibernate/service/jdbc/connections/spi/MultiTenantConnectionProvider.class] cannot be opened because it does not exist
Offending resource: file [C:\Users\subhransu.mishra\Downloads\sts-bundle\pivotal-tc-server-developer-3.1.0.RELEASE\base-instance\wtpwebapps\GOPHERWEB\WEB-INF\classes\META-INF\spring\applicationContext-jpa.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/C:/Users/subhransu.mishra/Downloads/sts-bundle/pivotal-tc-server-developer-3.1.0.RELEASE/base-instance/wtpwebapps/GOPHERWEB/WEB-INF/lib/mysql-connector-java-5.1.35.jar!/com/mysql/fabric/hibernate/FabricMultiTenantConnectionProvider.class]; nested exception is java.io.FileNotFoundException: class path resource [org/hibernate/service/jdbc/connections/spi/MultiTenantConnectionProvider.class] cannot be opened because it does not exist

【问题讨论】:

该错误抱怨缺少 oracle 类,该类是 Oracle JDBC 驱动程序的一部分。我在你的 pom 中看不到对 oracle JDBC 驱动程序的任何依赖。 但是我指定了mysql,persistence setup --provider HIBERNATE --database MYSQL 。并且还提供了mysql驱动的位置。我还应该添加oracle依赖吗? 显然扫描了一个类tcServer,依赖于这个类。 谢谢,很有帮助。但是现在又出现了新的错误,和mysql有关?请检查更新部分。 这似乎是版本不匹配:错误消息表明预期为 5.1.35,但在 pom 中指定了 5.1.18 【参考方案1】:

您收到 FileNotFoundException 因为它不在您的类路径中。请检查类路径中的 jar(特别是 jar 包中的此类)并尝试切换到最新版本的 mysql-connector.jar。这可能是 mysql-connector 和 hibernate jar 之间的兼容性问题。

【讨论】:

您好,已更新 mysql 连接器,但未在此处更新 pom 文件。对此感到抱歉。【参考方案2】:

这个问题已经在“5.1.30”版本的mysql jar中解决了。查看链接: http://forums.mysql.com/read.php?39,612977,612977

需要在 pom.xml 中升级 mysql-coonector jar 版本。

请注意,这应该是对“Taoufik Mohdit”的支持评论,但我没有足够的声誉。

【讨论】:

【参考方案3】:

mysql-connector jar 中的类com.mysql.fabric.hibernate.FabricMultiTenantConnectionProvider 使用org.hibernate.service.jdbc.connections.spi.MultiTenantConnectionProvider 接口来自 hibernate-core jar,它不在版本 4.3.6.Final 中的正确包下(它在那里但在不同的包下:org.hibernate.engine .jdbc.connections.spi)。 hibernate-core 版本 4.3.9.Final 在正确的包 (org.hibernate.service.jdbc.connections.spi) 中有这个类。

总而言之,如果您将 hibernate-core 升级到 4.3.9.Final,这应该有望解决问题。

【讨论】:

以上是关于Tomcat - 上下文初始化失败的主要内容,如果未能解决你的问题,请参考以下文章

由于已经存在根应用程序上下文,无法初始化上下文,因此在 tomcat 中部署战争失败

tomcat 配置始终不行 初始化失败

tomcat初始化失败的问题

初始化默认 SSL 上下文失败

ContextLoader:上下文初始化失败

上下文初始化失败 Spring