I wanted to know if i could use the same spring crudRepository and Services interface with multiple classes by attributing a generic type to both of them in order to avoid rewriting the same thing over and over.
i'll try to explain this idea better with an example;
imagine if we had two different classes "Dog" and "Cat"
public class Dog extends Serializable{ //attributes here... }
public class Cat extends Serializable{ //attributes here... }
the service interface will more likely look like this :
public interface Services<T> {
List<T> getAllRecords();
T getRecordById(int id);
T insertRecord(T animal);
// etc
}
and the repository will be like this :
public interface GenericRepository<T> extends CrudRepository<T,Serializable>{ //....}
then we'll be able to implement the services such as this :
@Service
public class CatServiceImpl implements Services<Cat>
{
@Autowired
GenericRepository<Cat> repositoryCat;
//...
}
@Service
public class DogServiceImpl implements Services<Dog>
{
@Autowired
GenericRepository<Dog> repositoryDog;
//...
}
and so on.. the problem is in the controller how can the @AutoWired annotation differentiate between the implementations? Any suggestions?