電子技術論壇_中國專業的電子工程師學習交流社區-中電網技術論壇's Archiver

look_w 發表于 2019-5-18 09:57

Spring Boot配置MongoDB多數據源(3)

排除mongo的自動注入

有一點需要特別注意的是Spring Boot會自動注入mongoTemplate,與我們設置的多個數據源有沖突。為了防止默認注入,需要排除自動注入的類。
找到Spring Boot的啟動類Applocation.java在@SpringBootApplication后面新增排除類如下:

@SpringBootApplication(exclude = {MongoAutoConfiguration.class,MongoDataAutoConfiguration.class,MongoDbAutoConfiguration.class})



完整的Applocation.java如下:

package com.biologic;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

import com.mofang.microservice.support.boot.feign.FeignDefaultContractConfiguration;
import com.mofang.microservice.support.boot.mongodb.MongoDbAutoConfiguration;



@SpringBootApplication(exclude = {MongoAutoConfiguration.class,MongoDataAutoConfiguration.class,MongoDbAutoConfiguration.class})
@EnableDiscoveryClient
public class Applocation {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Applocation.class, args);
    }

}



使用多個數據源

使用時,直接指明對應注入即可,如下:

package com.biologic.api;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.biologic.api.service.QualityCheckRepository;
import com.biologic.entity.QualityCheck;

@Controller
@RequestMapping(path = "/demo")
public class QualityCheckController {

    @Autowired
    @Qualifier(value = "mongoTemplate")
    MongoTemplate mongoTemplate;

    @Autowired
    @Qualifier(value = "logMongoTemplate")
    MongoTemplate logMongoTemplate;

    @GetMapping(path = "/qualityCheck")
    public @ResponseBody String qualityCheck() {


            PrepareReportPool repareReportPool = new PrepareReportPool();
            repareReportPool.setBarcodes(okBarCodes);
            repareReportPool.setChangeStateTaskId(autoQualityCheckLog.getId());
            repareReportPool.setCreateTime(new Date());
            mongoTemplate.save(repareReportPool);

        AutoQualityCheckLog autoQualityCheckLog = new AutoQualityCheckLog();
        autoQualityCheckLog.setId(UUID.randomUUID().toString());
        autoQualityCheckLog.setCheckTime(new Date());

        autoQualityCheckLog.setAbnormalBarcodes(abnormalBarCodes);
        autoQualityCheckLog.setFailBarcodes(failbarcodeList);
        autoQualityCheckLog.setPassBarcodes(okBarCodes);
        autoQualityCheckLog.setNoDpBarcodes(noDpBarcodes);
        autoQualityCheckLog.setStateAbnormalDp(stateAbnormalDp);
        autoQualityCheckLog.setOkStateChangeBarcodes(okStateChangeBarcodes);
        autoQualityCheckLog.setFailStateChangeBarcodes(failStateChangeBarcodes);
        logMongoTemplate.save(autoQualityCheckLog);
        return "ok";
    }

}

頁: [1]

Powered by eccn 7.0.0  © 2001-2009 Eccn Inc.

成茄子视频人app污下载-茄子视频ios下载安装-茄子视频懂你更多qz8app懂你更多