2011-03-01 173 views
2

我想在eclipse中使用spring和hibernate運行一個web應用程序。類未找到異常

調度 - 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是Java資源:src文件夾該項目在日食。

回答

2
  1. 檢查類應該有在src/com/vannila/domain/User.java

  2. 檢查這個類被註釋爲顯示here。而比較,你錯過了的事

+0

這個類在那裏,dispatcher-servlet.xml也是一樣的..我做了修改,只反映我使用mysql – elle

+0

我希望基本註釋在那裏 –

0

請在eclipse中創建包並將User.java添加到包中。

+0

我已經完成了這項工作 – elle

+0

我創建的軟件包位於Java Resources:src – elle

0

我嘗試了代碼,一切工作正常。

起初我懷疑<list>會給列表不是數組的實例,但是當我嘗試它時,它做得很好。

春天將拋出錯誤,如你上面寫的如果不能找到你想在這種情況下com.vaannila.domain.User

包括檢查的編譯類的輸出文件夾的類。有沒有User.class存在併成功編譯?

0

這是很容易,它是碰撞發生在你有。請檢查您包含的lib庫,在路徑:

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

你可以找到兩個文件命名,如:asm.jar和ASM -2.xxjar 這兩個jar文件來自不同的來源,其中一個來自spring,另一個來自hibernate,所以這裏發生了一些衝突,tomcat無法決定添加哪個jar,因此它會報告異常。

2

總是在eclipse中檢查'Problems'選項卡/視圖。我得到同樣的問題,原因很簡單,我包含「org.springframework.spring-library-3.1.0.RELEASE.libd」文件作爲庫時我正在配置構建路徑,這是在同一個「dist」文件夾中,所以我只是刪除了它,並且一切都正常工作