【问题标题】:Spring 4 and Hibernate 4/c3p0 with Entity Manager don't start带有实体管理器的 Spring 4 和 Hibernate 4/c3p0 无法启动
【发布时间】:2014-12-18 04:47:00
【问题描述】:

我是这样创建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,因为我在某些地方读到它在 Spring 4 with Hibernate 中是不必要的。

当我启动服务器时,它仍在加载,并且在 Tomcat7 中没有在 45 秒(也不是 180 秒)内启动。

我创建了一个 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;

【问题讨论】:

    标签: java spring hibernate c3p0 hibernate-entitymanager


    【解决方案1】:

    这不是一个完整的答案。我只是在给你指明一个方向。

    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;
    
    }
    

    还有更多信息here。他没有使用@autowireing,而是使用手动注入。我是你的情况,你可以尝试自动装配。

    还要确保在应用程序上下文文件的&lt;spring:component-scan /&gt; 路径中有这些类,否则spring 将无法识别和注入实体管理器。

    【讨论】:

    • 您的帖子不是我所期望的,但它很有帮助!非常感谢!
    • @felipeocr 可能也提交您的答案,以便其他人会发现它有用:)
    • 我会在我完成整个解决方案后发布!非常感谢。 :)
    • 已发布整个解决方案!如果有人对这些配置有问题,可以使用这个帖子来解决! :)
    猜你喜欢
    • 1970-01-01
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 2014-06-03
    • 2013-01-27
    • 2015-01-24
    相关资源
    最近更新 更多