春季啓動項類春數據倉庫JPA失敗注入春天開機使用@Autowired
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);
}
}
實體
個數據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.
如果我刪除從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;
我相信'@ Transactional'註釋缺失 – soorapadman
您的依賴關係是錯誤的(這是最後一個堆棧跟蹤/錯誤告訴你的)。添加你的pom文件或gradle文件。 –
添加了gradle文件 –