解决springboot环境切换失效的问题
作者:快乐柠檬 时间:2023-11-11 20:40:35
springboot环境切换失效
概述
最近在使用-Dspring.profiles.active=te 来切换spring-boot的环境时,发现日志打印的是:
...ApplicationStartUp -
The following profiles are active: de
也就是说,参数失效了。
debug调试时,发现spring-boot读取的也是de,不是te。
解决
下载了一个新的tomcat,然后重新发布程序,设置参数,启动,发现环境正常切换了过来。
SpringBoot多数据源切换无效(不切换)
SpringBoot的多数据源实现以实现AbstractRoutingDataSource#determineCurrentLookupKey()来达到多个数据源动态切换的目的。
网上有很多的文章可以获取具体方法,就不在讲了。
项目中需要用到多数据源MySQL和SQLServer两个数据库,系统要保持两个数据库的数据同步,就需要来回切数据源来操作数据库。
刚写好了数据从MySQL同步到SQLServer中的代码,测试发现数据源不能切换到SQLServer数据库连接,排查问题~~省略n多个小时后,
源码解析等理论就不写了(重要的是不会写)直接上修改方案
CSDN查到的方案有
数据源切换和事务的注入顺序问题,像下面这样的,加入@Order注解。测试无效(可能我搭的架构有问题)
@Aspect
@Slf4j
@Order(-1) // 保证优先级
@Component
public class DatasourceAspect {
@Pointcut("@within(DataSource)|| @annotation(DataSource)")
public void pointcut() {
}
@Before("pointcut() && @annotation(dataSource)")
public void before(DataSource dataSource) {
DatasourceContextHolder.setDatasource(dataSource.value().getDbName());
}
@After("pointcut()")
public void after() {
DatasourceContextHolder.clear();
}
}
Mapper(Dao)层切换数据源,反正我没找到怎么个用法,所以没有测试。
还有下面的这种写法。测试有效的
@DataSource(DatasourceEnum.DB3)
public int insertSelective( String hbNo, ) throws Exception{
CcpHeaderSqlServer record = new CcpHeaderSqlServer();
if (log.isDebugEnabled()) {
log.debug("同步到SQLServer的数据对象:[{}]", record);
}
......
// 这种调用保存数据的方法,有效切换数据源
XXXService service = SpringContextHolder.getBean(XXXService.class);
return service.saveHeader(record);
}
@DataSource(DatasourceEnum.DB3)
public int saveHeader(XXXEntity record) {
return xxxMapper.insertSelective(record);
}
来源:https://blog.csdn.net/limenghua9112/article/details/79608257
标签:springboot,环境,切换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#邮件定时群发工具Atilia用法实例
2022-06-13 11:50:24
![](https://img.aspxhome.com/file/2023/1/106351_0s.png)
Java学习关于循环和数组练习题整理
2022-07-19 21:01:24
Idea自动生成Entity实现过程详解
2022-06-03 12:18:24
![](https://img.aspxhome.com/file/2023/4/89474_0s.png)
SpringBoot整合Web开发之文件上传与@ControllerAdvice
2021-09-29 04:43:55
![](https://img.aspxhome.com/file/2023/5/119165_0s.png)
Android编程设计模式之备忘录模式详解
2023-06-29 14:55:15
![](https://img.aspxhome.com/file/2023/0/111730_0s.jpg)
Maven+SSM框架实现简单的增删改查
2023-11-16 17:14:38
![](https://img.aspxhome.com/file/2023/0/59720_0s.png)
Java面试题冲刺第二十二天-- Nginx
2023-11-16 13:43:48
![](https://img.aspxhome.com/file/2023/6/58916_0s.png)
Android 8.0如何完美适配全局dialog悬浮窗弹出
2023-04-09 12:54:00
![](https://img.aspxhome.com/file/2023/8/90138_0s.png)
详解Java实现设计模式之责任链模式
2023-11-08 10:32:07
![](https://img.aspxhome.com/file/2023/7/72557_0s.jpg)
Springboot整合pagehelper分页功能
2021-12-14 15:53:48
![](https://img.aspxhome.com/file/2023/0/65270_0s.png)
java实现一个简单的网络爬虫代码示例
2021-08-05 13:59:12
Spring依赖注入(DI)两种方式的示例详解
2021-09-14 12:00:20
Java中类的定义与实例化详解
2022-10-25 23:18:43
![](https://img.aspxhome.com/file/2023/4/69084_0s.png)
C#聊天程序服务端与客户端完整实例代码
2023-08-26 18:59:35
c# DataDirectory的用法
2021-07-30 14:36:22
Java线程中sleep和wait的区别详细介绍
2021-08-27 02:23:12
Java 网络编程总结
2023-11-10 22:19:29
![](https://img.aspxhome.com/file/2023/1/59071_0s.png)
SpringBoot添加自定义拦截器的实现代码
2023-11-26 13:30:54
SpringBoot如何进行对象复制的实践
2023-11-23 03:40:19
![](https://img.aspxhome.com/file/2023/5/59935_0s.jpg)
SpringBoot自动配置深入探究实现原理
2023-08-06 09:59:58
![](https://img.aspxhome.com/file/2023/9/62059_0s.png)