spring MVC hello world应用程序显示了CannotLoadBeanClassException

时间:2015-03-08 08:43:21

标签: java spring-mvc

我是Spring MVC的新手。我正在接受来自bellow链接的教程: Spring Hello World

在tomcat服务器上运行应用程序时,它显示以下异常,我猜它与控制器类文件路径有关,但无法解决:

javax.servlet.ServletException: Servlet.init() for servlet SpringMVCTutorial threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
    org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
    java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Unknown Source)
root cause

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [java.com.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: java.com.springmvctutorial.web.controller.HelloWorldController
    org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1266)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:581)
    org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1335)
    org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:901)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:652)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:600)

2 个答案:

答案 0 :(得分:1)

你在SpringMVCTutorial-servlet.xml放错了类路径。使用以下路径

<bean name="/helloworld.htm"
     class="com.springmvctutorial.web.controller.HelloWorldController"/>

答案 1 :(得分:0)

看起来您对应java.com.springmvctutorial.web.controller.HelloWorldController的类文件不在classpath下。

这里有两件事要检查; -

1)是否创建了HelloWorldController的类文件。

2)如果是,包含即java.com.springmvctutorial.web.controller.HelloWorldController的文件夹应位于类路径下

理想情况下,包名称应以com.springmvctutorial开头..而不是java.com ..根据java约定