java – 将MongoTemplate传递给Custom Repository实现

前端之家收集整理的这篇文章主要介绍了java – 将MongoTemplate传递给Custom Repository实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Project配置为使用多个MongoTemplate

Mongo Ref被传递为

  1. @EnableMongoRepositories(basePackages={"com.mypackage.one"},mongoTemplateRef="mongoTemplateOne")

对于com.mypackage.one包中的存储库

  1. @EnableMongoRepositories(basePackages={"com.mypackage.two"},mongoTemplateRef="mongoTemplateTwo")

对于com.mypackage.two包中的存储库

对于标准存储库,它工作正常.但对于我需要自定义行为的场景,我定义了myRepoCustomImpl以及我的自定义行为需求.

问题:我需要访问类似标准存储库的MongoTemplate.

例如
如果MyRepo将MyRepoCustom接口扩展为

  1. @Repository
  2. interface MyRepo extends MongoRepository

MyRepoCustomImpl

  1. @Service
  2. public class MyRepoCustomImpl implements MyRepoCustom{
  3. @Autowired
  4. @Qualifier("mongoTemplateOne")
  5. MongoTemplate mongoTmpl;
  6. @Override
  7. MyEntity myCustomNeedFunc(String arg){
  8. // MyImplemenation goes here
  9. }
  10. }

如果MyRepo在com.mypackage.one包中,mongoTemplateOne将被myRepo使用,所以应该有一些方法让MyRepoCustomImpl知道它也应该使用mongoTemplateOne,每当我在MongoTemplateRef中为MyRepo进行更改时,就像

  1. @EnableMongoRepositories(basePackages={"com.mypackage.one"},mongoTemplateRef="mongoTemplateThree")

现在我需要在MyRepoCustomImpl中对@Qualifier进行更改!
自定义行为有很多回购,因此它变得繁琐乏味.

问题:相反,没有任何方法可以根据它扩展到的回购自动注入或解决使用的MongoTemplate?

最佳答案
MongoRemplate接口不公开MongoTemplate.他们可能会暴露MongoTemplate @Bean的名称,这可以为您的问题提供解决方案.但是,鉴于他们没有,我将在下面提供一个可能符合您需求的示例.

首先,mongoTemplateRef指的是要使用的@Bean的名称,它不指定MongoTemplate的名称.

您需要提供每个MongoTemplate @Bean,然后在@EnableMongoRepositories注释中引用它.

由于您使用的是spring-boot,因此您可以利用MongoDataAutoConfiguration类.请看看它在这里做了什么https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfiguration.java.

最简单的例子就是这样.

package:com.xyz.repo(此实现依赖于MongoDataAutoConfiguration提供的配置)

  1. @Configuration
  2. @EnableMongoRepositories(basePackages={"com.xyz.repo"}) //mongoTemplateRef defaults to mongoTemplate
  3. public class XyzRepoConfiguration {
  4. }
  5. public abstract class BaseRepo {
  6. @Autowired
  7. MongoTemplate mongoTemplate;
  8. }
  9. @Service
  10. public class MyRepoCustomImpl extends BaseRepo implements MyRepoCustom {
  11. @Override
  12. MyEntity myCustomNeedFunc(String arg){
  13. // access to this.mongoTemplate is present
  14. }
  15. }

包:com.abc.repo

  1. @Configuration
  2. @EnableMongoRepositories(basePackages={"com.abc.repo"},mongoTemplateRef=AbcRepConfiguration.TEMPLATE_NAME)
  3. public class AbcRepoConfiguration {
  4. public static final String TEMPLATE_NAME = "mongoTemplateTwo";
  5. @Bean(name="mongoPropertiesTwo")
  6. @ConfigurationProperties(prefix="spring.data.mongodb2")
  7. public MongoProperties mongoProperties() {
  8. return new MongoProperties();
  9. }
  10. @Bean(name="mongoDbFactoryTwo")
  11. public SimpleMongoDbFactory mongoDbFactory(MongoClient mongo,@Qualifier("mongoPropertiesTwo") MongoProperties mongoProperties) throws Exception {
  12. String database = this.mongoProperties.getMongoClientDatabase();
  13. return new SimpleMongoDbFactory(mongo,database);
  14. }
  15. @Bean(name=AbcRepoConfiguration.TEMPLATE_NAME)
  16. public MongoTemplate mongoTemplate(@Qualifier("mongoDbFactoryTwo") MongoDbFactory mongoDbFactory,MongoConverter converter) throws UnknownHostException {
  17. return new MongoTemplate(mongoDbFactory,converter);
  18. }
  19. }
  20. public abstract class BaseRepo {
  21. @Autowired
  22. @Qualifier(AbcRepoConfiguration.TEMPLATE_NAME)
  23. MongoTemplate mongoTemplate;
  24. }
  25. @Service
  26. public class MyRepoCustomImpl extends BaseRepo implements MyRepoCustom {
  27. @Override
  28. MyEntity myCustomNeedFunc(String arg){
  29. // access to this.mongoTemplate is present
  30. }
  31. }

com.xyz.repo将依赖于application.properties中的spring.data.mongodb属性
com.abc.repo将依赖于application.properties中的spring.data.mongodb2属性

我以前没有使用过AbcRepoConfiguration.TEMPLATE_NAME方法,但它是在我的IDE中编译的.

如果您需要任何澄清,请告诉我.

猜你在找的Spring相关文章