java - 如何修复 "Field ... required a bean of type ... that could not be found"异常 Spring Boot

标签 java spring spring-boot spring-repositories

我正在使用 javabrains 的 spring boot 教程,一切都很清楚,直到将 CrudRepository 放入项目中。您可以在下面找到我的主要类(class):

package pl.springBootStarter.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CourseApiDataApplication
{
    public static void main(String args[])
{
    SpringApplication.run(CourseApiDataApplication.class,args);
}
}

服务类:

package pl.springBootStarter.app.topic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class TopicService
{
    @Autowired
    private TopicRepository topicRepository;

    private List<Topic> topics =  new ArrayList<>(Arrays.asList(
            new Topic("spring","spring framework", "spring framework dectription"),
            new Topic("sprin","spring framework", "spring framework dectription"),
            new Topic("spri","spring framework", "spring framework dectription")));

    public  List<Topic> getAllTopics()
    {
    //    return topics;
    List<Topic> t = new ArrayList<Topic>();
    topicRepository.findAll().forEach(t::add);
    return t;
    }

    public Topic getTopic (String id)
    {
        return   topics.stream().filter( t -> t.getId().equals(id)).findFirst().get();
    }

    public void addTopic(Topic topic) {
        topicRepository.save(topic);
    }

    public void updateTopic(Topic topic, String id)
    {
        topics.set(topics.indexOf(topics.stream().filter(t-> t.getId().equals(id)).findFirst().get()), topic);
    }

    public void deleteTopic(String id)
    {
        topics.remove(topics.stream().filter(t -> t.getId().equals(id)).findFirst().get());
    }
}

Repository接口(interface):

package pl.springBootStarter.app.topic;

import org.springframework.data.repository.CrudRepository;

public interface TopicRepository extends CrudRepository<Topic,String>
{

}

当我运行应用程序时,将 TopicRepository 注入(inject)到 TopicService 类中的 topicRepository 字段时出现问题。我收到以下错误:

Error starting ApplicationContext. To display the conditions report re-       run your application with 'debug' enabled.
2019-05-01 10:33:52.206 ERROR 6972 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field topicRepository in pl.springBootStarter.app.topic.TopicService required a bean of type 'pl.springBootStarter.app.topic.TopicRepository' that could not be found.

The injection point has the following annotations:
-    @org.springframework.beans.factory.annotation.Autowired(required=true)

Spring 不能 Autowiring 的原因是什么?

最佳答案

确保类被 spring 扫描!

(如果这是问题所在,这可能会有所帮助: Intellij Springboot problems on startup ).


可选地,您可能希望将 TopicRepository 注释为 @Repository .

@Repository
public interface TopicRepository extends CrudRepository<Topic,String>
{
}

在此处查看演示代码:https://github.com/lealceldeiro/repository-demo

关于java - 如何修复 "Field ... required a bean of type ... that could not be found"异常 Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55933825/

相关文章:

spring - 我无法在 Springboot-Kotlin 中排除 MongoAutoConfiguration (MongoSocketOpenException)

java - 如何使用 Spring Rest Controller 解决不明确的映射?

java - Spring Integration 窃听直接 MessageChannel 进行测试

java - 不可能使用 Google Map API V2 开发简单的 Android 应用程序

java - Spring 启动和 tomcat

spring - 有没有正确的方法来管理 Spring MVC 上下文中的临时文件?

java - 使用 ActimeMQ 和 Spring Consumer 通过 mqtt 发送和接收图像文件

Java 2D 游戏。除了调用 addnotify 之外,还能做什么?

java - Spark ML 索引器无法用点解析 DataFrame 列名?

java - 等待两个旋转器都选择一个项目