2012-05-11 37 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"/> 

如果妳這樣做,它會被初始化

相關問題