2012-12-05 108 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/>