Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
930 views
in Technique[技术] by (71.8m points)

java - spring data jpa repository inject fails using @Autowired in spring boot

spring boot entry class

package com.test;

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

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

Controller class

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

    }

}

Entity

package com.test.assetmanagementdigital.model;

import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name="ShopDetails")
public class Shop {

    private String shopName;
    private Address address;

    public String getShopName() {
        return shopName;
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }


}

data jpa repository interface

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

    }

Service class

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

    @Autowired
    private ShopRepository shopRepository;

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

}

gradle file

 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')
    }

I am getting following error

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.

if i remove the @Autowired annotation from ShopRepository then it will throw `NullPointerException

I have tried @EnableJpaRepositories("com.test.assetmanagementdigital.repository") here I get

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your Spring configuration is not correct.

The spring-boot-starter-data-jpa already provides the hibernate-core dependency. While you declare it with a specific version :

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

You have not to declare it a second time as your specified version may be different and not compatible with the version provided by the starter.
And according to your error, it seems be the case as the javax.persistence.PersistenceContext.synchronization() method is not found at runtime.

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

Just remove the hibernate-core dependency and it should work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...