2012-12-05 68 views
0

所以我有这个问题,测试春天JPA,这是我的代码:春天JPA不能通过EntityDao

ArticleService

@Service("articleService") 
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true) 
public class ArticleServiceImpl implements ArticleService { 


private ArticleDao articleDao; 

    @Autowired 
    public ArticleServiceImpl(ArticleDao articleDao) { 
    this.articleDao = articleDao; 
    } 

    public ArticleServiceImpl() { 
    } 

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false) 
    public void addArticle(Article article) { 
    articleDao.saveArticle(article); 
    } 
} 

ArticleDao

@Repository("articleDao") 
public class ArticleDaoImpl implements ArticleDao { 

    private EntityManager em; 

    @PersistenceContext 
    public void setEntityManager(EntityManager em) { 
    this.em = em; 
    } 

    // To Save the article detail 
    public void saveArticle(Article article) { 
    article.setAddedDate(new Date()); 
    em.persist(article); 
    } 
} 

问题在于在InputDatabaseServiceImpl类中执行这些方法。

public class InputDatabaseServiceImpl implements InputDatabaseService { 

public ArticleService articleService; 


public InputDatabaseServiceImpl(ArticleService articleService){ 
    this.articleService= articleService; 
} 

public int inputArticle(Article article) { 
    articleService.saveArticle(article); 
    return 0; 
} 

aplication appContext.xml

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

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

<!-- Declare a datasource that has pooling capabilities--> 
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
destroy-method="close" 
p:driverClass="${database.driver}" 
p:jdbcUrl="${database.url}" 
p:user="${database.user}" 
p:password="${database.password}" 
p:acquireIncrement="5" 
p:idleConnectionTestPeriod="60" 
p:maxPoolSize="100" 
p:maxStatements="50" 
p:minPoolSize="10" /> 

<!-- Declare a JPA entityManagerFactory--> 
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" > 
<property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property> 
<property name="persistenceUnitName" value="hibernatePersistenceUnit" /> 
<property name="dataSource" ref="dataSource"/> 
<property name="jpaVendorAdapter"> 
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" > 
<property name="showSql" value="true"/> 
</bean> 
</property> 
</bean> 

<!-- Declare a transaction manager--> 
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
<property name="entityManagerFactory" ref="entityManagerFactory" /> 
</bean> 
</beans> 

每当我打电话inputArticle类从我endpoint我行articleService.saveArticle(article);

我知道这是解决最简单的异常,但因为我有这个struggeling了NullPointerExpcetion一段时间,我需要帮助..任何人都可以给我一个提示,我错过了什么?

+0

你能展示你的弹簧配置吗? – Yevgeniy

+0

我将编辑我的文章 – Grzzzzzzzzzzzzz

回答

1

发生的事情是ArticleService属性未初始化。

这可能是由于InputDatabaseServiceImpl bean配置不匹配或注入依赖关系(如果您的bean是自动装配的)的不匹配造成的。

试试这个:

public class InputDatabaseServiceImpl implements InputDatabaseService { 


    @Autowired 
    public ArticleService articleService; 

    public InputDatabaseServiceImpl(){ 
     //no need of arguments constructor 
    } 

    public int inputArticle(Article article) { 
     articleService.saveArticle(article); 
     return 0; 
    } 

} 

自动装配一个bean,这个bean必须出示公共的默认构造器,你的ArticleServiceImpl类没有。修复程序的形式,这将被重构ArticleServiceImpl:

@Service("articleService") 
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true) 
public class ArticleServiceImpl implements ArticleService { 

    @Autowired 
    private ArticleDao articleDao; 


    public ArticleServiceImpl() { 
    //default constructor required for @Autowired 
    } 

    public ArticleServiceImpl() { 
    } 

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false) 
    public void addArticle(Article article) { 
    articleDao.saveArticle(article); 
    } 
} 
+0

并且缺少组件扫描以读取注释。 –

+1

@Tomas Narros,感谢您的回复,当我在构造函数之前放置'@ Autowired'时,仍然存在相同的异常'NullPointerException'当我在'Public ArticleService articleService之前放置'@ Autowired'时''BeanCreationException无法自动装入字段.. ' – Grzzzzzzzzzzzzz

+0

@格热罗兹,检查编辑的答案。我在属性上包含了'@ Autowired'而不是构造函数,并为后续错误提供了解释和最终修复。 –

1

问题是articleService为空,你应该自动线articleService使用@Autowired万一春未初始化

是管理InputDatabaseServiceImpl对象

否则,你有请求spring初始化articleService使用ClassPathXMLApplicationContext.getBean

0

正如其他人回答说的那样,您的articleService为空,因为尚未注入。在你的appContext.xml中你应该包括:

<!-- Bean creation --> 
<context:component-scan base-package="com.mypackage"/> 
<!-- Also, you can create your beans one by one --> 
<!-- <bean class="com.mypackage.MyBean" /> --> 

<!-- Enables autowired annotations --> 
<context:annotation-config/>