springboot 多模块多数据源@DS失效

@DS注解不生效的几种情况

进过验证,@DS注解加到mapper接口、service接口、service方法里都不生效,获取的还是默认的主数据源。猜测是由于spring的aop切面机制导致拦截不到@DS注解,进而不能切换数据源,正确的做法是添加到service实现类或者实现类里具体的方法上。
在事务方法内调用@DS注解的方法,@DS注解同样不生效,原因是spring只能拦截到最外层方法的@Transactional注解,此时加载该事务的数据源,在事务方法内即使调用了@DS注解的方法,获取的是外层事务的数据源,导致@DS失效。
在同一个实现类中,一个非DS注解的常规方法里调用@DS注解的方法,同样存在@DS失效的情况,原因同2,是由spring的aop机制导致的,如果确有这种业务需要,可以将该DS注解方法定义在不同的类中,通过bean注入的方式调用,就不会出现这个问题。
  • 15
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,我们可以使用多个数据源,但是默认情况下只能使用一个数据源。如果我们想要使用多个数据源,我们可以使用Dynamic DataSource Routing(动态数据源路由)来解决这个问题。Dynamic DataSource Routing可以根据当前线程的上下文信息来动态地切换数据源。 在使用Dynamic DataSource Routing之前,我们需要使用Spring Boot的AOP功能来拦截MyBatis的Mapper接口,并根据当前线程的上下文信息来动态地切换数据源。具体步骤如下: 1.添加依赖 ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>${dynamic.datasource.version}</version> </dependency> ``` 2.配置数据源 ```yaml spring: datasource: master: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver slave1: url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver ``` 3.配置动态数据源 ```java @Configuration public class DataSourceConfig { @Bean @ConfigurationProperties(prefix = "spring.datasource.master") public DataSource masterDataSource() { return DataSourceBuilder.create().build(); } @Bean @ConfigurationProperties(prefix = "spring.datasource.slave1") public DataSource slave1DataSource() { return DataSourceBuilder.create().build(); } @Bean public DynamicDataSource dynamicDataSource(@Qualifier("masterDataSource") DataSource masterDataSource, @Qualifier("slave1DataSource") DataSource slave1DataSource) { Map<Object, Object> targetDataSources = new HashMap<>(); targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource); targetDataSources.put(DataSourceType.SLAVE1.name(), slave1DataSource); return new DynamicDataSource(masterDataSource, targetDataSources); } } ``` 4.配置AOP ```java @Aspect @Component public class DataSourceAspect { @Pointcut("@annotation(com.baomidou.dynamic.datasource.annotation.DS)") public void dsPointCut() { } @Before("dsPointCut()") public void beforeSwitchDS(JoinPoint point) { MethodSignature signature = (MethodSignature) point.getSignature(); DS annotation = signature.getMethod().getAnnotation(DS.class); if (annotation == null) { DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER); } else { DynamicDataSourceContextHolder.setDataSourceType(annotation.value()); } } @After("dsPointCut()") public void afterSwitchDS(JoinPoint point) { DynamicDataSourceContextHolder.clearDataSourceType(); } } ``` 5.使用@DS注解来动态指定数据源 ```java @DS("slave1") public interface UserMapper { List<User> findAll(); } ``` 以上就是在Spring Boot中配置多数据源的方法,通过Dynamic DataSource Routing来实现动态切换数据源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值