ClassNotFoundException,即使该类可用

时间:2015-10-12 10:57:29

标签: java spring spring-mvc

我的问题是,即使课程OrderDAOOrderDAOIml可用,我也会收到此错误,即使所有内容都运行良好。我的堆栈跟踪如下:

   SEVERE: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping': Initialization of bean failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.exclusively.oms.dao.OrderDAOIml] for bean with name 'dataDao' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.exclusively.oms.dao.OrderDAOIml
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.exclusively.oms.dao.OrderDAOIml] for bean with name 'dataDao' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.exclusively.oms.dao.OrderDAOIml
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.exclusively.oms.service.OrderServiceIml] for bean with name 'dataServices' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.exclusively.oms.service.OrderServiceIml
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)

    ... 32 more
Caused by: java.lang.ClassNotFoundException: com.exclusively.oms.dao.OrderDAOIml
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:249)
    at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:395)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1349)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1320)
    ... 49 more
Related cause:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.exclusively.oms.dao.OrderDAOIml] for bean with name 'dataDao' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.exclusively.oms.dao.OrderDAOIml
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1328)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:622)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:591)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1397)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:434)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:404)

Caused by: java.lang.ClassNotFoundException: com.exclusively.oms.dao.OrderDAOIml
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:249)
    at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:395)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1349)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1320)
    ... 51 more
Related cause:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.exclusively.oms.service.OrderServiceIml] for bean with name 'dataServices' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.exclusively.oms.service.OrderServiceIml
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1328)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:622)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:591)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1397)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:434)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:404)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:519)
    at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1060)
    at org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors(BeanFactoryUtils.java:276)
    at org.springframework.web.servlet.handler.AbstractHandlerMapping.detectMappedInterceptors(AbstractHandlerMapping.java:221)
    at org.springframework.web.servlet.handler.AbstractHandlerMapping.initApplicationContext(AbstractHandlerMapping.java:196)
    at org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.initApplicationContext
    ... 51 more

Oct 12, 2015 3:38:28 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet spring
java.lang.ClassNotFoundException: com.exclusively.oms.dao.OrderDAOIml
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1547)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:249)
    at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:395)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1349)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1320)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:622)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType

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

<context:annotation-config />

<mvc:default-servlet-handler />

<context:component-scan base-package="com.exclusively.oms" />  

<mvc:annotation-driven />  

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
<property name="driverClassName" value="com.mysql.jdbc.Driver" />  
<property name="url" value="jdbc:mysql://localhost:3306/employee_db" />  
<property name="username" value="root" />  
<property name="password" value="root" />  
</bean>  

<bean id="sessionFactory"  
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">  
<property name="dataSource" ref="dataSource" />  
<property name="annotatedClasses">  
<list>  
<value>com.exclusively.oms.entity.Order</value>  
</list>  
</property>
<property name="hibernateProperties">  
<props>  
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>  
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>  
</props>  
</property>  
</bean>  

<bean id="txManager"  
class="org.springframework.orm.hibernate4.HibernateTransactionManager">  
<property name="sessionFactory" ref="sessionFactory" />  
</bean>  


<bean id="persistenceExceptionTranslationPostProcessor"  
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<bean id="dataDao" class="com.exclusively.oms.dao.OrderDAOIml"></bean>  
<bean id="dataServices" class="com.exclusively.oms.service.OrderServiceIml"></bean>  

<tx:annotation-driven transaction-manager="txManager" />

</beans>

和web.xml:

 <!DOCTYPE web-app PUBLIC  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"  "http://java.sun.com/dtd/web-app_2_3.dtd" >

 <web-app>
 <display-name>Archetype Created Web Application</display-name>

 <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>/</url-pattern>
</servlet-mapping>

2 个答案:

答案 0 :(得分:1)

如果类存在于类路径下,则不能抛出

ClassNotFoundException。这肯定会是代码问题。这些可能是原因: -

1)类存在但未部署在classpath / right文件夹下

2)当应用程序尝试通过其字符串名称加载类时抛出的ClassNotFoundException,因此编译可以通过,但在运行时它将是一个问题。所以可能是查找包不正确但部署在正确的路径

答案 1 :(得分:0)

尝试使用相同的类路径从命令行执行此操作:

javap com.exclusively.oms.dao.OrderDAOIml
javap com.exclusively.oms.service.OrderServiceIml

如果存在这些类,则会对其API进行描述。如果没有,则出错。