ContextLoader - Context initialization failed

52.6k views Asked by At

Unable to Start the deployed war file. I got the following error at the terminal

[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]];     

The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.

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>

Thank you for your valuable advice. I have edited the post with server.xml and context.xml.

I am following broadleafcommerce's tutorial [http://www.broadleafcommerce.com/docs/core/current/getting-started].

In order to configure Switching to MySql I have made changes as in this link [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>

Even though I have added hibernate-3.2.7.ga.jar file to the lib directory, I get this exception -ClassLoadingException:Unable to load class [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 ...

But the jar file do have the class.

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

And also I have not mentioned anything about the hibernate dependencies in the pom.xml file.

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>

I don't find the file named applicationContext-servlet-cms-contentClient.xml in the project.

2

There are 2 answers

14
BillFromHawaii On BEST ANSWER

I wish you'd posted your xml for your spring configuration so I could see if there were any more issues but at the very least you'll need to define all your jndi names in tomcat - in server.xml as a <resource /> in <globalnames /> and context.xml as <resourcelink /> in the <context /> tag. Server.xml will look something like this (you will need to select proper setting for the type of db your using):

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

Although don't delete resources already defined. And this in context.xml:

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

I don't want to get dunned for including a link but if you search "tomcat jndi datasource how to" I think you'll find at least one thorough example of what's needed. You'll also need to include the jar containing your driver (com.mysql.jdbc.Driver in this example) in tomcat/lib.

0
Sajith Swaminathan On

Finally I got answer to my question. Adding hibernate dependencies to my pom.xml did the trick. Also don't forget to add hibernate jar(hibernate-3.2.7.ga.jar) file to lib directory.

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

I think adding the above code resolved the ClassLoadingException:Unable to load class [org.hibernate.dialect.MySQL5InnoDBDialect] which was mentioned in my post.

Answers from Bill fixed JNDI naming error.