7

次の構成が与えられます。

@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");
  }
}

映画にアクセスするには、次のようなリポジトリが必要です。

@Repository
public interface MoviesRepository extends
    MongoRepository<ProductPages, String> {

    ... some method declarations to access movies ...
}

どのテンプレートを使用するかをリポジトリに伝えるアノテーション主導の方法はありますか? そうでない場合、問題を解決するために他に何ができますか?

4

2 に答える 2

2

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:

  1. enumerate all the packages/classes to scan to find mongo dao that will be included on this configuration
  2. specify the MongoTemplate(bean name) that will be used by the Mongo Dao scanned by this configuration

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.

于 2015-09-19T09:17:11.873 に答える