ContextLoader - 上下文初始化失败

时间:2014-11-13 07:07:05

标签: java spring hibernate spring-mvc broadleaf-commerce

无法启动已部署的war文件。 我在终端上收到以下错误

[ERROR] 17:23:17 ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in resource loaded from byte array: Cannot resolve reference to bean 'blPersistenceUnitManager' while setting bean property 'persistenceUnitManager'; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blPersistenceUnitManager': Injection of resource dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blMergedDataSources' defined in resource loaded from byte array: Cannot resolve reference to bean 'webDS' while setting bean property 'sourceMap' with key [TypedStringValue: value [jdbc/web], target type [null]];     

最终跟踪堆栈跟踪是由于出于调试目的而抛出的错误以及尝试终止导致非法访问的线程而引起的,并且没有任何功能影响。

context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<Context>

<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>

<ResourceLink global="jdbc/web" name="jdbc/web" type="javax.sql.DataSource"/>


</Context>

的Server.xml

<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
    <Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/>
    <Listener className="org.apache.catalina.core.JasperListener"/>
    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
    <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener"/>
    <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
    <GlobalNamingResources>

    <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>

    <Resource auth="Container" 
    driverClassName="com.mysql.jdbc.Driver" 
    initialSize="5" 
    maxActive="120" 
    maxIdle="5" 
    maxWait="5000" 
    name="jdbc/web" 
    username="root" 
    password="admin123" 
    poolPreparedStatements="true" 
    type="javax.sql.DataSource" 
    url="jdbc:mysql://localhost:3306/broadleaf" 
    validationQuery="select 1"/>
    </GlobalNamingResources>

   <Service name="Catalina">
      <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

      <!-- Define an AJP 1.3 Connector on port 8009 -->
      <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
      <Engine defaultHost="localhost" name="Catalina">

         <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>

         <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true" xmlNamespaceAware="false" xmlValidation="false">

         </Host>
      </Engine>
  </Service>
 </Server>

感谢您的宝贵意见。我用server.xml和context.xml编辑了帖子。

我正在关注broadleafcommerce的教程[http://www.broadleafcommerce.com/docs/core/current/getting-started]

为了配置切换到MySql ,我在此链接中进行了更改[http://www.broadleafcommerce.com/docs/core/current/tutorials/getting-started-tutorials/switch-to-mysql-tutorial]

的applicationContext-servlet.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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.2.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">


<context:component-scan base-package="org.broadleafcommerce.common.web"/>
<context:component-scan base-package="org.broadleafcommerce.profile.web.controller"/>
<context:component-scan base-package="org.broadleafcommerce.profile.web.core.service.login"/>


<context:component-scan base-package="com.heatclinic.sample" />

<!-- Turn on AOP annotations (required by Broadleaf) -->
<aop:config/>


<bean class="org.broadleafcommerce.core.web.catalog.ProductHandlerMapping">
  <property name="order" value="2"/>
</bean>


<bean class="org.broadleafcommerce.cms.web.PageHandlerMapping">
  <property name="order" value="3"/>
</bean>


<bean class="org.broadleafcommerce.core.web.catalog.CategoryHandlerMapping">
  <property name="order" value="4"/>
</bean>


<context:component-scan base-package="com.heatclinic.controller" />


<mvc:annotation-driven/>



<mvc:interceptors>
    <bean id="webContentInterceptor" class="org.broadleafcommerce.core.web.interceptor.NonResourceWebContentInterceptor">
        <property name="cacheSeconds" value="0"/>
        <property name="useExpiresHeader" value="true"/>
        <property name="useCacheControlHeader" value="true"/>
        <property name="useCacheControlNoStore" value="true"/>
    </bean>

    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="blLocaleCode"/>
    </bean>
</mvc:interceptors>


<bean id="localeResolver" class="org.broadleafcommerce.common.web.BroadleafCookieLocaleResolver" />


<mvc:resources order="-10" location="/img/, classpath:/common_style/img/" mapping="/img/**" />
<mvc:resources order="-10" location="/fonts/, classpath:/common_style/fonts/" mapping="/fonts/**" />
<mvc:resources order="-10" location="WEB-INF/favicon.ico" mapping="/favicon.ico" />


<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="order" value="-10" />
    <property name="mappings">
        <props>
            <prop key="/js/**">blJsResources</prop>
            <prop key="/css/**">blCssResources</prop>
        </props>
    </property>
</bean>

<!-- Set up the view resolver to be used by Spring -->
<bean class="org.broadleafcommerce.common.web.BroadleafThymeleafViewResolver">
    <property name="templateEngine" ref="blWebTemplateEngine" />
    <property name="order" value="1" />
    <property name="cache" value="${thymeleaf.view.resolver.cache}" />
    <property name="fullPageLayout" value="layout/fullPageLayout" />
    <property name="characterEncoding" value="UTF-8" />
    <property name="layoutMap">
        <map>
            <entry key="account/" value="layout/accountLayout" />
            <entry key="catalog/" value="NONE" />
            <entry key="checkout/" value="layout/checkoutLayout" />
            <entry key="checkout/confirmation" value="layout/fullPageNoNavLayout" />
            <entry key="layout/" value="NONE" />
            <entry key="content/NONE" value="NONE" />
        </map>
    </property>
</bean>


<bean id="blShippingInfoFormValidator" class="org.broadleafcommerce.core.web.checkout.validator.USShippingInfoFormValidator" />

</beans>

即使我已将 hibernate-3.2.7.ga.jar 文件添加到lib目录,我也会收到此异常-ClassLoadingException:无法加载类[org.hibernate.dialect.MySQL5InnoDBDialect] 。

[ERROR] 11:09:39 ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blEntityManagerFactorySecureInfo' defined in resource loaded from byte array: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: blSecurePU] Unable to build EntityManagerFactory

Caused by: javax.persistence.PersistenceException: [PersistenceUnit: blSecurePU] Unable to build EntityManagerFactory ....

Caused by: org.hibernate.HibernateException: Dialect class not found: org.hibernate.dialect.MySQL5InnoDBDialect ...

Caused by: org.hibernate.service.classloading.spi.ClassLoadingException: Unable to load class [org.hibernate.dialect.MySQL5InnoDBDialect ] ....

Caused by: java.lang.ClassNotFoundException: Could not load requested class : org.hibernate.dialect.MySQL5InnoDBDialect ...

但是jar文件确实有类。

3576 Wed Jun 03 16:19:06 PDT 2009 org/hibernate/dialect/MckoiDialect.class
5326 Wed Jun 03 16:19:06 PDT 2009 org/hibernate/dialect/MimerSQLDialect.class
595 Wed Jun 03 16:19:06 PDT 2009 org/hibernate/dialect/MySQL5Dialect.class
648 Wed Jun 03 16:19:06 PDT 2009 org/hibernate/dialect/MySQL5InnoDBDialect.class
9682 Wed Jun 03 16:19:06 PDT 2009 org/hibernate/dialect/MySQLDialect.class
642 Wed Jun 03 16:19:06 PDT 2009 org/hibernate/dialect/MySQLInnoDBDialect.class
544 Wed Jun 03 16:19:06 PDT 2009 org/hibernate/dialect/MySQLMyISAMDialect.class

此外,我还没有提到有关pom.xml 文件中 hibernate依赖项的任何内容。

的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/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.heatclinic</groupId>
<artifactId>ecommerce-website</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>ecommerce</name>
<url>http://www.blc-archetype.com</url>
<properties>
    <blc.version>3.1.8-GA</blc.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<description>My Company's eCommerce Website</description>
<repositories>
    <repository>
        <id>public snapshots</id>
        <name>public snapshots</name>
        <url>http://nexus.broadleafcommerce.org/nexus/content/repositories/snapshots/</url>
    </repository>
</repositories>
<build>
    <pluginManagement>
        <plugins>
            <plugin>

                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.zeroturnaround</groupId>
                                    <artifactId>jrebel-maven-plugin</artifactId>
                                    <versionRange>[1.0,)</versionRange>
                                    <goals>
                                        <goal>generate</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore />
                                </action>
                            </pluginExecution>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>org.apache.maven.plugins</groupId>
                                    <artifactId>maven-antrun-plugin</artifactId>
                                    <versionRange>[1.3,)</versionRange>
                                    <goals>
                                        <goal>run</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore />
                                </action>
                            </pluginExecution>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>
                                        org.codehaus.mojo
                                    </groupId>
                                    <artifactId>
                                        build-helper-maven-plugin
                                    </artifactId>
                                    <versionRange>
                                        [1.7,)
                                    </versionRange>
                                    <goals>
                                        <goal>
                                            timestamp-property
                                        </goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>
                                        org.codehaus.mojo
                                    </groupId>
                                    <artifactId>
                                        keytool-maven-plugin
                                    </artifactId>
                                    <versionRange>
                                        [1.5,)
                                    </versionRange>
                                    <goals>
                                        <goal>clean</goal>
                                        <goal>generateKeyPair</goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.0.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.zeroturnaround</groupId>
                <artifactId>jrebel-maven-plugin</artifactId>
                <version>1.1.3</version>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.7</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.10</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <configuration>
                    <excludeDefaultDirectories>true</excludeDefaultDirectories>
                    <filesets>

                        <fileset>
                            <directory>target</directory>
                            <includes>
                                <include>**/*</include>
                            </includes>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

<profiles>
    <profile>
        <id>default-profile</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <file>
                <exists>${java.home}/../lib/tools.jar</exists>
            </file>
        </activation>
        <properties>
            <toolsjar>${java.home}/../lib/tools.jar</toolsjar>
        </properties>
    </profile>
    <profile>
        <id>mac-profile</id>
        <activation>
            <activeByDefault>false</activeByDefault>
            <file>
                <exists>${java.home}/../Classes/classes.jar</exists>
            </file>
        </activation>
        <properties>
            <toolsjar>${java.home}/../Classes/classes.jar</toolsjar>
        </properties>
    </profile>
</profiles>
<dependencyManagement>
    <dependencies>
        <!-- Activated by the profiles above for the paths to tools.jar-->
        <dependency>
            <groupId>com.sun</groupId>
            <artifactId>tools</artifactId>
            <version>[1.6,)</version>
           <scope>system</scope>
           <systemPath>${toolsjar}</systemPath>
        </dependency>

        <dependency>
            <groupId>com.heatclinic</groupId>
            <artifactId>core</artifactId>
            <version>1.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>

        <!--Broadleaf libraries -->
        <dependency>
            <groupId>org.broadleafcommerce</groupId>
            <artifactId>broadleaf-common</artifactId>
            <version>${blc.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.broadleafcommerce</groupId>
            <artifactId>broadleaf-profile</artifactId>
            <version>${blc.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.broadleafcommerce</groupId>
            <artifactId>broadleaf-profile-web</artifactId>
            <version>${blc.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.broadleafcommerce</groupId>
            <artifactId>broadleaf-framework</artifactId>
            <version>${blc.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.broadleafcommerce</groupId>
            <artifactId>broadleaf-framework-web</artifactId>
            <version>${blc.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.broadleafcommerce</groupId>
            <artifactId>broadleaf-admin-module</artifactId>
            <version>${blc.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.broadleafcommerce</groupId>
            <artifactId>broadleaf-contentmanagement-module</artifactId>
            <version>${blc.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.broadleafcommerce</groupId>
            <artifactId>broadleaf-open-admin-platform</artifactId>
            <version>${blc.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.broadleafcommerce</groupId>
            <artifactId>integration</artifactId>
            <version>${blc.version}</version>
            <type>jar</type>
            <classifier>tests</classifier>
            <scope>test</scope>
        </dependency>

        <!-- Other dependencies -->
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.3.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <type>jar</type>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>5.9</version>
            <type>jar</type>
            <classifier>jdk15</classifier>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jdbc</artifactId>
            <version>7.0.30</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.26</version>
            <type>jar</type>
            <scope>provided</scope>
        </dependency> 
    </dependencies>
</dependencyManagement>
<modules>
    <module>admin</module>
    <module>core</module>
    <module>site</module>
</modules>
</project>

我在项目中找不到名为 applicationContext-servlet-cms-contentClient.xml 的文件。

2 个答案:

答案 0 :(得分:1)

我希望您为弹簧配置发布xml,这样我就可以看到是否还有其他问题,但至少您需要定义所有您的jndi tomcat中的名称 - 在server.xml中<resource />中的<globalnames /><resourcelink />标记中的<context />中的<GlobalNamingResources> <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" initialSize="5" maxActive="120" maxIdle="5" maxWait="5000" name="jdbc/web" username="user" password="password" poolPreparedStatements="true" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/dbname" validationQuery="select 1"/> </GlobalNamingResources> 。 Server.xml看起来像这样(你需要为你使用的db类型选择合适的设置):

<context>    
   <ResourceLink global="jdbc/web" name="jdbc/web" type="javax.sql.DataSource"/>
</context>

虽然不能删除已定义的资源。这在context.xml中:

{{1}}

我不想因为包含链接而感到沮丧但是如果你搜索&#34; tomcat jndi数据源如何&#34;我想你至少可以找到一个完整的例子来说明需要什么。您还需要在tomcat / lib中包含包含驱动程序的jar(本例中为com.mysql.jdbc.Driver)。

答案 1 :(得分:1)

最后我回答了我的问题。将 hibernate依赖项添加到我的 pom.xml 就可以了。另外不要忘记将hibernate jar(hibernate-3.2.7.ga.jar)文件添加到lib目录。

<dependency>
   <groupId>org.hibernate</groupId>
    <artifactId>hibernate</artifactId>
    <version>3.2.7.ga</version>
    <type>jar</type>
    <scope>provided</scope>
</dependency>

我认为添加上面的代码解决了ClassLoadingException:无法加载我的帖子中提到的类[org.hibernate.dialect.MySQL5InnoDBDialect]。

来自Bill的答案修复了JNDI命名错误。