我正在使用 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);
}
}
Controller 类是:
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);
}
}
存储库接口(interface):
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)}
最佳答案
查看了您在 github 上发布的代码后,我将其拉出,因此您的依赖项出现了问题。您缺少包含 jar 中所有依赖项的 spring 构建插件。
始终使用Spring initlizr当开始一个新项目时,它会自动为您设置所有这些(除非您对 Spring 有良好的经验并且知道自己在做什么)。
完全工作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>
// latest version of spring as of writing 2.1.7
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
</parent>
// set what version you want of java 1.8, 9, 10, 11, 12?
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
// You need the spring plugin to bild a fat jar that includes all
// the dependencies. Without this, no dependencies are included in
// the jar and you get NoSuchBeanDefinitionexception
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
您还需要更新您的应用程序属性
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
否则在启动服务器时您会收到警告。
关于java - 我在使用 JpaRepository 创建简单的 Spring Boot 项目时遇到 NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57740335/