找不到类的例外

时间:2011-03-01 10:59:02

标签: java eclipse hibernate spring

我正在尝试使用spring和hibernate在eclipse中运行web应用程序。

dispatcher-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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost/test"/>
        <property name="username" value="root"/>
        <property name="password" value="mysql"/>
    </bean>

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.vaannila.domain.User</value>
            </list>
        </property>


        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>

    <bean id="myUserDAO" class="com.vaannila.dao.UserDAOImpl">
        <property name="sessionFactory" ref="mySessionFactory"/>
    </bean>

    <bean name="/user/*.htm" class="com.vaannila.web.UserController" >
        <property name="userDAO" ref="myUserDAO" />
    </bean>

</beans>

我得到的错误是,


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySessionFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.util.ArrayList' to required type 'java.lang.Class[]' for property 'annotatedClasses'; nested exception is java.lang.IllegalArgumentException: Cannot find class [com.vaannila.domain.User]
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:900)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:455)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
    org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
    org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
    javax.servlet.GenericServlet.init(GenericServlet.java:212)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
    java.lang.Thread.run(Thread.java:636)

com.vaannila.domain.User位于eclipse项目的Java Resources:src文件夹中。

5 个答案:

答案 0 :(得分:2)

  1. 检查src/com/vannila/domain/User.java

  2. 中的课程是否存在
  3. 检查该类是否按照here所示进行注释。并比较您错过的地方

答案 1 :(得分:2)

始终检查&#39;问题&#34; eclipse中的选项卡/视图。我得到了同样的问题,原因很简单,我包括了&#34; org.springframework.spring-library-3.1.0.RELEASE.libd&#34;当我配置构建路径时,文件作为库,并且它在同一个&#34; dist&#34;夹。所以我删除了它,一切正常。

答案 2 :(得分:0)

请在eclipse中创建包并将User.java添加到包中。

答案 3 :(得分:0)

我尝试了代码,一切正常。

起初我怀疑<list>会给List的实例不是数组,但是当我尝试它时,它做得很好。

如果他们在这种情况下找不到你想要包含的类com.vaannila.domain.User

,那么弹出会像你上面写的那样抛出错误

检查输出文件夹中的已编译类。 User.class是否存在并成功编译?

答案 4 :(得分:0)

这很简单,就是你所包含的库中发生的碰撞。请检查你所包含的lib,在路径中:

   tomcat6/webapps/<you project name>/WEB-INF/lib/

您可以找到两个名为的文件:asm.jar和asm-2.x.x.jar 两个jar文件来自不同的源码,一个来自spring,另一个来自hibernate,所以这里发生了一些碰撞,tomcat无法决定添加哪个jar。所以报告异常。