繁体   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