Hello there,
I have created a simple spring boot maven project for my other project. I've created a custom repository interface along with it's implementation based on Spring naming conventions from the docs.
Here is some details about the project structure: I have also pushed the code on Github
Hierarchy:
Controller
@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
public class YourController {
private final YourService yourService;
@GetMapping("/search")
public List<?> search(@RequestParam("fname") String fname) {
return yourService.search(fname);
}
}
Service
@Service
@RequiredArgsConstructor
public class YourService {
private final MyModelRepository myModelRepository;
public List<?> search(String fname) {
return myModelRepository.searchAll(fname, MyModel.class);
}
}
Repository
@Repository
public interface MyModelRepository extends MyCustomModelRepository<MyModel> {
}
Custom Repository and Implementation
public interface MyCustomModelRepository<T> {
List<T> searchAll(String fname, Class<T> entityClass);
}
@Repository
@Transactional(readOnly = true)
public class MyCustomModelRepositoryImpl<T> implements MyCustomModelRepository<T> {
private final EntityManager entityManager;
public MyCustomModelRepositoryImpl(EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public List<T> searchAll(String fname, Class<T> entityClass) {
return Collections.emptyList();
}
}
Error
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.example.demo.service.YourService required a bean of type 'com.example.demo.repository.MyModelRepository' that could not be found.
Action:
Consider defining a bean of type 'com.example.demo.repository.MyModelRepository' in your configuration.
My intentions of creating this is, I'm building my own dependency to use it throughout my other projects So I will extend and use my custom repository from other repositories of different projects.
I have tried multiple solutions from multiple sources But I could seem to find out what the issue is. Please help.
Comment From: philwebb
Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.
Comment From: philwebb
Briefly looking at the code, it looks like you're trying to inject MyModelRepository, but you have no implementation of that interface:
MyCustomModelRepository
^ ^
MyCustomModelRepositoryImpl MyModelRepository
You only have MyCustomModelRepositoryImpl and by extension MyCustomModelRepository.