首页 > 解决方案 > 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毕竟阶级是存在的。

标签: springspring-bootspring-data-jpa

解决方案


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 {
    // ...
}

推荐阅读