【问题标题】:spring boot fails to start-- define a bean of type 'TopicRepository' in configurationspring boot 无法启动——在配置中定义一个“TopicRepository”类型的bean
【发布时间】:2019-10-30 14:48:35
【问题描述】:

我在关注this JavaBrains Spring Boot 教程。

我的项目结构如下:

CourseApiApp.java

@SpringBootApplication
@ComponentScan(basePackages = {
    "com.bloodynacho.rishab.topics"
})
@EntityScan("com.bloodynacho.rishab.topics")
public class CourseApiApp {

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

TopicController.java

@RestController
public class TopicController {

    @Autowired
    private TopicService topicService;

    @RequestMapping(
        value = "/topics"
    )
    public List<Topic> getAllTopcs() {
        return topicService.getAllTopics();
    }
}

TopicService.java

@Service
public class TopicService {

    @Autowired
    private TopicRepository topicRepository;

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

Topic.java

@Entity
public class Topic {
    @Id
    private String id;
    private String name;
    private String description;
}

TopicRepository.java

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

pom.xml

<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-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

我在Topic.java 中使用了lombok @Getter@Getter@AllArgsConstructor,但在阅读here 的其中一个答案后我将其删除。

我读过this1this2this3

我还是明白了

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

Description:
Field topicRepository in com.bloodynacho.rishab.topics.TopicService required a bean of type 'com.bloodynacho.rishab.topics.TopicRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.bloodynacho.rishab.topics.TopicRepository' in your configuration.
Process finished with exit code 1

编辑:我读到this 解释了即使没有实际实现接口,@Autowired 也是如何工作的。我理解解决方案,但我不明白如何解决我的问题。显然,Spring Data 的设置和配置方式存在一些问题(如答案中所述)

【问题讨论】:

  • TopicRepository 不应该是一个接口,而应该是一个类。
  • 你确定吗?如果我做 public class TopicRepository extends CrudRepository {} 我在 IntelliJ 中得到一个编译错误,说 Class TopicRepository 必须被声明为抽象或在 CrudRepository 中实现抽象方法 save(S)。另外,我查看了教程视频,还有它的界面,而不是一个类。
  • 尝试创建一个虚拟方法 save(S) 并使其成为一个类。它应该可以工作。
  • @LHCHIN 会删除编译错误但那是错误的,因为 @SpringBootApplication 仅在使用它的包中查找包,因此删除 @ComponentScan 基本上意味着,Spring 不知道主题和课程包,看这个答案stackoverflow.com/questions/40384056/…
  • 当然,我的意思是如果你将主类的包名设置为com.bloodynacho.rishab,那么你不需要添加这两个注释。而且在我看来,主类的包名应该是其他包的根名。

标签: spring spring-boot spring-data-jpa derby lombok


【解决方案1】:

因为如果您的其他包层次结构位于带有 @SpringBootApplication 注释的主应用程序之下,那么您就会被隐式组件扫描所覆盖。

因此,一个简单的解决方案可以通过以下 2 个步骤完成:

  1. 将主类的包重命名为com.bloodynacho.rishab
    (这就是我的建议,主应用程序的完整包名应该是其他包的根。)
  2. 删除@ComponentScan@EntityScan 注释。
    (虽然@ComponentScan@EntityScan不同,但根据我的经验也可以去掉。)

【讨论】:

    猜你喜欢
    • 2017-03-16
    • 2017-05-30
    • 2021-04-04
    • 2022-09-29
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 2019-12-05
    • 2021-03-04
    相关资源
    最近更新 更多