2 years ago
#318243
Suraev Ivan
Spring can't find repository bean
I'm using spring boot with spring data jdbc and I got trouble with run my app I have StudentService class:
package ru.turnip.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import ru.turnip.model.Student;
import ru.turnip.repository.StudentRepository;
import ru.turnip.utils.student.BusyStudents;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@Service
public class StudentService {
    private final BusyStudents busyStudents;
    private final StudentRepository studentRepository;
    public StudentService(BusyStudents busyStudents, StudentRepository studentRepository) {
        this.busyStudents = busyStudents;
        this.studentRepository = studentRepository;
    }
...
}
And StudentRepository interface:
package ru.turnip.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import ru.turnip.model.Student;
import java.util.UUID;
@Repository
public interface StudentRepository extends CrudRepository<Student, UUID> {  }
So, when I try to run app, I get an eror, and I can't figure out where the problem is.

That my config class:
package ru.turnip;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.relational.core.mapping.event.BeforeSaveEvent;
import org.springframework.scheduling.annotation.EnableScheduling;
import ru.turnip.model.Student;
import java.time.Clock;
import java.util.UUID;
@ComponentScan
@Configuration
@EnableScheduling
public class ApplicationConfig {
    @Bean
    public Clock clock() {
        return Clock.systemDefaultZone();
    }
    @Bean
    public ApplicationListener<BeforeSaveEvent> idGenerator() {
        return event -> {
            var entity = event.getEntity();
            if (entity instanceof Student) {
                ((Student) entity).setId(UUID.randomUUID());
            }
        };
    }
}
And project structure:

I tried to explicitly set package to be scanned, and also moved the repository to the package with the config. Also I tried to annotate with @Autowired field and constructor in StudentService
spring-boot
spring-data-jdbc
0 Answers
Your Answer