spring-boot配置多例,多数据源遇到的坑

spring-boot配置多例,多数据源遇到的坑

在不配置多例的时候是不用配置红框内的设置,但是配置多例后如果不配置就会找不到数据配置信息。

spring-boot配置多例,多数据源遇到的坑

配置多例要添加@Scope注解。

spring-boot配置多例,多数据源遇到的坑

使用这种方式来获取对象。

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DataSourceConfig.class);
TrademarkService trademarkService = context.getBean(TrademarkService.class);

  @Primary 只能在一个数据源上