Home > database >  Parameter 0 of constructor in service required a bean of type repository that could not be found
Parameter 0 of constructor in service required a bean of type repository that could not be found

Time:09-04

I am working on dynamic searching and I would like to add specyfikacja-arg-resolver to my rest api. I am using this: https://github.com/tkaczmarzyk/specification-arg-resolver#enabling-spec-annotations-in-your-spring-app

My api is splited for Controller -> Service -> Repository. Service looks like:

@Service
@RequiredArgsConstructor
public class DriverService {
    private final DriverEntityRepository driverEntityRepository;

    public Set<DriverEntity> getAllWithSpec(Specification<DriverEntity> customerSpec, Pageable pageable) {
        return driverEntityRepository.findAll(customerSpec, pageable);
    }
}

Controller:

@GetMapping("/test2")
public Set<DriverDTO> test3(
        @And({
                @Spec(path = "name", spec = Equal.class),
                @Spec(path = "surname", spec = Equal.class)
        }) Specification<DriverEntity> customerSpec,
        Pageable pageable) {
    Set<DriverEntity> driverEntities = driverService.getAllWithSpec(customerSpec, pageable);
    return modelMapperService.mapSetToSetOfEnteredClass(driverEntities, DriverDTO.class);
}

and I added this as in specification:

@Configuration
@EnableJpaRepositories
public class MyConfig implements WebMvcConfigurer {

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new SpecificationArgumentResolver());
        }
    }

and after start of my app I am getting:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.example.service.DriverService required a bean of type 'com.example.repository.DriverEntityRepository' that could not be found.


Action:

Consider defining a bean of type 'com.example.repository.DriverEntityRepository' in your configuration.


Process finished with exit code 1

but when I put "MyConfig" into comments I am getting error below

java.lang.IllegalStateException: No primary or single unique constructor found for interface org.springframework.data.jpa.domain.Specification

CodePudding user response:

Ok, so when I removed

@EnableJpaRepositories

from MyConfig Class everything works and searching works correctly with request parameteres :)

  • Related