2012-05-11 27 views
1

我想探讨的几个教程后,春天JPA的数据添加到我的春天MVC Web项目我的仓库不能自动初始化。但是我发现我的仓库不能自动初始化,我的服务类中有NullPointerException异常。请参阅我下面的示例代码:同时使用Spring数据JPA

我的资料库:

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

我的服务:

​​

我的配置:

<?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(主题),subjectRepository为空发现, 我的问题是类似这种post

+0

你有一种坚持ence.xml文件位于src/main/resources/META-INF下?另外,试着摆脱jpa:respoitories元素的repository-impl-postfix属性,可能会导致spring-data查找您的repo的实现,而不是弹簧数据管理的接口。 – dardo

+0

是的,我在META-INF文件夹中有一个persistence.xml文件。我已经尝试删除repository-impl-postfix属性。 – Tom

回答

0

@Autowired注解被语句激活:

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

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