com.example.demo.bookController.BookController
中的field bookRepository需要找不到类型为com.example.demo.bookRepo.BookRepository
的bean。
注入点具有以下注释:
@org.springframework.beans.factory.annotation.Autowired(required=true)
这是我的Book.java
:
@Document(collection = "Book")
public class Book {
@Id
private int bookId;
private String bookName;
private String bookAuthor;
public int getBookId() {
return bookId;
}
public Book(int bookId, String bookName, String bookAuthor) {
super();
this.bookId = bookId;
this.bookName = bookName;
this.bookAuthor = bookAuthor;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
@Override
public String toString() {
return "Book [bookId=" + bookId + ", bookName=" + bookName + ", bookAuthor=" + bookAuthor + "]";
}
}
BookRepository.java
:
@Repository
public interface BookRepository extends MongoRepository<Book, Integer>{
}
这是我的BookController.java:
@RestController
public class BookController {
@Autowired
private BookRepository bookRepository;
@PostMapping("/addBook")
public String addBook(@RequestBody Book book) {
bookRepository.save(book);
return "the boo has been added. ID : " + book.getBookId();
}
@GetMapping("/findBook/{id}")
public Optional<Book> findBookByID(@PathVariable int id) {
return bookRepository.findById(id);
}
@GetMapping("/findBook")
public List<Book> getAllTheBooks() {
return bookRepository.findAll();
}
@DeleteMapping("/deleteBook")
public String deleteBook(@PathVariable int id) {
bookRepository.deleteById(id);
return "the book with the id : " + id + " is deleted from the DB ";
}
}
pom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>restProject-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>restProject-3</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这是我的文件夹结构:
----com
----example
----demo
RestProject3Application.java
----bookController
BookController.java
----bookRepo
BookRepository.java
----model
Book.java
我尝试在主类中添加@EnableMongoRepositories(basePackageClasses = BookRepository.class)
,但仍然给我同样的错误。
我还尝试在两个类中添加Qualifier批注,但结果相同。
我还尝试在主类中使用@ComponentScan(basePackages = "com.example.demo.bookRepo, com.example.demo.bookController, com.example.demo")
并导致同样的错误。
我无法访问BookRepository bean。有人可以帮我吗?