所以我在测试Spring JPA时遇到了这个问题,这是我的代码: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;
}
应用
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
类时,我都会在NullPointerExpcetion
行中找到articleService.saveArticle(article);
我知道这是要解决的最简单的例外,但是一段时间以来,我一直在苦苦挣扎,我需要帮助。任何人都可以给我一个提示,我想念什么?
最佳答案
发生的情况是未初始化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);
}
}