struts spring hibernate示例中的org.springframework.beans.factory.BeanCreationException

时间:2012-03-12 11:59:17

标签: spring hibernate struts

我试图在tomcat6中运行spring struts和hibernate示例,我收到了以下错误。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/classes/config/database/spring/HibernateSessionFactory.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: entity class not found: com.mkyong.customer.model.Customer
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    at org.springframework.web.struts.ContextLoaderPlugIn.createWebApplicationContext(ContextLoaderPlugIn.java:354)
    at org.springframework.web.struts.ContextLoaderPlugIn.initWebApplicationContext(ContextLoaderPlugIn.java:295)
    at org.springframework.web.struts.ContextLoaderPlugIn.init(ContextLoaderPlugIn.java:225)
    at org.apache.struts.action.ActionServlet.initModulePlugIns(ActionServlet.java:871)
    at org.apache.struts.action.ActionServlet.init(ActionServlet.java:359)
    at javax.servlet.GenericServlet.init(GenericServlet.java:212)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4421)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4734)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)

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

<!-- Hibernate session factory -->

    <bean id="sessionFactory" 
         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

        <property name="dataSource">
          <ref bean="dataSource"/>
        </property>

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

        <property name="mappingResources">
            <list>
                   <value>com/mkyong/customer/hibernate/Customer.hbm.xml</value>
            </list>
         </property>    

    </bean>
    </beans>

Customer.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 20 April 2010 8:33:09 PM by Hibernate Tools 3.2.5.Beta -->
<hibernate-mapping>
    <class name="com.mkyong.customer.model.Customer" table="customer" catalog="mkyong">
        <id name="customerId" type="long">
            <column name="CUSTOMER_ID" />
            <generator class="identity" />
        </id>
        <property name="name" type="string">
            <column name="NAME" length="45" not-null="true" />
        </property>
        <property name="address" type="string">
            <column name="ADDRESS" not-null="true" />
        </property>
        <property name="createdDate" type="timestamp">
            <column name="CREATED_DATE" length="19" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

SpringBeans.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <!-- Database Configuration -->
    <import resource="config/database/spring/DataSource.xml"/>
    <import resource="config/database/spring/HibernateSessionFactory.xml"/>

    <!-- Beans Declaration -->
    <import resource="com/mkyong/customer/spring/CustomerBean.xml"/>


</beans>

Customer.java的目录结构

StrutsSpringExample
    src/main/java
        com.mkyong.customer
            model
              Customer.java

Customer.java的包

com.mkyong.customer.model

2 个答案:

答案 0 :(得分:1)

根据错误消息,在类路径中找不到以下类:

com.mkyong.customer.model.Customer

答案 1 :(得分:0)

此错误很明显,initializeBean失败,因为无法找到 Customer 类。

现在您需要确保Customer.class存在于目录

# find . -name Customer.class
./WEB-INF/classes/com/mkyong/customer/model/Customer.class
# pwd
/usr/share/tomcat6/webapps/<your_webapp_context>

另外,请确保将所有需要的库放在lib文件夹中。下面是我的webapp lib文件夹中存在的一个。 注意:当不满足库依赖关系时,在war文件部署期间会遇到很多异常。

  • ANTLR-2.7.2.jar
  • 公地BeanUtils的-1.8.0.jar
  • 公地链1.2.jar
  • 公地消化器-1.8.jar
  • 公地-IO-1.1.jar
  • 公地验证-1.3.1.jar
  • DOM4J-1.6.1.jar
  • 的geronimo-JPA-2.0-spec.jar
  • 番石榴11.0.1.jar
  • 冬眠-公地注解-3.2.0.Final.jar
  • 冬眠核-3.6.4.Final.jar
  • 了Javassist-3.12.0.GA.jar
  • 口 - 2.0.8.jar
  • 的PostgreSQL-jdbc4-9.1.jar
  • 石英全1.6.6.jar
  • 弹簧豆-2.5.jar
  • 弹簧上下文2.5.jar
  • 弹簧上下文支持-2.5.jar
  • 弹簧芯2.5.jar
  • 弹簧JDBC-2.5.jar
  • 弹簧ORM-2.5.jar
  • 弹簧tx.jar
  • 弹簧的web-2.5.jar
  • 弹簧webmvc-支柱-2.5.jar
  • 支柱核-1.3.10.jar
  • 支柱-额外-1.3.10.jar
  • 支柱-标签库-1.3.10.jar
  • 支柱瓷砖-1.3.10.jar

我几天前完成了与PostgreSQL集成的教程,它运行正常。如果你愿意,我可以上传我的war文件。