使用Spring数据支持Mongo的多个数据源

时间:2018-03-02 13:52:32

标签: mongodb spring-data

我正在尝试使用spring数据配置两个不同的mongo数据源。根据我的要求,我需要在运行时动态更改我对基本包的扫描。用户可以通过提供数据源和实体映射,通过属性文件为特定实体选择数据源。 例如:entity1:dataSource1,entity2:dataSource2。根据此映射,我需要打包名称,并且需要在运行时替换@EnableMongoRepositories(basePackages =" xxx.xxx.xxxx")。我使用Xml配置和java配置都累了很多。但是我找不到可能的方法。如果有问题,可以为问题提供解决方案。我正在粘贴实体类,存储库和配置XML和Java配置

package com.account.entity;
import java.io.Serializable;
import java.util.Date;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
    @Document(collection = "SampleAccount")

public class Account {

    @Id
    private String acntName;

    @Indexed
    private Date startDate;

    @Indexed
    private String accntType;

    private Long acntId;

    private Byte byteField;

    public String getAcntName() {
        return this.acntName;
    }

    public void setAcntName(String acntName) {
        this.acntName = acntName;
    }

    public Date getStartDate() {
        return this.startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public String getAccntType() {
        return this.accntType;
    }

    public void setAccntType(String accntType) {
        this.accntType = accntType;
    }

    public Long getAcntId() {
        return this.acntId;
    }

    public void setAcntId(Long acntId) {
        this.acntId = acntId;
    }

    public Byte getByteField() {
        return this.byteField;
    }

    public void setByteField(Byte byteField) {
        this.byteField = byteField;
    }

}


public interface AccountRepository extends MongoRepository<Account, String>{

    public Page<Account> findByAcntNameOrStartDate(String acntName, Date startDate, Pageable pageable);

    public Page<Account> findByAccntTypeOrStartDate(String accntType, Date startDate, Pageable pageable);

    public Account findByAcntName(String acntName);

    public Page<Account> findByAcntNameIn(List<String> pkIdList, Pageable pageable);

}

package com.user.entity;

import java.io.Serializable;
import java.util.Date;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "SampleUser")

public class User {

    @Id
    private String acntName;

    @Indexed
    private Date startDate;

    @Indexed
    private String accntType;

    private Long acntId;

    private Byte byteField;

    public String getAcntName() {
        return this.acntName;
    }

    public void setAcntName(String acntName) {
        this.acntName = acntName;
    }

    public Date getStartDate() {
        return this.startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public String getAccntType() {
        return this.accntType;
    }

    public void setAccntType(String accntType) {
        this.accntType = accntType;
    }

    public Long getAcntId() {
        return this.acntId;
    }

    public void setAcntId(Long acntId) {
        this.acntId = acntId;
    }

    public Byte getByteField() {
        return this.byteField;
    }

    public void setByteField(Byte byteField) {
        this.byteField = byteField;
    }

}

package com.user.repo;

import java.util.Date;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;

import com.account.entity.Account;
import com.user.entity.User;

public interface UserRepository extends MongoRepository<User, String>{

    public Page<Account> findByAcntNameOrStartDate(String acntName, Date startDate, Pageable pageable);

    public Page<Account> findByAccntTypeOrStartDate(String accntType, Date startDate, Pageable pageable);

    public Account findByAcntName(String acntName);

    public Page<Account> findByAcntNameIn(List<String> pkIdList, Pageable pageable);

}



JavaConfig Files:

MongoConfig.class


package com.Config;

import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.CustomConversions;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;

@Configuration
@EnableMongoRepositories(basePackages = {"xxx.xxx.xxx"},mongoTemplateRef="primaryTemplate")
public class MongoConfig {

    @Resource
    Environment environment;

    public @Bean(name = "primarydb")

            MongoDbFactory mongoDbFactory() throws Exception {

        MongoCredential credential = MongoCredential.createCredential(
                (environment.getProperty("xxx.datastore.mongo.server.userName")),
                environment.getProperty("xxx.datastore.mongo.server.database"),
                environment.getProperty("xxx.datastore.mongo.server.password").toCharArray());

        List<MongoCredential> mongoCredentials = new ArrayList<>();
        mongoCredentials.add(credential);

        MongoClientOptions options = MongoClientOptions.builder()
                .connectionsPerHost(
                        Integer.parseInt(environment.getProperty("xxx.datastore.mongo.server.connectionsPerHost")))
                .build();

        List<ServerAddress> serverAddress = new ArrayList<>();

        prepareServerAddress(serverAddress);

        MongoClient mongoClient = new MongoClient(serverAddress, mongoCredentials, options);

        return new SimpleMongoDbFactory(mongoClient,
                environment.getProperty("xxx.datastore.mongo.server.database"));
    }

    private void prepareServerAddress(List<ServerAddress> serverAddress) throws UnknownHostException {

        String serverAddressList[] = environment.getProperty("XDM.datastore.mongo.server.address")
                .split("\\s*,\\s*");

        for (String svrAddress : serverAddressList) {
            String address[] = svrAddress.split("\\s*:\\s*");
            String host = Objects.nonNull(address[0]) ? address[0] : "127.0.0.1";
            Integer port = Objects.nonNull(address[1]) ? Integer.valueOf(address[1]) : 27017;
            serverAddress.add(new ServerAddress(host, port));
        }
    }

    @Primary
    public @Bean(name = "primaryTemplate")

            MongoTemplate mongoTemplate(@Qualifier(value = "primarydb") MongoDbFactory factory) throws Exception {
        MongoTemplate mongoTemplate = new MongoTemplate(factory);

        return mongoTemplate;
    }
}

0 个答案:

没有答案