微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Spring无法注入实体管理器工厂

我使用JPA为我的DAO类编写测试,使用Hibernate作为JPA提供程序,以及Spring 3.2.我无法正确注入实体管理器,尝试访问它时会出现NullPointerException.我的GenericDAO实现如下所示:

@Repository
public class GenericDAOImpl implements GenericDAO {

    @PersistenceContext(unitName="unitname")
    private EntityManager entityManager;

    public EntityManager getEntityManager() {
        return entityManager;
    }


    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    // NullPointerException when calling this,entityManager is null
    public Query createNamedQuery(String name) {
        return entityManager.createNamedQuery(name);
    }        

    // many other methods....
}

测试类看起来像这样:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/com/main/resources/root-context.xml","/com/main/resources/servlet-context.xml"})
public class TestModel {

    @Before
    public void setUp() throws Exception{
        ...
    }

    @Test
    public void test(){
        ...
    }
}

我的root-context.xml如下:

spring-beans-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd        
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        "
>

    
                
                                 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐