当前位置: 首页 > 面试题库 >

在Spring Boot中使用@ Autowired,Spring数据JPA存储库注入失败

韩玉石
2023-03-14
问题内容

Spring Boot入门级

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

如果我@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配置不正确。

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

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

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

合并bean定义的后处理失败;嵌套的异常是 java.lang.NoSuchMethodError
javax.persistence.PersistenceContext.synchronization()Ljavax/persistence/SynchronizationType;

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



 类似资料:
  • 我突然想到,可以用三个基于泛型的类来替换每个对象类型的多个类,从而节省大量的样板代码。我不是很清楚该怎么做,事实上这是不是一个好主意?

  • 它是否将其存储在缓存中?我有一个应用程序,但应用程序中没有任何地方。属性是提到的db详细信息。我可以通过邮递员存储数据和查询它。

  • 我显然遗漏了一些东西。我正在制作一个简单的应用程序,其中包含并面临以下错误: 我的代码: 应用程序: pom.xml 控制器: 人事服务: 个人服务: PersonRepository(此存储库不能自动连接): 已经在网上搜索了。我什么都没找到。有什么想法吗?

  • 我有一个springboot项目,为了执行请求,我正在连接到我的mysql数据库。我有一个实体ExportBatch: 我在ExportBatchRepository中定义了一个新方法: 在我的控制器里,我是这样做的: 当我调用“/GetLastBatchsProblemes”时会遇到这个异常:java.lang.ClassCastException:[Ljava.lang.Object;不能强制

  • 是否有一种方法可以使通用Spring数据JPA存储库正确处理类似的方法?例如只返回狗,而不返回所有动物?或者至少,最好的变通方法是什么? 它的工作几乎完美,保存每一个动物在自己的桌子上,等等。唯一的问题是:同时返回水豚和狗。这个答案解释说: 这只有在域类使用单表继承时才起作用。我们在引导时能得到的关于domain类的唯一信息是它将是Product对象。因此,对于像findAll()甚至findBy

  • 我有一个micronaut数据应用程序,对于复制操作,我需要使用相同的JPA存储库来访问多个数据源。我想将它们注入不同的变量,例如和。数据源在带有默认名称和目标名称。如果我用声明一个存储库变量,它将被初始化以访问默认数据源。 问题是,如何声明注入的存储库变量,以便它访问目标数据源?我可以声明或的目标注入,如下所示。但这两种注释都不会对存储库变量或参数产生影响。