The following configuration is given:
@Configuration
public class AppConfiguration {
@Bean
Mongo mongo() throws UnknownHostException {
return new Mongo("localhost");
}
@Bean(name = "MovieTemplate")
MongoTemplate beagleTemplate(Mongo mongo) {
return new MongoTemplate(mongo, "MovieDatabase");
}
@Bean(name = "AnotherTemplate")
MongoTemplate tmdbTemplate(Mongo mongo) {
return new MongoTemplate(mongo, "AnotherDatabase");
}
}
I need a repository to access movies, which looks kinda like this:
@Repository
public interface MoviesRepository extends
MongoRepository<ProductPages, String> {
... some method declarations to access movies ...
}
Is there an annotation driven way to tell the repository which template to use? If not, what else could be done to solve the problem?
You have to use this annotation on the Configuration class
@EnableMongoRepositories(
basePackages = {"com.yyy.dao.jpa", "com.xxx.dao.jpa"},
mongoTemplateRef = "MovieTemplate"
)
and configure this:
So you need a configuration class for every set of Mongo Dao along with their corresponding MongoTemplate.
NOTE: If you intend to use a different Mongo client for each template, then you have to make sure the appropriate Mongo client bean is passed to the MongoTemplate, such as using a Qualifier, or a different argument name that matches the Mongo's method name with declared @Bean.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With