Spring Boot配置MongoDB多数据源(3)
- UID
- 1066743
|
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";
}
} |
|
|
|
|
|