I have been using the Repository pattern without any issues - when I fire up the server it scans the mongo:repository
element in my applicationContext and auto creates a collection for each interface extending Repository.Code:... <mongo:repositories base-package="com.mycompany.registry"/> ...
The way I am getting my DB connection is by extending MongoTemplate and MongoDbFactory so I can get db startup properties from a properties file:Code:public interface AuditLogRepository extends MongoRepository<AuditLogEvent, ObjectId> ... {
This all works fine. However, now I am trying to add a second connection/MongoTemplate to a different DB, but when I do, all the Repository collections are created in both DBs. I created a separate mongoTemplate, MongoDbFactory and matched them to their own mongo:repository element via mongo-template-ref attribute,Code:@Component(RegistryMongoTemplate.NAME) public class RegistryMongoTemplate extends MongoTemplate implements MongoOperations { public static final String NAME = "mongoTemplate"; @Autowired public RegistryMongoTemplate(@Qualifier(RegistryDatabaseFactory.NAME) MongoDbFactory factory) { super(factory); } } @Component(RegistryDatabaseFactory.NAME) public class RegistryDatabaseFactory implements MongoDbFactory { public static final String NAME = "mongoDbFactory"; private DB db = null; @Autowired public RegistryDatabaseFactory( @Qualifier(RegistryDefaultsBootstrapConfig.NAME) RegistryDefaultsBootstrapConfig bootConfig throws RegistryException { db = ... // use the bootstrap props to create db instance. } @Override public DB getDb() throws DataAccessException { return db; } ... }
but I am not having any luck - all repositories are created on both DBs.Code:<mongo:repositories base-package="com.mycompany.other.stuff" mongo-template-ref="mongoTemplateOther"/>


Reply With Quote
