【发布时间】:2018-06-04 08:53:22
【问题描述】:
当我尝试如下构建/运行 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 类毕竟存在。
【问题讨论】:
-
请添加完整的堆栈跟踪
-
将
Application类放在com.myApp.code包中,而不是子包中。 -
@M Deinum。如果您想将您的评论转换为答案,我会接受。非常感谢。
标签: spring spring-boot spring-data-jpa