【问题标题】:Field bookRepository in com.code.service.BookServiceImpl required a bean of type 'com.myAppp.code.respository.BookRepository' that could not be foundcom.code.service.BookServiceImpl 中的字段 bookRepository 需要找不到类型为“com.myAppp.code.respository.BookRepository”的 bean
【发布时间】: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


【解决方案1】:

Spring boot 只会在同一包或主类的子包 (Application) 中查找存储库、实体和组件。您已经添加了@ComponentScan 以指向另一个包,但您还应该将包添加到@EntityScan@EnableJpaRepositories,例如:

@SpringBootApplication
@EnableJpaRepositories("com.myApp.code") // Add this
@EntityScan("com.myApp.code") // Add this 
@ComponentScan(basePackages="com.myApp.code")
public class Application {
    // ...
}

JpaRepository not implemented/injected when in separate package from componentscan 中也提到了这一点。

或者,as mentioned in the comments,您可以将主类放在com.myApp.code 本身中。

Application 类放在com.myApp.code 包中,而不是子包中。 – M. Deinum

通过这样做,您可以删除所有三个注释:

@SpringBootApplication // Other annotations can be removed
public class Application {
    // ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    • 2019-07-20
    • 2020-06-16
    • 1970-01-01
    相关资源
    最近更新 更多