程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在Spring Boot中使用@ Autowired,Spring Data JPA存储库注入失败大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在Spring Boot中使用@ Autowired,Spring Data JPA存储库注入失败?

开发过程中遇到在Spring Boot中使用@ Autowired,Spring Data JPA存储库注入失败的问题如何解决?下面主要结合日常开发的经验,给出你关于在Spring Boot中使用@ Autowired,Spring Data JPA存储库注入失败的解决方法建议,希望对你解决在Spring Boot中使用@ Autowired,Spring Data JPA存储库注入失败有所启发或帮助;

您的Spring配置不正确。

在@H_403_3@spring-boot-starter-data-jpa已经提供了@H_403_3@hibernate-core依赖。当您使用特定版本声明它时:

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

您不必再次声明它,因为您指定的版本可能不同,并且与启动程序提供的版本不兼容。 并且根据您的错误,似乎是这种情况,因为@H_403_3@javax.persistence.PersistenceContext.synchronization()在运行时找不到该方法。

只需删除@H_403_3@hibernate-core依赖项,它就可以工作。

解决方法

弹簧靴入门级

package com.test;

@SpringBootApplication(exclude={
DatasourceAutoConfiguration.class,DatasourcetransactionManagerAutoConfiguration.class})
public class AssetManagementDigital2Application {

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

控制器类

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

    }

}

实体

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


}

数据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')
        @R_450_8932@ompile('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.

如果我@Autowired从中删除注释,ShopRepository它将抛出`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;

大佬总结

以上是大佬教程为你收集整理的在Spring Boot中使用@ Autowired,Spring Data JPA存储库注入失败全部内容,希望文章能够帮你解决在Spring Boot中使用@ Autowired,Spring Data JPA存储库注入失败所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。