java 在spring boot中使用@Autowired注入spring数据jpa存储库失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44413706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spring data jpa repository inject fails using @Autowired in spring boot
提问by kiran rathod
spring boot entry class
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 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
数据 jpa 存储库接口
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
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')
}
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
如果我@Autowired
从中删除注释,ShopRepository
它将抛出 `NullPointerException
I have tried @EnableJpaRepositories("com.test.assetmanagementdigital.repository")
here I get
我在@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;
回答by davidxxx
Your Spring configuration is not correct.
您的 Spring 配置不正确。
The spring-boot-starter-data-jpa
already provides the hibernate-core
dependency. While you declare it with a specific version :
在spring-boot-starter-data-jpa
已经提供了hibernate-core
依赖。当您使用特定版本声明它时:
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.
您不必再次声明它,因为您指定的版本可能与启动器提供的版本不同且不兼容。
根据您的错误,似乎是这种情况,因为javax.persistence.PersistenceContext.synchronization()
在运行时找不到该方法。
Post-processing of merged bean definition failed; nested exception is
java.lang.NoSuchMethodError
:javax.persistence.PersistenceContext.synchronization()Ljavax/persistence/SynchronizationType
;
合并bean定义后处理失败;嵌套的异常是
java.lang.NoSuchMethodError
:javax.persistence.PersistenceContext.synchronization()Ljavax/persistence/SynchronizationType
;
Just remove the hibernate-core
dependency and it should work.
只需删除hibernate-core
依赖项,它应该可以工作。
回答by Darshan Mehta
You need to provide the package name to Spring to scan the repositories from, using @EnableJpaRepositories
annnotation, e.g.:
您需要使用@EnableJpaRepositories
注释向 Spring 提供包名称以从中扫描存储库,例如:
@SpringBootApplication
@EnableJpaRepositories("com.test.assetmanagementdigital.repository")
public class AssetManagementDigital2Application
Here'sthe documentation.
这是文档。