gpt4 book ai didi

java - spring data jpa repository inject在spring boot中使用@Autowired注入(inject)失败

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:03:33 26 4
gpt4 key购买 nike

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

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

}

}

实体

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存储库接口(interface)

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;

最佳答案

您的 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 依赖项,它就可以正常工作。

关于java - spring data jpa repository inject在spring boot中使用@Autowired注入(inject)失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44413706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com