Tomcat给出了org.springframework.beans.factory.CannotLoadBeanClassException:

时间:2014-06-09 04:22:45

标签: java spring class tomcat

我正在使用以下设置

WEB-INF /类/ COM / MYORG / springmvctutorial /网络/控制器/ HelloWorldController.java

  package com.myorg.springmvctutorial.web.controller;
  import org.springframework.web.servlet.mvc.Controller;
  import org.springframework.web.servlet.ModelAndView;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import java.io.IOException;

  public class HelloWorldController implements Controller {
    protected final Log logger = LogFactory.getLog(getClass());
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse       response)
      throws ServletException, IOException {
      logger.info("Return View");
      return new ModelAndView("helloworld.jsp");
    }
  }

WEB-INF / web.xml中

  <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="true">

<servlet>
        <servlet-name>SpringMVCTutorial</servlet-name>
        <servlet-class>
          org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
        <servlet-name>SpringMVCTutorial</servlet-name>
        <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
</web-app>

WEB-INF / SpringMVCTutorial-servlet.xml中

  <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0" metadata-complete="true">

<servlet>
        <servlet-name>SpringMVCTutorial</servlet-name>
        <servlet-class>
          org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
        <servlet-name>SpringMVCTutorial</servlet-name>
        <url-pattern>*.htm</url-pattern>
  </servlet-mapping>
</web-app>

然而,当我启动我的tomcat时,它找不到HelloWorldController的类定义。有人能发现错误吗?这是我得到的例外

  Jun 09, 2014 12:04:57 AM org.apache.catalina.core.ApplicationContext log
  INFO: Initializing Spring FrameworkServlet 'SpringMVCTutorial'
  Jun 09, 2014 12:04:57 AM org.apache.catalina.core.ApplicationContext log
  SEVERE: StandardWrapper.Throwable
  org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class       [com.myorg.springmvctutorial.web.controller.HelloWorldController] for bean with name '/helloworld.htm' defined in ServletContext resource [/WEB-INF/SpringMVCTutorial-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.myorg.springmvctutorial.web.controller.HelloWorldController
    at       org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1327)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:594)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1396)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:959)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:683)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1267)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1186)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1081)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5027)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1114)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1673)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
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: com.myorg.springmvctutorial.web.controller.HelloWorldController
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:247)
at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:395)
at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1348)
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1319)
... 29 more

  Jun 09, 2014 12:04:57 AM org.apache.catalina.core.StandardContext loadOnStartup
  SEVERE: Servlet /SpringMVC threw load() exception
  java.lang.ClassNotFoundException:       com.myorg.springmvctutorial.web.controller.HelloWorldController
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)

我花了很多时间调试这个。请帮忙!

1 个答案:

答案 0 :(得分:1)

它尝试加载的Bean类名称是

    com.myorg.springmvctutorial.web.controller.HelloWorldController

哪个匹配控制器类的完全限定名称:

    com.myorg.springmvctutorial.web.controller.HelloWorldController

然而JVM无法加载它。这很可能意味着“.class”文件不在webapp中正确的位置供类加载器查找。然后我看到了这个:

    WEB-INF/classes/com/myorg/springmvctutorial/web/controller/HelloWorldController.java

这是一个“.java”文件而不是“.class”文件。 (什么?!?)

所以我怀疑问题在于您已将源代码而不是已编译的代码放入您的webapp中。像这样的错误肯定会解释症状......