0

我的目标是从 applicationContext.xml 文件中实例化 EntityManagerFactory 以获取在 SQL 数据库中注册的所有帖子。以下是主要文件的内容:

应用程序上下文.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Properties files linkers -->

    <context:property-placeholder location="/WEB-INF/resources/database/jdbc.properties"/>
    <context:property-placeholder location="/WEB-INF/resources/database/hibernate.properties"/>

    <!-- Config database - initialization -->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- Three main layers definition -->

    <context:annotation-config />
    <context:component-scan base-package="com.zone42"/>

    <!-- Transaction sub-system initialization -->

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

(WEB-INF/classes/)persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="post-unit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.zone42.model.Post</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
    </persistence-unit>
</persistence>

PostDAO.java

public class PostDAO extends GenericDAOEntity<Post> implements IPostDAO
{

}

GenericDAOEntity.java

@Transactional
public class GenericDAOEntity<T> implements IGenericDAO<T>
{
    /**
     * Properties
     */

    @Autowired
    @PersistenceContext(unitName="post-unit")
    private EntityManagerFactory entityManagerFactory/* = Persistence.createEntityManagerFactory(persistence_unit_name)*/;

    //Get all posts

    @SuppressWarnings("unchecked")
    public List<T> findAll(Class<T> obj) {
        EntityManager entityManager = this.entityManagerFactory.createEntityManager();
        Query query = entityManager.createQuery("from " + obj.getSimpleName());
        return (query.getResultList());
    }

    /**
     * Accessors
     */

    public EntityManagerFactory getEntityManagerFactory() {
        return entityManagerFactory;
    }

    @PersistenceContext(unitName="post-unit")
    public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
        this.entityManagerFactory = entityManagerFactory;
    }   
}

我尝试了几种配置组合,但没有成功。当我想从 entityfactory 实例创建 EntityManager 实例时,来自NullPointerException该方法。findAll我想我有一个配置问题。我想明确代码在我直接在类中使用 operator new 实例化 EntityManagerFactory 时有效。现在我只想分配我的工厂,选择另一种方式,即使用 appicationContext.xml 文件中的 xml。谁能帮我?提前致谢。

4

1 回答 1

0

您需要将字段/设置器标记为@Autowired或在 XML 中明确连接引用:

<bean class="PostDAO">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
于 2013-01-17T17:56:40.333 回答