2017-06-07 119 views
2

春季啓動項類春數據倉庫JPA失敗注入春天開機使用@Autowired

package com.test; 

@SpringBootApplication(exclude={ 
DataSourceAutoConfiguration.class, 
DataSourceTransactionManagerAutoConfiguration.class}) 
public class AssetManagementDigital2Application { 

    public static void main(String[] args) { 
     SpringApplication.run(AssetManagementDigital2Application.class, args); 
    } 
} 

Controller類

package com.test.assetmanagementdigital.controller; 

@RestController 

public class ShopController { 

    @Autowired 
    private ShopServiceImpl shopServiceImpl; 

    @RequestMapping(value="/shops",method=RequestMethod.POST) 
    public void shopDetails(Shop shop){ 
     shopServiceImpl.addShopDetails(shop); 

    } 

} 

實體

​​

數據JPA倉庫接口

package com.test.assetmanagementdigital.repository; 
    @Repository 
    public interface ShopRepository extends CrudRepository<Shop,Long>{ 

    } 

服務類

package com.test.assetmanagementdigital.service; 
@Service 
public class ShopServiceImpl { 

    @Autowired 
    private ShopRepository shopRepository; 

    public void addShopDetails(Shop shop) { 
     shopRepository.save(shop); 
    } 

} 

gradle這個文件

buildscript { 
     ext { 
      springBootVersion = '2.0.0.BUILD-SNAPSHOT' 
     } 
     repositories { 
      mavenCentral() 
      maven { url "https://repo.spring.io/snapshot" } 
      maven { url "https://repo.spring.io/milestone" } 
     } 
     dependencies { 
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
     } 
    } 

    apply plugin: 'java' 
    apply plugin: 'eclipse-wtp' 
    apply plugin: 'org.springframework.boot' 
    apply plugin: 'io.spring.dependency-management' 
    apply plugin: 'war' 

    version = '0.0.1-SNAPSHOT' 
    sourceCompatibility = 1.8 

    repositories { 
     mavenCentral() 
     maven { url "https://repo.spring.io/snapshot" } 
     maven { url "https://repo.spring.io/milestone" } 
    } 

    configurations { 
     providedRuntime 
    } 

    dependencies { 
     compile('org.springframework.boot:spring-boot-starter-data-jpa') 
     compile('org.springframework.boot:spring-boot-starter-web') 
     compile("com.h2database:h2") 
     compile group: 'org.hibernate', name: 'hibernate-core', version: '4.2.2.Final' 
     providedRuntime('org.springframework.boot:spring-boot-starter-tomcat') 
     testCompile('org.springframework.boot:spring-boot-starter-test') 
    } 

我收到以下錯誤

Description: 

Field shopRepository in com.test.assetmanagementdigital.service.ShopServiceImpl required a bean of type 'com.test.assetmanagementdigital.repository.ShopRepository' that could not be found. 

Action: 

Consider defining a bean of type 'com.test.assetmanagementdigital.repository.ShopRepository' in your configuration. 

如果我刪除從ShopRepository@Autowired註釋,然後它會拋出`NullPointerException異常

我在這裏試圖@EnableJpaRepositories("com.test.assetmanagementdigital.repository")我得到

org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'shopController': Unsatisfied dependency expressed through field 'shopServiceImpl'; nested exception is 
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'shopServiceImpl': Unsatisfied dependency expressed through field 'shopRepository'; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shopRepository': Post-processing of merged bean definition failed; nested exception is 
java.lang.NoSuchMethodError: javax.persistence.PersistenceContext.synchronization()Ljavax/persistence/SynchronizationType; 
+0

我相信'@ Transactional'註釋缺失 – soorapadman

+1

您的依賴關係是錯誤的(這是最後一個堆棧跟蹤/錯誤告訴你的)。添加你的pom文件或gradle文件。 –

+0

添加了gradle文件 –

回答

1

Spring配置不正確。

spring-boot-starter-data-jpa已經提供了hibernate-core依賴關係。當你與一個特定版本聲明它:

compile group: 'org.hibernate', name: 'hibernate-core', version: '4.2.2.Final' 

你還沒有宣佈它爲你指定的版本可能會有所不同,並與起動機提供的版本不兼容第二次。
根據你的錯誤,似乎是這樣的,因爲在運行時沒有找到javax.persistence.PersistenceContext.synchronization()方法。

Post-processing of merged bean definition failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.PersistenceContext.synchronization()Ljavax/persistence/SynchronizationType;

只需卸下hibernate-core依賴,它應該工作。

+1

你不需要'@ EnableJpaRepositories'在春季啓動應用程序。 Spring Boot會自動檢測Spring Data JPA(及其他)併爲其配置適當的「@ Enable * Repositories」。 –

+0

@M。 Deinum你是完全正確的。我修改了我的答案。感謝您確保優質的內容。 – davidxxx

-1

您需要提供軟件包的名稱,以春,從掃描庫,使用@EnableJpaRepositories annnotation,例如:

@SpringBootApplication 
@EnableJpaRepositories("com.test.assetmanagementdigital.repository") 
public class AssetManagementDigital2Application 

Here's的文檔。

+0

我有上面的編輯問題,請檢查n也試過這個之前 –

+1

你不需要'@ EnableJpaRepositories'在彈簧啓動應用程序。 Spring Boot會自動檢測Spring Data JPA(及其他)併爲其配置適當的「@ Enable * Repositories」。 –

+0

@kiranrathod有沒有任何理由爲什麼'DataSourceAutoConfiguration'被禁用? –