簡體   English   中英

Spring 4和帶有實體管理器的Hibernate 4 / c3p0無法啟動

[英]Spring 4 and Hibernate 4/c3p0 with Entity Manager don't start

我以這種方式創建Spring + JPA / Hibernate / c3p0的配置:

Spring-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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <context:component-scan base-package="com.nassoft.erpweb"/>

    <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/" />

    <mvc:annotation-driven />

    <mvc:interceptors>
        <bean class="com.nassoft.erpweb.login.interceptor.AuthenticatorInterceptor" />
    </mvc:interceptors>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="packagesToScan" value="com.nassoft.erpweb.*" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
            </props>
        </property>
    </bean>

    <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/nsm_erp" />
        <property name="user" value="root" />
        <property name="password" value="1234" />

        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="20" />
        <property name="maxStatements" value="50" />
        <property name="idleConnectionTestPeriod" value="3000" />
        <property name="loginTimeout" value="300" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <tx:annotation-driven />

    <bean id="persistenceExceptionTranslationPostProcessor" class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

</beans>

我沒有使用persistence.xml,因為我在某些地方閱讀了Hibernate在Spring 4中不需要的內容。

當我啟動服務器時,它仍在加載,並且不在Tomcat7中的45s(也不是180s)中啟動。

我創建了一個EntityManager的工廠以在我的項目中使用:

package com.nassoft.erpweb.factory;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUnit;

public class ConnectionFactory {

        @PersistenceUnit
        private static EntityManagerFactory entityManagerFactory;

        public static EntityManager getEntityManager(){
            if (entityManagerFactory == null){
                entityManagerFactory = Persistence.createEntityManagerFactory("ERPWeb");
            }

            return entityManagerFactory.createEntityManager();
        }
}

我認為我的配置不正確,但是我找不到任何地方有很好的文字說明。

有人能幫我嗎?

編輯。

問題解決了! 首先:我在每個控制器中應用了依賴注入 ,以將DAO與IoC結合在一起。 第二:我使用注釋@Repository在每個DAO中創建一個存儲庫,該存儲庫將接收我的數據庫方法。 第三:我以這種方式為每個DAO創建了EntityManage:

@PersistenceContext
private EntityManager manager;

這不是一個完整的答案。 我只是為您指明方向。

Spring無法找到您的ConnectionFactory類,因此它將不會注入entityManagerFactory。 您不需要再次創建一個單例來傳遞entityManager,因此不需要ConnectionFactory類。 Spring將通過注入DAO或Controller等為您完成此操作,例如,您具有以下獲取數據的DAO。

@Service
public class SomeDAO {

     @AutoWire -- i'm not sure what you call for the entityManager.
     private static EntityManagerFactory entityManagerFactory;

}

還有更多的信息在這里 他使用手動注入而不是@autowiring。 我的情況下,您可以嘗試自動裝配。

還要確保在應用程序上下文文件的<spring:component-scan />路徑中具有這些類,否則spring將無法識別並注入實體管理器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM