2

この方法で 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 で 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 を導入しました。2 番目: アノテーション@Repositoryを使用して、データベース メソッドを受け取る各 DAO にリポジトリを作成します。3 番目: DAO ごとにこの方法で EntityManage を作成しました。

@PersistenceContext
private EntityManager manager;
4

1 に答える 1

0

これは完全な答えではありません。私はあなたに方向を示しているだけです。

Spring はあなたの ConnectionFactory クラスを見つけることができないため、entityManagerFactory を注入しません。entityManager を渡すためのシングルトンを再度作成する必要はないため、 ConnectionFactory クラスは必要ありません。Spring は、DAO やコントローラーなどに注入することでそれを行います。たとえば、データを取得する次の 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 がエンティティ マネージャーを認識して挿入できなくなります。

于 2014-10-22T18:01:09.620 に答える