简体   繁体   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

I am building a basic spring boot application with Redis as the data store. 我正在使用Redis作为数据存储构建基本的Spring Boot应用程序。 I have followed all the general spring-data-redit tutorials and doing everything exactly like here. 我遵循了所有常规的spring-data-redit教程,并完全按照此处的步骤进行操作。 https://github.com/eugenp/tutorials/tree/master/persistence-modules/spring-data-redis https://github.com/eugenp/tutorials/tree/master/persistence-modules/spring-data-redis

But when I start the application, I end up in this error. 但是,当我启动应用程序时,我最终会遇到此错误。

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

I have been looking for a solution for a few hours now. 我已经寻找了几个小时的解决方案。 I have tried component scanning the whole package. 我试过组件扫描整个程序包。

IntelliJ is able to locate the bean from @EnableRedisRepositories annotation. IntelliJ能够从@EnableRedisRepositories批注中找到Bean。 You know the green button on the left. 您知道左侧的绿色按钮。 But when the application is run, it doesn't. 但是,当应用程序运行时,它不会。

I am using spring boot and spring boot data 2.1.3.RELEASE 我正在使用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>

The actual object 实际对象

@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 Class that is autowiring the repository 自动连接存储库的服务类

@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 资料库

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

}

Redis configuration file 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;
    }
}

Finally after hours of endless debugging I was able to resolve the issue. 经过数小时无休止的调试,终于可以解决问题。

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

The Task class was declared abstract for some other reason and the Repository was not being registered in the context because of that. 由于其他一些原因,Task类被声明为抽象类,因此没有在上下文中注册存储库。

Hope it helps someone else. 希望它可以帮助别人。

暂无
暂无

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

相关问题 考虑在您的配置中定义类型为“com.repository.UserRepository”的 bean - Consider defining a bean of type 'com.repository.UserRepository' in your configuration 春季启动:考虑在配置中定义类型为“ com.repository.services.interfacename”的bean - Spring boot: Consider defining a bean of type 'com.repository.services.interfacename' in your configuration 考虑在您的配置中定义类型为“com.example.conexion.repository.ClienteRepository”的 bean - Consider defining a bean of type 'com.example.conexion.repository.ClienteRepository' in your configuration 考虑在你的配置中定义一个“com.issuemanagement.repository.IssueHistoryRepository”类型的bean - Consider defining a bean of type 'com.issuemanagement.repository.IssueHistoryRepository' in your configuration 考虑在您的配置中定义类型为“ com.face.ImageRepository”的bean? - Consider defining a bean of type 'com.face.ImageRepository' in your configuration? 考虑在你的配置中定义一个 'com.test.simpletest.mapper.PersonMapper' 类型的 bean - Consider defining a bean of type 'com.test.simpletest.mapper.PersonMapper' in your configuration 考虑在您的配置中定义“com.project.airCompanies.repo.AirCompanyRepository”类型的 bean。 带MySQL数据库 - Consider defining a bean of type 'com.project.airCompanies.repo.AirCompanyRepository' in your configuration. With MySQL database 考虑在您的配置中定义“com.fsse2207.project_backend.service.ProductService”类型的 bean - Consider defining a bean of type 'com.fsse2207.project_backend.service.ProductService' in your configuration 考虑在配置问题中定义类型的 bean - Consider defining a bean of type in your configuration issue 考虑在你的配置中定义一个 '...' 类型的 bean - Consider defining a bean of type '...' in your configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM