spring - com.code.service.BookServiceImpl 中的字段 bookRepository 需要找不到类型为“com.myAppp.code.respository.BookRepository”的 bean
问题描述
当我尝试构建/运行 SpringBoot 应用程序时收到错误消息,如下所示:
Field bookRepository in com.myApp.code.service.BookServiceImpl required a bean of type 'com.myApp.code.respository.BookRepository' that could not be found.
有问题的存储库是:
package com.myApp.code.respository;
import com.myApp.code.model.Book;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface BookRepository extends CrudRepository<Book, Long> {
}
在服务类中,我有以下内容:
package com.myApp.code.service;
import com.myApp.code.model.Book;
import com.myApp.code.respository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookRepository bookRepository;
@Override
public void list() {
//return bookRepository.findAll();
for (Book book : bookRepository.findAll()) {
System.out.println(book.getTitle());
}
}
在有关的控制器中,我有:
@Controller
@RequestMapping(value="book")
public class BookController {
@Autowired
private BookService bookService;
@Autowired
private PersonService personService;
@Autowired
private BookValidator bookValidator;
private final Logger LOG = LoggerFactory.getLogger(getClass().getName());
public BookController() {
}
// Displays the catalogue.
@RequestMapping(value="/catalogue", method=RequestMethod.GET)
public String index(Model model) {
LOG.info(BookController.class.getName() + ".catalogue() method called.");
// Populate catalogue.
bookService.list();
// model.addAttribute("books", books);
// Set view.
return "/catalogue";
}
在 Application.java 文件中我有:
@SpringBootApplication
@EnableJpaRepositories
@ComponentScan(basePackages="com.myApp.code")
public class Application {
private final Logger LOG = LoggerFactory.getLogger(getClass().getName());
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
书类是:
@Entity
@Table(name="BOOK")
public class Book implements Serializable {
// Fields.
@Id
@Column(name="id", unique=true, nullable=false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@Column(name="AUTHOR", nullable=false, length=50)
private String author;
@Column(name="TITLE", nullable=false, length=100)
private String title;
@Column(name="DESCRIPTION", nullable=false, length=500)
private String description;
@Column(name="ONLOAN", nullable=false, length=5)
private String onLoan;
@ManyToOne(fetch=FetchType.EAGER, targetEntity = Person.class)
@JoinColumn(name="Person_Id", nullable=true)
private Person person;
我的 Maven POM 文件是:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.14.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
谁能告诉我为什么我会收到这条消息?BookRepository
毕竟阶级是存在的。
解决方案
Spring boot 只会在同一个包或主类 ( Application
) 的子包中查找存储库、实体和组件。您已经添加了@ComponentScan
指向另一个包的 to ,但您还应该将包添加到@EntityScan
和@EnableJpaRepositories
,例如:
@SpringBootApplication
@EnableJpaRepositories("com.myApp.code") // Add this
@EntityScan("com.myApp.code") // Add this
@ComponentScan(basePackages="com.myApp.code")
public class Application {
// ...
}
JpaRepository中也提到了这一点,在与 componentscan 分开的包中时未实现/注入。
或者,如评论中所述,您可以将主类放在com.myApp.code
自身中。
将您的
Application
课程放在com.myApp.code
包中,而不是子包中。– M. Deinum
通过这样做,您可以删除所有三个注释:
@SpringBootApplication // Other annotations can be removed
public class Application {
// ...
}
推荐阅读
- php - PHP 生成 PDF 错误:FPDF 错误:无法打开图像文件:
- laravel - Laravel 8 Bootstrap 不读取引导程序
- kdb - 如何在 KDB 中进行移动总和?
- django - 使用从同一视图创建的参数的 CreateView 重定向
- swift - SwiftUI:使列表项的剩余区域检测点击
- cloudcannon - cloudcannon - 配置为集合的页面只能作为数据进行编辑
- python - 响应 Python 中的输入
- python - 带有条件python的元素和下一个元素的总和
- node.js - 即使在 Chrome 退出 Node.js 后如何保持会话
- r - 如何使用 ggplot2 绘制具有 3 个独立离散变量和一个连续因变量的数据框?(也欢迎其他套餐)