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

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

问题描述

在浏览了几个教程后,我正在尝试将 spring 数据 jpa 添加到我的 spring-mvc web 项目中.但是我发现我的存储库无法自动初始化,我的服务类中出现 NullPointerException.请参阅我的以下示例代码:

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:

我的仓库:

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

}

我的控制器:<代码>

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

我的服务:

@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 为空,我的问题类似于这个post

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

推荐答案

@Autowired注解被语句激活:

@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天全站免登陆