简体   繁体   中英

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. I have followed all the general spring-data-redit tutorials and doing everything exactly like here. 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. 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

<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

@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.

Hope it helps someone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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