繁体   English   中英

考虑在配置中定义类型为'com.test.project.repositories.TaskRepository'的bean @Repository注解已经存在

[英]Consider defining a bean of type 'com.test.project.repositories.TaskRepository' in your configuration @Repository annotation is already there

我正在使用Redis作为数据存储构建基本的Spring Boot应用程序。 我遵循了所有常规的spring-data-redit教程,并完全按照此处的步骤进行操作。 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能够从@EnableRedisRepositories批注中找到Bean。 您知道左侧的绿色按钮。 但是,当应用程序运行时,它不会。

我正在使用Spring Boot和Spring Boot数据2.1.3.RELEASE

<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;
    }
}

经过数小时无休止的调试,终于可以解决问题。

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

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

希望它可以帮助别人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM