我正在使用 JpaRepository 创建一个简单的 Spring Boot 应用程序,但是当我尝试运行我的应用程序时,它会给出一个错误“NoSuchBeanDefinitionException”。我是 Spring Boot 的新手。
我还尝试使用 @SpringBootApplication
@EnableJpaRepositories("com.ab.repository")
注释来注释我的主类,但是每当我尝试注释时@EnableJpaRepository()
,它都会在 STS 中显示错误
The type org.springframework.data.repository.config.BootstrapMode cannot be resolved. It is indirectly referenced from required .class files.
以前我没有使用这个注释,但是我在一个问题中看到我必须告诉我的班级启用 JPA 存储库,所以我也尝试了这个,但它也不起作用。
我的主要课程
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootMain {
public static void main(String[] args) {
SpringApplication.run(SpringBootMain.class, args);
}
}
控制器类是:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.ab.model.WebServiceModel;
import com.ab.service.WebSrvService;
@RestController
public class WebServiceController {
@Autowired
private WebSrvService webSrvService;
@PostMapping(value = "/save")
public void saveRecord(@RequestBody WebServiceModel webServiceModel) {
webSrvService.saveRecord(webServiceModel);
}
}
服务等级:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ab.model.WebServiceModel;
import com.ab.repository.WebServiceRepository;
@Service
public class WebSrvService {
@Autowired
private WebServiceRepository webServiceRepository;
public void saveRecord(WebServiceModel webServiceModel) {
webServiceRepository.save(webServiceModel);
}
}
存储库接口:
import org.springframework.data.jpa.repository.JpaRepository;
import com.ab.model.WebServiceModel;
public interface WebServiceRepository extends JpaRepository<WebServiceModel, Integer> {
}
我的 pom.xml 文件是:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ab</groupId>
<artifactId>SpringBootTry</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.10.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>3.2.18.RELEASE</version>
</dependency>
</dependencies>
</project>
请纠正我做错了什么,我希望它能够正常运行,但我收到一条错误消息:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ab.repository.WebServiceRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}