当前位置: 首页 > 知识库问答 >
问题:

api.database.basic.apidatabase.models.topicservice中的字段topicRepository需要一个类型为

羊舌勇
2023-03-14

我正在从这里的教程中学习Java spring Boot

我一直得到“api.database.basic.apidatabase.models.TopicService中的字段topicRepository需要一个类型为'api.database.basic.apidatabase.interface.topicRepository'的bean,但找不到。”

我读到这个错误可能是因为componentScan由于类的注释不佳或包布局不佳而失败。

    package api.dataBase.basic.apiDatabase;

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

@SpringBootApplication ()
public class ApiDatabaseApplication {

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

}
package api.dataBase.basic.apiDatabase.Models;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Topic {
    @Id
    private String id;
    private String name;
    private String description;
    //private Address address;

    public Topic(final String id, final String name, final String description) {
        this.id = id;
        this.name = name;
        this.description = description;
    }

    public Topic() {
    }

@Id
public String getId() {
        return id;
    }

    public void setId(final String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(final String description) {
        this.description = description;
    }
}
package api.dataBase.basic.apiDatabase.Interface;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;


import api.dataBase.basic.apiDatabase.Models.Topic;

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


}
package api.dataBase.basic.apiDatabase.Models;

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

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

import api.dataBase.basic.apiDatabase.Interface.TopicRepository;


@Service
public class TopicService {

    @Autowired
    private TopicRepository topicRepository;

    private List<Topic> Oldtopics = new ArrayList<>(Arrays.asList(
            new Topic("1001", "test", "hello 1"),
            new Topic("1002", "hello", "hello 2")
    ));

    public List<Topic> getTopics() {

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

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


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

    public void updateTopic(final Topic topic, final String id) {
        for (int i = 0; i < Oldtopics.size(); i++) {
            if (Oldtopics.get(i).getId().equals(id)) {
                Oldtopics.set(i, topic);
                return;
            }
        }
    }

    public void deleteTopic(String id) {
        Oldtopics.removeIf(t -> t.getId().equals(id));
    }

    public void deleteTopic(final String[] id) {
        for (int i = 0; i < Oldtopics.size(); i++) {
            for (String ids : id) {
                if (Oldtopics.get(i).getId().equals(ids)) {
                    Oldtopics.remove(i);
                }
            }
        }
    }
}
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-11-21 19:18:05.350 ERROR 4192 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Field topicRepository in api.dataBase.basic.apiDatabase.Models.TopicService required a bean of type 'api.dataBase.basic.apiDatabase.Interface.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 'api.dataBase.basic.apiDatabase.Interface.TopicRepository' in your configuration.
    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>api.dataBase.basic</groupId>
    <artifactId>apiDatabase</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>apiDatabase</name>
    <description>API project with JPA database</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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>
            </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

共有1个答案

吕霄
2023-03-14

这种情况可能会导致配置丢失,从而导致组件扫描混乱。

我建议您将包含以下内容的“config.java”添加到源根(src/main/java)

@Configuration
@EnableJpaRepositories
class Config {
}
 类似资料:
  • 我在尝试运行我的应用程序时遇到以下错误: com.alon.service.EmployeeServiceImpl中得字段edao需要类型为“com.alon.Repository.EmployeeRepository”得bean,但找不到该bean. 注入点有以下注释: null EmployeeServiceImpl: 我的应用程序:

  • 我是一名spring boot学习者,所以我一直在尝试创建一些基本的spring boot应用程序。我试图运行开发的应用程序时出错。 我的错误是[[https://i.stack.imgur.com/oyQDi.png][1]][1] java: ItemDetails.java:[软件包名称:io.ajithan.springbootstarter.model] ItemResponse.jav

  • 结构没有问题。spring boot可以扫描UserMapper,但不能扫描UserService。我试着给我的UserService@Mapper组件,然后它就可以被扫描了。但我不知道如何使用其他方法进行扫描。我尝试了@服务,但不起作用。

  • 我是spring boot的新手,一直在尝试制作简单的REST web应用程序。我遵循了不同指南中所示的步骤,但当我尝试运行应用程序时,仍然会遇到同样的问题。 这是我每次尝试运行代码时看到的输出 项目类别:

  • 在spring Boot1.5.9上创建spring boot应用程序时,我收到以下错误消息。有谁能帮忙吗?提前道谢。 Oracle 11g数据库。 请找到我在pom.xml中使用的以下类: Java代码-应用程序: Java代码存储库: Java代码服务:

  • 我是Spring新来的,所以我有一个这样的问题:描述: 字段templateEngine在com.diet4you。拉普科·叶卡捷琳娜。MailComponent需要找不到类型org.thymeleaf.TemplateEngine的bean。 注入点具有以下注释:-@org。springframework。豆。工厂注释。自动连线(必需=真) 已找到以下候选项,但无法注入:-“ThymeleafA