使用SpringDataJPA时,我的存储库无法自动初始化
在浏览了几篇教程之后,我正在尝试将SpringDataJPA添加到我的SpringMVCWeb项目中。但我发现我的存储库无法自动初始化,我的服务类中出现了NullPointerException。请参阅我的以下示例代码: 我的存储库:使用SpringDataJPA时,我的存储库无法自动初始化,spring,spring-mvc,spring-data-jpa,Spring,Spring Mvc,Spring Data Jpa,在浏览了几篇教程之后,我正在尝试将SpringDataJPA添加到我的SpringMVCWeb项目中。但我发现我的存储库无法自动初始化,我的服务类中出现了NullPointerException。请参阅我的以下示例代码: 我的存储库: public interface SubjectRepository extends JpaRepository<PSubject, String>{ public Page<PSubject> findByType(String titl
public interface SubjectRepository extends JpaRepository<PSubject, String>{
public Page<PSubject> findByType(String title, Pageable pageable);
public Page<PSubject> findByType(String title);
public Page<PSubject> findByMacaddress(String macaddress, Pageable pageable);
public Page<PSubject> findByMacaddress(String macaddress);
public Page<PSubject> findByUri(String uri);
我的服务:
@Service("subjectService")
public class SubjectServiceImpl extends VPService implements SubjectService
{
@Autowired
private SubjectRepository subjectRepository;
......
@Override
@Transactional(propagation=Propagation.REQUIRED, readOnly=true)
public PSubject get(String subject) {
PSubject subObj = subjectRepository.findOne(subject);
return subObj;
}
.....
我的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
....
<jpa:repositories base-package="com.mypacke.repository" repository-impl-postfix="Impl" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager"/>
我在此行中发现subjectRepository.findOnesubject,subjectRepository为null,
我的问题与此类似,此自动连线批注由以下语句激活:
<context:component-scan base-package="base.package"/>
如果这样做,它将被初始化您是否有位于src/main/resources/META-INF下的persistence.xml文档?另外,请尝试删除jpa:respoitories元素的repository impl postfix属性,这可能会导致spring data查找repo的实现,而不是spring data托管接口。是的,我在META-INF文件夹中有一个persistence.xml文件。我已经尝试删除repository impl postfix属性。
<context:component-scan base-package="base.package"/>