Java Spring4和Hibernate4/c3p0与实体管理器don';开始

Java Spring4和Hibernate4/c3p0与实体管理器don';开始,java,spring,hibernate,c3p0,hibernate-entitymanager,Java,Spring,Hibernate,C3p0,Hibernate Entitymanager,我以这种方式创建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://ww

我以这种方式创建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>
我认为我的配置是不正确的,但我没有找到任何地方有一个好的文字

有人能帮我吗

编辑

问题解决了! 第一:我在每个控制器中应用了依赖注入,以使DAO具有IoC。 第二:我使用注释
@Repository
在每个将接收我的数据库方法的DAO中创建一个存储库。 第三:我以这种方式为每个DAO创建了EntityManager:

@PersistenceContext
private EntityManager manager;

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

Spring找不到您的ConnectionFactory类,因此它不会注入entityManagerFactory。您不需要再次创建用于传递entityManager的singleton,因此不需要ConnectionFactory类。Spring将通过注入DAO或控制器等来为您完成此操作,例如,您有以下DAOt获取数据

@Service
public class SomeDAO {

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

}
还有更多的信息。他使用的不是@autowiring,而是手动注射。我想看看你的情况,你可以试试autowiring


还要确保这些类位于应用程序上下文文件的路径中,否则spring将无法识别和注入实体管理器。

您的帖子不是我所期望的,但它像anwser一样非常有用!非常感谢!@felipeocr可能也会提交您的答案,以便其他人会发现它的用处ful:)我会在完成整个解决方案后发布!非常感谢。:)整个解决方案已发布!如果有人对这些配置有问题,可以使用此帖子解决!:)
@Service
public class SomeDAO {

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

}