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

考虑定义COM类型的bean。测验项目仓库。您的配置@Repository注释中的TaskRepository已经存在

任飞鸣
2023-03-14

我正在用Redis作为数据存储构建一个基本的Spring引导应用程序。我已经遵循了所有一般的Spring数据编辑教程,并完全像这里一样做一切。https://github.com/eugenp/tutorials/tree/master/persistence-modules/spring-data-redis

但当我启动应用程序时,我最终会出现这个错误。

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

Description:

Field taskRepository in com.test.project.services.TaskService required a bean of type 'com.test.project.repositories.TaskRepository' 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.test.project.repositories.TaskRepository' in your configuration.


Process finished with exit code 1

我已经找了几个小时的解决方案了。我试过扫描整个包裹。

IntelliJ能够从@EnableRedisRepository注释中定位bean。你知道左边的绿色按钮。但是当应用程序运行时,它不会。

我使用的是spring boot和spring boot数据2.1.3。释放

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

实物

@RedisHash("KnapsackTask")
@Data
@Log
public class KnapsackTask extends Task implements Serializable {

    Problem problem;
    Solution solution;


    public KnapsackTask(Problem problem) {
        this.problem        = problem;
        this.timestamps     = new Timestamps();
        this.timestamps     .setSubmitted((System.currentTimeMillis() / 1000L));
    }

    public KnapsackTask(String taskId) {
        this.taskId = taskId;
    }

    public KnapsackTask submit() {
        log.info(problem.getCapacity().toString());
        problem.getValues().forEach(p -> log.info(p.toString()));
        problem.getWeights().forEach(p -> log.info(p.toString()));
        log.info(this.taskId);
        log.info(this.getStatus().toString());
        return this;
    }

    public KnapsackTask process() {
        return this;
    }

    public KnapsackTask complete() {
        return this;
    }
}

自动连接存储库的服务类

@Service
public class TaskService {

    @Autowired
    TaskRepository taskRepository;

    public Task submitTask(Task task) {
        task.submit();
        task.generateNewTaskId();
        task.setStatus(Task.Status.SUBMITTED);
        taskRepository.save(task);
        return task;
    }

    public Task processTask(Task task) {
        task.process();
        task.setStatus(Task.Status.STARTED);
        taskRepository.save(task);
        return task;
    }

    public Task completeTask(Task task) {
        task.complete();
        task.setStatus(Task.Status.COMPLETED);
        taskRepository.save(task);
        return task;
    }

    public Task getTask(String taskId) {
        return taskRepository.findById(taskId).get();
    }


    public class TaskNotFound extends RuntimeException {

    }
}

存储库

@Repository
public interface TaskRepository extends CrudRepository<Task, String> {

}

Redis配置文件

@Configuration
@EnableRedisRepositories(basePackages = "com.test.project.repositories")
@PropertySource("classpath:application.properties")
public class RedisConfig {

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory();
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(jedisConnectionFactory());
        template.setValueSerializer(new GenericToStringSerializer<>(Object.class));
        return template;
    }
}

共有1个答案

娄德运
2023-03-14

经过数小时无休止的调试,我终于解决了这个问题。

@Repository
public interface TaskRepository extends CrudRepository<Task, String> {}

由于其他原因,任务类被声明为抽象的,因此存储库没有在上下文中注册。

希望它能帮助别人。

 类似资料:
  • 下午好,我正在尝试使用Spring Boot运行SOAP服务,但出现以下错误: 应用程序启动失败 描述: 我知道也许这应该是个愚蠢的错误,但我看不出来 附件MI代码: SpringBootSoapApp.java ClienteServiceImpl.java client.xsd 应用程序.属性 我的项目结构 我试图遵循以下示例:https://www.javaspringclub.com/pu

  • 描述:com.mongotest.demo.seeder中构造函数的参数0需要类型为“com.mongotest.repositories.studentrepository”的bean,但找不到该bean。 pom.xml

  • 我有 ProductController.java ProductService.java ProductServiceImpl.java 这是我的错误: 我有完整日志:https://gist.github.com/donhuvy/b918e20eeeb7cbe3c4be4167d066f7fd 这是我的完整源代码https://github.com/donhuvy/accounting/com

  • 我得到这个错误。在这种情况下可以做什么? 申请无法启动 说明:com.issuemanagement.service.impl.中构造函数的参数0 Issue历史服务Impl需要一个无法找到的类型为“com.issuemanagement.repository.Issue历史存储库”的bean。 操作:考虑定义“com”类型的bean。问题管理。存储库。配置中的IssueHistoryReposi

  • 我试图在Spring Boot中创建一个简单的REST服务。在我使用CrudRepository之前,一切都很好。现在我得到了这个错误- ***应用程序启动失败 描述: 公司中的现场er。Spring靴。io。受雇者EmployeeService需要“company”类型的bean。Spring靴。io。受雇者找不到EmployeeRepo“”。 措施: 考虑定义一个“company”类型的bea

  • 我正在做SpringBoot项目,并遵循一些测试SpringBoot的说明。 当我尝试将mysql DB与项目连接时,服务找不到映射器。 我不知道为什么它找不到映射器... 这是服务代码和 这是映射程序代码 下面的错误是 我将发布我的包裹设置的图片。。。