Issue
I Have two repository interfaces that connect MongoDB and CouchBase :
public interface UserRepositoryMongo extends MongoRepository<User, Long> {
}
public interface UserRepositoryCouch extends CouchbasePagingAndSortingRepository<User, Long> {
}
Is there a way to interchangeably @Autowire
these repositories into UserService
on condition?
The condition will be inside property file : application.properties
.
**Note :**
*These repositories can have custom methods too.*
Solution
Another attempt to solve this problem.
Based on the property, the autowired customRepository will be an instance of SimpleMongoRepository or SimpleCouchRepository.
public interface CustomRepository extends CrudRepository<User, Long> {
User findByLastName(String lastName); //custom methods
}
@Qualifier("specificRepo")
@ConditionalOnProperty("mongo.url")
public interface UserRepositoryMongo extends MongoRepository<User, Long>, CustomRepository{
}
@Qualifier("specificRepo")
@ConditionalOnProperty("couch.url")
public interface UserRepositoryCouch extends CouchbasePagingAndSortingRepository<User, Long>, CustomRepository{
}
public class UserService {
@Autowired
@Qualifier("specificRepo")
private CustomRepository repository;
}
Answered By - Kumar V
Answer Checked By - Marie Seifert (JavaFixing Admin)