• SpringBoot线程上下文传递数据


    1、底层实现

    使用ThreadLocal,使用方法

    1. public T get()
    2. public void set(T value)
    3. public void remove()

    2、自定义上下文

    1. package com.ybw.context.config;
    2. import com.ybw.context.dto.TenantDTO;
    3. /**
    4. * @author ybw
    5. * @version V1.0
    6. * @className TenantContext
    7. * @date 2022/9/30
    8. **/
    9. public class TenantContext {
    10. private static final ThreadLocal<TenantDTO> context = new ThreadLocal<>();
    11. /**
    12. * 构造方法私有化
    13. *
    14. * @methodName: TenantContext
    15. * @return:
    16. * @author: ybw
    17. * @date: 2022/9/30
    18. **/
    19. private TenantContext() {
    20. }
    21. /**
    22. * 存放租户信息
    23. *
    24. * @param tenantDTO
    25. * @methodName: set
    26. * @return: void
    27. * @author: ybw
    28. * @date: 2022/9/30
    29. **/
    30. public static void set(TenantDTO tenantDTO) {
    31. context.set(tenantDTO);
    32. }
    33. /**
    34. * 获取组合信息
    35. *
    36. * @methodName: get
    37. * @return: com.ybw.mybatis.multi.tenant.dto.TenantDTO
    38. * @author: ybw
    39. * @date: 2022/9/30
    40. **/
    41. public static TenantDTO get() {
    42. return context.get();
    43. }
    44. /**
    45. * 清除当前线程内引用,防止内存泄漏
    46. *
    47. * @methodName: remove
    48. * @return: void
    49. * @author: ybw
    50. * @date: 2022/9/30
    51. **/
    52. public static void remove() {
    53. context.remove();
    54. }
    55. }

    3、定义dto

    1. package com.ybw.context.dto;
    2. import lombok.AllArgsConstructor;
    3. import lombok.Data;
    4. import lombok.NoArgsConstructor;
    5. /**
    6. * @author ybw
    7. * @version V1.0
    8. * @className TenantDTO
    9. * @date 2022/9/30
    10. **/
    11. @NoArgsConstructor
    12. @AllArgsConstructor
    13. @Data
    14. public class TenantDTO {
    15. /**
    16. * 组合层级
    17. *
    18. * @author: ybw
    19. * @date: 2022/9/30
    20. **/
    21. private Integer level;
    22. /**
    23. * 租户id
    24. *
    25. * @author: ybw
    26. * @date: 2022/9/30
    27. **/
    28. private Long tenantId;
    29. }

    4、调用

    1. package com.ybw.context.service;
    2. /**
    3. * @className TenantService
    4. * @author ybw
    5. * @date 2022/10/1
    6. * @version V1.0
    7. **/
    8. public interface TenantService {
    9. /**
    10. *
    11. * @methodName: print
    12. * @return: void
    13. * @author: ybw
    14. * @date: 2022/10/1
    15. **/
    16. void print();
    17. }
    1. package com.ybw.context.service.impl;
    2. import com.alibaba.fastjson2.JSON;
    3. import com.ybw.context.config.TenantContext;
    4. import com.ybw.context.dto.TenantDTO;
    5. import com.ybw.context.service.TenantService;
    6. import lombok.extern.slf4j.Slf4j;
    7. import org.springframework.stereotype.Service;
    8. @Service
    9. @Slf4j
    10. public class TenantServiceImpl implements TenantService {
    11. @Override
    12. public void print() {
    13. TenantDTO tenantDTO = TenantContext.get();
    14. TenantContext.remove();
    15. if (tenantDTO == null) {
    16. return;
    17. }
    18. log.info("tenantDTO:{}", JSON.toJSONString(tenantDTO));
    19. }
    20. }

    5、测试

    1. package com.ybw.context.service;
    2. import com.ybw.context.config.TenantContext;
    3. import com.ybw.context.dto.TenantDTO;
    4. import lombok.extern.slf4j.Slf4j;
    5. import org.junit.jupiter.api.Test;
    6. import org.springframework.boot.test.context.SpringBootTest;
    7. import javax.annotation.Resource;
    8. import static org.junit.jupiter.api.Assertions.*;
    9. @SpringBootTest
    10. @Slf4j
    11. class TenantServiceTest {
    12. @Resource
    13. private TenantService tenantService;
    14. @Test
    15. void print() {
    16. TenantContext.set(new TenantDTO(1,12L));
    17. tenantService.print();
    18. }
    19. }

    输出结果

    [INFO ] 2022-10-01 22:45:13.207 [main] c.y.c.service.TenantServiceImpl - tenantDTO:{"level":1,"tenantId":12}
    

    6、示例代码

    share: 分享仓库 - Gitee.com

  • 相关阅读:
    JMU 数科 数据库与数据仓库期末总结(2)选择题
    双引擎 GPU 容器虚拟化,用户态和内核态的技术解析和实践分享
    【算法修炼】二分查找最接近元素(最接近下标)
    11.25总结
    C# Sqlite数据库的搭建及使用技巧
    Emmabuntüs Debian Edition 5 正式发布
    基于矩阵分解模型的协同过滤理论概述(涉及到SVD,SVD++,TimeSVD++)
    电脑报2022年第24期Scratch制作通讯录管理系统存在的小瑕疵
    【学习笔记】ARC147/ARC141/ARC145
    能通过Ping命令访问CentOS 9 Stream,但在使用Xshell连接
  • 原文地址:https://blog.csdn.net/xixingzhe2/article/details/127138247