Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC: CannotLoadBeanClassException for bean in classpath resource Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC: CannotLoadBeanClassException for bean in classpath resource

    My set up is as follows:
    • Tomcat 7
    • Spring MVC 3.0.6
    • Eclipse Indigo


    File structur looks like this

    Code:
    src
        - main
          - java
          - resources
            - mdp-servlet.xml
            - mdp-service.xml
          - webapp
            - WEB-INF
              - web.xml
        - test
    mdp-servlet.xml

    Code:
    <?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"
        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:component-scan base-package="ch.hszt.mdp.web" />
    
        <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> 
    
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    
    
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    </beans>
    mdp-service.xml

    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
        <bean id="userService" class="ch.hszt.mdp.service.UserServiceImpl" />
    
    </beans>
    web.xml

    Code:
    <?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_2_5.xsd"
        id="WebApp_ID" version="2.5">
    
        <!-- Needed to see file mdp-service.xml -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:mdp-service.xml</param-value>
        </context-param>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    
        <display-name>testing</display-name>
    
        <servlet>
            <servlet-name>mdp</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>mdp</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
    </web-app>
    Sourcefolder:
    • src/main/java
    • src/main/resources
    • src/test/java

    This is the error message I get when I try to start the project with Tomcat within eclipse

    Code:
    org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [ch.hszt.mdp.service.UserServiceImpl] for bean with name 'userService' defined in class path resource [mdp-service.xml]; nested exception is java.lang.ClassNotFoundException: ch.hszt.mdp.service.UserServiceImpl
    somehow I'm getting now a different error...haven't changed anything...

    Code:
    org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/mdp-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/mdp-servlet.xml]

    Has anyone an idea to fix this?

    p.s. Yes the file exists ;-)
    Last edited by gabac; Nov 23rd, 2011, 08:17 AM.

  • #2

    p.s. Yes the file exists ;-)
    No it doesn't... There is no /WEB-INF/mdp-servlet.xml... There is a /WEB-INF/classes/mdp-servlet.xml.. That is quite different!

    The mdp-servlet.xml isn't copied to the WEB-INF but to the root of the class path (which is classes inside the WEB-INF dir). Either move that file or configure the DispatcherServlet to load classpath:mdp-servlet.xml.

    Comment


    • #3
      Sry I'm really new to this framwork. How could I configure the DispatcherServlet?
      Thx again!

      Never mind...

      Code:
      <init-param>
      			<param-name>contextConfigLocation</param-name>
      			<param-value>classpath:mdp-servlet.xml</param-value>
      		</init-param>
      Last edited by gabac; Nov 23rd, 2011, 10:39 AM.

      Comment

      Working...
      X