Можете да разширите MappingMongoEntityInformation, за да замените getCollectionName(). Операциите на хранилището извикват getCollectionName() при всяка операция. Предполагам, че tenantId ще бъде ThreadLocal
public class TenantThreadLocal extends ThreadLocal<String> {
private final static TenantThreadLocal instance = new TenantThreadLocal();
public static TenantThreadLocal instance() {
return instance;
}
}
И замененият клас с пропуснати конструктори.
public class TenantMappingMongoEntityInformation<T, ID extends java.io.Serializable> extends MappingMongoEntityInformation<T, ID> {
@Override
public String getCollectionName() {
return TenantThreadLocal.instance().get() + super.getCollectionName();
}
}
След това създайте вашето хранилище:
MongoPersistentEntity<YourObject> persistentEntity =
(MongoPersistentEntity<YourObject>)
mongoOperations.getConverter()
.getMappingContext()
.getPersistentEntity(YourObject.class);
MongoEntityInformation<YourObject, ObjectId> mongoEntityInformation =
new MappingMongoEntityInformation<YourObject, ObjectId>(persistentEntity);
CrudRepository<YourObject, ObjectId> repository =
new SimpleMongoRepository<YourObject, ObjectId>(mongoEntityInformation, mongoOperations);