Spring Maven org.springframework.beans.factory.BeanCreationException

时间:2015-12-11 10:59:36

标签: java spring maven spring-mvc

这是Stack:

Error creating bean with name 'org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping#0' defined in ServletContext resource [/WEB-INF/mvc-config.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.pankanis.controller.CustomerController] for bean with name 'CustomerController' defined in ServletContext resource [/WEB-INF/mvc-config.xml]; nested exception is java.lang.ClassNotFoundException: com.pankanis.controller.CustomerController
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.pankanis.controller.CustomerController] for bean with name 'CustomerController' defined in ServletContext resource [/WEB-INF/mvc-config.xml]; nested exception is java.lang.ClassNotFoundException: com.pankanis.controller.CustomerController
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.pankanis.dao.CustomerDAO] for bean with name 'CustomerDAO' defined in ServletContext resource [/WEB-INF/mvc-config.xml]; nested exception is java.lang.ClassNotFoundException: com.pankanis.dao.CustomerDAO
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.pankanis.model.Customer] for bean with name 'CustomerModel' defined in ServletContext resource [/WEB-INF/mvc-config.xml]; nested exception is java.lang.ClassNotFoundException: com.pankanis.model.Customer
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.pankanis.service.CustomerServiceImpl] for bean with name 'CustomerServiceImpl' defined in ServletContext resource [/WEB-INF/mvc-config.xml]; nested exception is java.lang.ClassNotFoundException: com.pankanis.service.CustomerServiceImpl
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.pankanis.controller.CustomerController] for bean with name 'CustomerController' defined in ServletContext resource [/WEB-INF/mvc-config.xml]; nested exception is java.lang.ClassNotFoundException: com.pankanis.controller.CustomerController
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1351)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:628)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:597)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1444)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:445)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:423)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:409)
    at org.springframework.context.support.AbstractApplicationContext.getBeanNamesForType(AbstractApplicationContext.java:1141)
    at org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.detectHandlers(AbstractDetectingUrlHandlerMapping.java:75)
    at org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.initApplicationContext(AbstractDetectingUrlHandlerMapping.java:58)
    at org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:120)
    at org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:76)
    at org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:74)
    at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:119)
    at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:94)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    ... 21 more
Caused by: java.lang.ClassNotFoundException: com.pankanis.controller.CustomerController
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1713)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1558)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:250)
    at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:394)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1396)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1343)
    ... 38 more

我的CustomerController:

package com.pankanis.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.pankanis.model.Customer;
import com.pankanis.service.*;

@RequestMapping("/customer")
public class CustomerController {

    CustomerController(){}
    @Autowired 
    private CustomerService serv;

    public CustomerService getServ() {
        return serv;
    }

    public void setServ(CustomerService serv) {
        this.serv = serv;
    }

    @RequestMapping(value="/addCustomer",method=RequestMethod.POST)
    public String AddCustomer(@RequestBody Customer cust,HttpServletRequest request,HttpServletResponse response)
    {       

        String CustomerId = serv.AddCustomer(cust);     
        return CustomerId;          
    }
}

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

    <!-- Uncomment and your base-package here: <context:component-scan base-package="org.springframework.samples.web"/> -->



    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>



        <bean
            class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

        <bean id="CustomerController" class="com.pankanis.controller.CustomerController">
            <property name="serv" ref="CustomerServiceImpl"></property>
        </bean>

        <bean id="CustomerDAO" class="com.pankanis.dao.CustomerDAO"></bean>

        <bean id="CustomerModel" class="com.pankanis.model.Customer"></bean>

        <bean id="CustomerServiceImpl" class="com.pankanis.service.CustomerServiceImpl">
            <property name="customerDAO" ref="CustomerDAO"></property>
        </bean>
    </beans>

My web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    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">

    <display-name>SpringMavenWeb</display-name>

    <!-- - Location of the XML file that defines the root application context. 
        - Applied by ContextLoaderListener. -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <!-- - Servlet that dispatches request to registered handlers (Controller 
        implementations). -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

        <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/customer/*</url-pattern>
    </servlet-mapping>

</web-app>

2 个答案:

答案 0 :(得分:0)

验证所请求的类的名称是否正确以及类路径中是否存在相应的.jar文件。如果没有,则必须将其显式添加到应用程序的类路径中。

如果您的类路径中存在指定的.jar文件,那么您的应用程序的类路径将被覆盖,您必须找到应用程序使用的确切类路径。

如果异常是由第三方类引起的,则必须标识抛出异常的类,然后在类路径中添加缺少的.jar文件。

希望有所帮助。

答案 1 :(得分:-1)

尝试公开您的CustomerController构造函数。