BaseRepoistory和CustomRepository部分使用不同的mongoTemplate。我有两个环境:database1,database2,为此,我有两个mongoTemplates,但是当我尝试在自定义存储库中使用时,BaseRepository正在使用database1 mongoTemplate,而CustomRepository正在使用database2 mongoTemplate,即使它们配置为使用database2 mongoTemplate,
我有以下两种配置,一种用于数据库1,另一种用于数据库2:
@Configuration(value = "long-database1")
@EnableMongoRepositories(
basePackageClasses = { Database1Repository.class },
mongoTemplateRef = "database1MongoTemplate"
)
public class Database1MongoDbConfig extends AbstractMongoDbConfig {
public Database1MongoDbConfig() {
super("mongo.database1"); //property name of database
}
@Override
@Bean(name="database1MongoTemplate")
public MongoTemplate mongoTemplate() throws Exception {
return super.mongoTemplate();
}
@Override
@Bean(name="database1MongoDbFactory")
public MongoDbFactory mongoDbFactory() throws Exception {
return super.mongoDbFactory();
}
@Bean
public Database1RepositoryCustom database1RepositoryImpl(@Qualifier("database1MongoTemplate") MongoTemplate database1MongoTemplate) throws Exception{
return new DatabaseRepositoryImpl(database1MongoTemplate);
}
}
@Configuration(value = "long-database2")
@EnableMongoRepositories(
basePackageClasses = { Database2Repository.class },
mongoTemplateRef = "database2MongoTemplate"
)
public class Database2MongoDbConfig extends AbstractMongoDbConfig {
public Database2MongoDbConfig() {
super("mongo.database1"); //property name of database
}
@Override
@Bean(name="database2MongoTemplate")
public MongoTemplate mongoTemplate() throws Exception {
return super.mongoTemplate();
}
@Override
@Bean(name="database2MongoDbFactory")
public MongoDbFactory mongoDbFactory() throws Exception {
return super.mongoDbFactory();
}
@Bean
public Database2RepositoryCustom database2RepositoryImpl(@Qualifier("database2MongoTemplate") MongoTemplate database2MongoTemplate) throws Exception{
return new DatabaseRepositoryImpl(database2MongoTemplate);
}
}
@NoRepositoryBean
public interface DatabaseRepository extends MongoRepository<DisclaimerResponse, String>, DatabaseRepositoryCustom {
}
@Repository
public interface Database1Repository extends DatabaseRepository {
}
@Repository
public interface Database2Repository extends DatabaseRepository {
}
public class DatabaseRepositoryImpl implements DatabaseRepositoryCustom {
private final MongoTemplate mongoTemplate;
@Autowired
public DatabaseRepositoryImpl(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
...
}
当您尝试获取任何存储库的实例时,它可能是数据库1或数据库2的实例,但是BaseRepository部分始终将mongoTemplate用作database1,而CustomRepository使用正确的mongoTemplate(可以是database1或database2 mongo模板,具体取决于配置)。
这就是我的问题,为什么BaseRepository始终使用存储库,为什么它与CustomRepositoryPart不同