[英]Spring 4 and Hibernate 4/c3p0 with Entity Manager don't start
I create the configuration of Spring + JPA/Hibernate/c3p0 on this way: 我以这种方式创建Spring + JPA / Hibernate / c3p0的配置:
Spring-Servlet.xml 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>
I'm not using persistence.xml because I read in some places its not necessary in Spring 4 with Hibernate. 我没有使用persistence.xml,因为我在某些地方阅读了Hibernate在Spring 4中不需要的内容。
When I start the server it still loading and don't start in 45s (nor 180s) in Tomcat7. 当我启动服务器时,它仍在加载,并且不在Tomcat7中的45s(也不是180s)中启动。
I create a factory of EntityManager to use in my project: 我创建了一个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();
}
}
I think my configuration is not correct, but I don't found any places with a good text about it. 我认为我的配置不正确,但是我找不到任何地方有很好的文字说明。
Can someone help-me? 有人能帮我吗?
Edited. 编辑。
Problem solved! 问题解决了! First: I applied Dependency Injection in each controller to bring the DAOs with IoC.
首先:我在每个控制器中应用了依赖注入 ,以将DAO与IoC结合在一起。 Second: I use the annotation
@Repository
to create a repository in each DAO that will receive my databases methods. 第二:我使用注释
@Repository
在每个DAO中创建一个存储库,该存储库将接收我的数据库方法。 Third: I created the EntityManage in this way for each DAO: 第三:我以这种方式为每个DAO创建了EntityManage:
@PersistenceContext
private EntityManager manager;
This is not a complete answer. 这不是一个完整的答案。 I"m just pointing you to a direction.
我只是为您指明方向。
Spring cannot find the ConnectionFactory class of yours, so it will not inject the entityManagerFactory. Spring无法找到您的ConnectionFactory类,因此它将不会注入entityManagerFactory。 Its not required for you to again create a singleton for passing the entityManager, so no ConnectionFactory class is required.
您不需要再次创建一个单例来传递entityManager,因此不需要ConnectionFactory类。 Spring will do it for you by injecting into the DAO or Controller etc., for example you have the following DAO that gets the data.
Spring将通过注入DAO或Controller等为您完成此操作,例如,您具有以下获取数据的DAO。
@Service
public class SomeDAO {
@AutoWire -- i'm not sure what you call for the entityManager.
private static EntityManagerFactory entityManagerFactory;
}
There is more info here . 还有更多的信息在这里 。 Instead of @autowiring he is using manual injection.
他使用手动注入而不是@autowiring。 I your case ,you can try with autowiring.
我的情况下,您可以尝试自动装配。
Also make sure that you have these classes in the <spring:component-scan />
path of the application context file or else the spring wont be able to recognize and inject the entity manager. 还要确保在应用程序上下文文件的
<spring:component-scan />
路径中具有这些类,否则spring将无法识别并注入实体管理器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.