使用Spring Data JPA时,我的存储库无法自动初始化

时间:2012-05-11 02:36:05

标签: spring spring-mvc spring-data-jpa

我正在尝试在探索几个教程之后将spring数据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);

}

我的控制器:

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

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

...

我在这行找到了subjectRepository.findOne(subject),subjectRepository为null, 我的问题类似于post

1 个答案:

答案 0 :(得分:0)

@Autowired注释由语句激活:

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

如果你这样做,它会被初始化