使用Spring Data JPA时我的存储库无法自动初始化 [英] My repository can't initialize automatically while using Spring Data JPA

查看:232
本文介绍了使用Spring Data JPA时我的存储库无法自动初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在探索了一些教程之后,我试图将spring data jpa添加到我的spring-mvc Web项目中.但是我发现我的存储库无法自动初始化,我在服务类中得到了NullPointerException.请查看我的以下示例代码:

我的存储库:

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.findOne(subject),subjectRepository为空, 我的问题与帖子

解决方案

@Autowired注释由以下语句激活:

<context:component-scan base-package="base.package"/>

如果您这样做,它将被初始化

I am trying to add spring data jpa to my spring-mvc web project after exploring a couple of tutorials. But I found my repository can't initialize automatically, I got NullPointerException in my service class. Please see my following sample code:

My repository:

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);

}

My controller:

@Controller
@RequestMapping("/subject")  
public class VPSubjectController
{
....
@RequestMapping("/{id}.htm")
    public ModelAndView detail(@PathVariable String id)
    {
        ModelAndView mav = new ModelAndView("subject/detail");
        PSubject subject = subjectService.get(id);
....
}
}

My Service:

@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;
    }
.....

My configuration:

<?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"/>

....

I found in this line subjectRepository.findOne(subject) ,subjectRepository is null, My question is similar this post

解决方案

@Autowired annotation is activated by the statement:

<context:component-scan base-package="base.package"/>

If u do that, it would be initialized

这篇关于使用Spring Data JPA时我的存储库无法自动初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆