2

问题

我无法sessionFactory在我的项目中定义正确的 bean,并且 Web 应用程序初始化失败。

日志输出

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webServiceTest' defined in ServletContext resource [/WEB-INF/
classes/appcontext-spring.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate
' is required
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1336)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:471)
        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:220)
        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.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
        ... 50 more
Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
        at org.springframework.orm.hibernate3.support.HibernateDaoSupport.checkDaoConfig(HibernateDaoSupport.java:117)
        at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1367)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1333)
        ... 60 more

我使用xml映射和 pojos 创建了一个带有 maven 的 ORM 库。我已导入我的项目,但无法配置访问 DDBB。

  • jdbc.properties文件用 DDBB 连接(, 等)很好地db.url定义db.properties
  • 库中的所有 POJO 都扩展了HibernateDaoSupport并实现了基本操作接口 (CRUD)。我正在使用扩展DaoBase的服务webServiceTest进行测试。
  • 我在 ORM 库中进行了JUnit测试,结果很好。

这是appcontext-persistance.xml文件中的dataSource,sessionFactory等定义:

    <!-- PERSISTENCE SETUP -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName">
        <value>${db.driver}</value>
    </property>
    <property name="url">
        <value>${db.url}</value>
    </property>
    <property name="username">
        <value>${db.username}</value>
    </property>
    <property name="password">
        <value>${db.password}</value>
    </property>

    <property name="maxActive">
        <value>${db.maxActive}</value>
    </property>

    <!-- Maximum number of idle dB connections to retain in pool. Set 
        to 0 for no limit -->
    <property name="maxIdle">
        <value>${db.maxIdle}</value>
    </property>

    <!-- Maximum time to wait for a dB connection to become available 
        in ms, in this example 10 seconds. An Exception is thrown if this timeout 
        is exceeded. Set to -1 to wait indefinitely. -->
    <property name="maxWait">
        <value>${db.maxWait}</value>
    </property>

    <!-- Autocommit setting. This setting is required to make Hibernate 
        work. Or you can remove calls to commit(). -->
    <property name="defaultAutoCommit">
        <value>true</value>
    </property>

    <!-- Recover abandoned connections -->
    <property name="removeAbandoned">
        <value>true</value>
    </property>

    <!-- Set the number of seconds a dB connection has been idle before 
        it is considered abandoned. -->
    <property name="removeAbandonedTimeout">
        <value>11</value>
    </property>

    <!-- Log a stack trace of the code which abandoned the dB connection 
        resources. -->
    <property name="logAbandoned">
        <value>true</value>
    </property>

</bean>

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref local="sessionFactory" />
    </property>
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="lobHandler">
        <ref bean="oracleLobHandler" />
    </property>
    <property name="dataSource" ref="dataSource" />

    <!-- configura la fuente de los mappings -->
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />

    <property name="hibernateProperties">
        <props>

            <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9iDialect</prop>
            <prop key="hibernate.generate_statistics">true</prop>

        </props>
    </property>
</bean>

问题:

  • Spring 配置必须从 ORM jar加载hibernate.cfg.xml ?我怎样才能做到这一点 ?
  • 我应该如何将 Spring ORM 项目中的配置加载到另一个项目中?
  • 我在 ORM 中定义了映射,但我没有在 ORM 中定义任何应用程序上下文以包含在 jar 中。
4

1 回答 1

1

我已经使用 beans 标记的属性解决了这个问题,它加载了appcontext-spring.xmldefault-autowire="byName"中所需的对象注入的所有配置。

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
    default-lazy-init="true" default-autowire="byName">

并创建 sessionFactory。

于 2011-11-23T09:05:19.100 回答