Ghost Rider
Ghost Rider

Reputation: 795

Spring MVC with tiles: java.lang.NoClassDefFoundError: org/springframework/beans/factory/NoUniqueBeanDefinitionException

I am new to spring and I am following some of the online tutorial website to try my 1st web site with spring-mvc , hibernate with mySQL as back end, tiles.

I have configures some of the files and included all the neccessary jars to this project and when I try to start my Apache-tomcat-7 instance from my eclipse I am getting the below error.

I have read more than 15 threads with the same exception but since my lack of knowlendge I couldn't resolve my issue. I am struggiling since 2 days to resolve this issues.

Let me know if i need to paste any other information.

Below is my error Message:

java.lang.NoClassDefFoundError:  org/springframework/beans/factory/NoUniqueBeanDefinitionException
 at java.lang.Class.getDeclaredMethods0(Native Method)
 at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
 at java.lang.Class.getDeclaredMethods(Unknown Source)
 at org.springframework.core.type.StandardAnnotationMetadata.hasAnnotatedMethods(StandardAnnotationMetadata.java:136)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.checkConfigurationClassCandidate(ConfigurationClassBeanDefinitionReader.java:320)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:157)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:132)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:584)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:405)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:443)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:459)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:340)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:307)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5327)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5617)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.springframework.beans.factory.NoUniqueBeanDefinitionException
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1856)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1705)
... 27 more

  Nov 03, 2016 11:56:56 AM org.apache.catalina.core.StandardContext loadOnStartup
  SEVERE: Servlet [spring] in web application [/shoppingCart] threw load() exception
  java.lang.ClassNotFoundException: org.springframework.beans.factory.NoUniqueBeanDefinitionException
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1856)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1705)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getDeclaredMethods(Unknown Source)
at org.springframework.core.type.StandardAnnotationMetadata.hasAnnotatedMethods(StandardAnnotationMetadata.java:136)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.checkConfigurationClassCandidate(ConfigurationClassBeanDefinitionReader.java:320)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:157)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:132)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:584)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:405)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:443)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:459)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:340)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:307)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
at javax.servlet.GenericServlet.init(GenericServlet.java:158)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5327)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5617)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

 Nov 03, 2016 11:56:56 AM org.apache.coyote.AbstractProtocol start
 INFO: Starting ProtocolHandler ["http-bio-1255"]
 Nov 03, 2016 11:56:56 AM org.apache.coyote.AbstractProtocol start
 INFO: Starting ProtocolHandler ["ajp-bio-8010"]
 Nov 03, 2016 11:56:56 AM org.apache.catalina.startup.Catalina start
 INFO: Server startup in 3663 ms

web.xml:

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" version="3.0">

   <display-name>CakeShoppingCart</display-name>
  <welcome-file-list>
    <welcome-file>WEB-INF/jsp/index.jsp</welcome-file>
  </welcome-file-list>

   <servlet>  
      <servlet-name>spring</servlet-name>  
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
       <load-on-startup>1</load-on-startup>  
    </servlet>  

   <servlet-mapping>  
      <servlet-name>spring</servlet-name>  
      <url-pattern>*.jsp</url-pattern>  
   </servlet-mapping>
 </web-app>

spring-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:p="http://www.springframework.org/schema/p"    
 xmlns:context="http://www.springframework.org/schema/context"    
 xsi:schemaLocation="http://www.springframework.org/schema/beans    
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
 http://www.springframework.org/schema/context    
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:annotation-config/>
 <context:component-scan base-package="com.cakecart.spring.controller"/>

 <bean id="viewResolver"  
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">  
    <property name="viewClass">  
        <value>  
            org.springframework.web.servlet.view.tiles2.TilesView  
        </value>  
    </property>  
</bean>
<bean id="tilesConfigurer"  
    class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">  
    <property name="definitions">  
        <list>  
            <value>/WEB-INF/tiles.xml</value>  
        </list>  
    </property>  
   </bean>
 </beans>

application-context.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:p="http://www.springframework.org/schema/p"  
  xmlns:context="http://www.springframework.org/schema/context" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans   
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context    
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">  

 <bean id="dataSource"  class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
 <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
 <property name="url" value="jdbc:mysql://PGGW010.ericsson.se:3306/shopping_cart?useSSL=false" />  
 <property name="username" value="cart_user" />  
 <property name="password" value="cart_user" />  
 </bean>  

 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" scope="singleton">
  <property name="dataSource" ref="dataSource"/>
  <property name="packagesToScan" value="com.cakecart.resource"/>

  <property name="hibernateProperties">
    <props>
        <prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
        <!-- validate | update | create | create-drop -->
        <prop key="hibernate.hbm2ddl.auto">update</prop>
    </props>
  </property>

 </bean>
 <bean id="employeeDaoImpleObj" class="com.cakecart.resource.dao.impl.EmployeeDaoImpl" scope="singleton"/>

 </beans>  

Here is the structure of my eclipse project:

enter image description here

Update 03-Nov-2016: posting my build.xml file and lib image file as per the request @javaguy :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build" name="CakeShoppingCart">

<property environment="env"/>
<property name="ECLIPSE_HOME" value="E:/Softwares/Java/eclipse/eclipse_ee_Juno/eclipse"/>
<property name="debuglevel" value="source,lines,vars"/>
<property name="dest.dir" value="dest"/>
<property name="lib.dir" value="lib"/>
<property name="target" value="1.7"/>
<property name="source" value="1.7"/>
<property name="build.dir" value="build/classes"/>
<path id="CakeShoppingCart.classpath">
    <pathelement location="build/classes"/>
</path>

<target name="war" depends="jar">
    <echo message="${ant.project.name}: Preparing war file to deploye">   </echo>
    <war destfile="${dest.dir}/${ant.project.name}.war" webxml="WebContent/WEB-INF/web.xml">
        <fileset dir="WebContent"></fileset>
        <lib dir="${lib.dir}"/>
    </war>
 </target>
 <target name="jar" depends="build">
    <echo message="${ant.project.name}: Creating JAR file of the project src"/>
    <jar destfile="${lib.dir}/${ant.project.name}.jar" basedir="${build.dir}"></jar>
</target>
<target name="init" depends="cleanall">
    <mkdir dir="build/classes"/>
    <mkdir dir="${dest.dir}" />
    <copy includeemptydirs="false" todir="build/classes">
        <fileset dir="src">
            <exclude name="**/*.java"/>
        </fileset>
    </copy>
</target>
<target name="clean">
    <echo message="${ant.project.name}: cleaning the project"/>
    <delete dir="build/classes"/>
    <delete dir="${dest.dir}"/>
</target>
<target depends="clean" name="cleanall"/>
<target depends="init" name="build">
    <echo message="${ant.project.name}: Build source files of the project"/>
    <javac debug="true" debuglevel="${debuglevel}" destdir="build/classes" includeantruntime="false" source="${source}" target="${target}">
        <src path="src"/>
        <classpath refid="CakeShoppingCart.classpath"/>
    </javac>
  </target>
</project>

enter image description here

Upvotes: 0

Views: 904

Answers (1)

Vasu
Vasu

Reputation: 22442

You have provided the Spring classes (dependent jars) during the project compilation time, but not during the runtime.

Your Apache-tomcat-7 server NOT able to resolve and load the Spring dependencies (jar files), so you need to add the Spring jars to the Tomcat classpath.

So, copy the spring dependency jar files to the Tomcat/webapps/yourproject/WEB-INF/lib folder or directly to Tomcat/lib folder.

Upvotes: 1

Related Questions