Crea la tua interfaccia di repository con @NoRepositoryBean
, lo collegheremo noi stessi:
@NoRepositoryBean
public interface ModelMongoRepository extends MongoRepository<Model, String> {
}
Quindi, in un @Configuration
class, istanziare i 2 bean del repository usando MongoRepositoryFactoryBean
. Entrambi i repository restituiranno la stessa interfaccia Spring Data Repository, ma assegneremo loro MongoOperations
differenti (es:dettagli del database):
@Configuration
@EnableMongoRepositories
public class MongoConfiguration {
@Bean
@Qualifier("one")
public ModelMongoRepository modelMongoRepositoryOne() throws DataAccessException, Exception {
MongoRepositoryFactoryBean<ModelMongoRepository, Model, String> myFactory = new MongoRepositoryFactoryBean<ModelMongoRepository, Model, String>();
myFactory.setRepositoryInterface(ModelMongoRepository.class);
myFactory.setMongoOperations(createMongoOperations("hostname1", 21979, "dbName1", "username1", "password1"));
myFactory.afterPropertiesSet();
return myFactory.getObject();
}
@Bean
@Qualifier("two")
public ModelMongoRepository modelMongoRepositoryTwo() throws DataAccessException, Exception {
MongoRepositoryFactoryBean<ModelMongoRepository, Model, String> myFactory = new MongoRepositoryFactoryBean<ModelMongoRepository, Model, String>();
myFactory.setRepositoryInterface(ModelMongoRepository.class);
myFactory.setMongoOperations(createMongoOperations("hostname2", 21990, "dbName2", "username2", "password2"));
myFactory.afterPropertiesSet();
return myFactory.getObject();
}
private MongoOperations createMongoOperations(String hostname, int port, String dbName, String user, String pwd) throws DataAccessException, Exception {
MongoCredential mongoCredentials = MongoCredential.createScramSha1Credential(user, dbName, pwd.toCharArray());
MongoClient mongoClient = new MongoClient(new ServerAddress(hostname, port), Arrays.asList(mongoCredentials));
Mongo mongo = new SimpleMongoDbFactory(mongoClient, dbName).getDb().getMongo();
return new MongoTemplate(mongo, dbName);
}
//or this one if you have a connection string
private MongoOperations createMongoOperations(String dbConnection) throws DataAccessException, Exception {
MongoClientURI mongoClientURI = new MongoClientURI(dbConnection);
MongoClient mongoClient = new MongoClient(mongoClientURI);
Mongo mongo = new SimpleMongoDbFactory(mongoClient, mongoClientURI.getDatabase()).getDb().getMongo();
return new MongoTemplate(mongo, mongoClientURI.getDatabase());
}
}
Ora hai 2 bean con @Qualifier
distinto nomi, ciascuno configurato per database diversi e utilizzando lo stesso modello.
Puoi iniettarli usando @Qualifier
:
@Autowired
@Qualifier("one")
private ModelMongoRepository mongoRepositoryOne;
@Autowired
@Qualifier("two")
private ModelMongoRepository mongoRepositoryTwo;
Per semplicità, ho codificato a fondo i valori nella classe di configurazione, ma puoi iniettarli dalle proprietà in application.properties/yml.
Ecco la modifica se si desidera creare un'implementazione personalizzata senza perdere i vantaggi dei repository dell'interfaccia dati di primavera. le specifiche dicono questo:
Crea una nuova interfaccia, che tecnicamente non ha nulla a che fare con i dati di primavera, buona vecchia interfaccia:
public interface CustomMethodsRepository {
public void getById(Model model){
}
Fai in modo che l'interfaccia del tuo repository estenda questa nuova interfaccia:
@NoRepositoryBean
public interface ModelMongoRepository extends MongoRepository<Model, String>, CustomMethodsRepository {
}
Quindi, crea la tua classe di implementazione, che solo implementa la tua interfaccia non Spring-data:
public class ModelMongoRepositoryImpl implements CustomModelMongoRepository {
private MongoOperations mongoOperations;
public ModelMongoRepositoryImpl(MongoOperations mongoOperations) {
this.mongoOperations = mongoOperations;
}
public void getById(Model model){
System.out.println("test");
}
}
Modifica la configurazione Java per aggiungere myFactory.setCustomImplementation(new ModelMongoRepositoryImpl());
:
@Bean
@Qualifier("one")
public ModelMongoRepository modelMongoRepositoryOne() throws DataAccessException, Exception {
MongoRepositoryFactoryBean<ModelMongoRepository, Model, String> myFactory = new MongoRepositoryFactoryBean<ModelMongoRepository, Model, String>();
MongoOperations mongoOperations = createMongoOperations("hostname1", 21979, "dbName1", "usdername1", "password1");
myFactory.setCustomImplementation(new ModelMongoRepositoryImpl(mongoOperations));
myFactory.setRepositoryInterface(ModelMongoRepository.class);
myFactory.setMongoOperations(mongoOperations);
myFactory.afterPropertiesSet();
return myFactory.getObject();
}
Se non stavi cablando il repository manualmente tramite la configurazione Java, questa implementazione DEVE essere denominata ModelMongoRepositoryImpl
per abbinare l'interfaccia ModelMongoRepository +"Impl"
. E verrebbe gestito automaticamente entro la primavera.