0

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. enter image description here

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: enter image description here

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

  • Assuming your Application class is annotated with @ SpringBootApplication, it already has an @ ComponentScan. (@ SpringBootApplication includes an @ ComponentScan). I would remove the @ ComponentScan from the ApplicationConfig class. – Josh Van de Walle Mar 21 '22 at 21:28
  • Does adding `EnableJdbcRepositories` to the configuration help? Do you have multiple Spring Data modules in the project? – Jens Schauder Mar 23 '22 at 07:40
  • Please post error message and log output as text (formatted as code) not as images. It enables using it for googling, in answers, and in comments. – Jens Schauder Mar 23 '22 at 07:42

0 Answers0