Spring Hibernate PostgreSQL配置问题

时间:2012-08-22 14:26:50

标签: spring hibernate

我遇到了关于spring hibernate和postgresql配置的各种问题。我尝试解决一个问题,我得到一些不同的问题。 我使用的是Spring 3.1.0发行版和hibernate 4.1.4.Final 我收到错误BeanFactory未初始化或已经关闭 - 在通过ApplicationContext访问bean之前调用'refresh'

我的web.xml是

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

我的调度员 - servlet是:

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
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.1.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<mvc:annotation-driven> </mvc:annotation-driven>
<context:component-scan base-package="com.max.ade.common.model" />
<context:component-scan base-package="com.max.ade.daoImpl" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://localhost:5432/TestUserDB" />
    <property name="username" value="postgres" />
    <property name="password" value="root123" />
</bean>
<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean">

    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    <!-- Scan packages for annotated entities -->
    <property name="packagesToScan" value="com.max.ade.common.model" />
</bean>
<!-- Transaction support beans -->
<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-  driven>
</beans>

在问题解决之后,上面是我的dispatcher-servlet.xml的文件结构。 配置事项是否与hibernate 3.6.4和hibernate 4.1.4的工作方式不同,除了提到的问题(BeanFactory未初始化或已经关闭 - 在通过ApplicationContext访问bean之前调用'refresh')

任何具体指针都会有很大的帮助。

感谢。

1 个答案:

答案 0 :(得分:2)

好像你正在混合使用hibernate 3和hibernate 4,

尝试更改此

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

到这个

class="org.springframework.orm.hibernate4.HibernateTransactionManager">

此致