在做项目重构的时候增加了两个功能

  1、多数据源。

  2、token的验证从以前的数据库验证,移到了redis端。

1、多数据源使用 druid-spring-boot-starter 套件

  其核心代码如下

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Configuration
public class DynamicDataSourceConfig {
 
    @Bean
    @ConfigurationProperties("spring.datasource.druid.first")
    public DataSource firstDataSource(){
        return DruidDataSourceBuilder.create().build();
    }
 
    @Bean
    @ConfigurationProperties("spring.datasource.druid.second")
    public DataSource secondDataSource(){
        return DruidDataSourceBuilder.create().build();
    }
 
    @Bean
    @Primary
    public DynamicDataSource dataSource(DataSource firstDataSource, DataSource secondDataSource) {
        Map<String, DataSource> targetDataSources = new HashMap<>();
        //targetDataSources.put(DataSourceContext.FIRST, firstDataSource);
        targetDataSources.put(DataSourceContext.SECOND, secondDataSource);
        return new DynamicDataSource(firstDataSource, targetDataSources);
    }
}

  2、token验证规则使用spring-shiro,核心代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
@Component
public class OAuth2Realm extends AuthorizingRealm {
//    @Autowired
//    private ShiroService shiroService;
    @Autowired
    private RedisTokenManager redisTokenManager;
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof OAuth2Token;
    }
 
    /**
     * 授权(验证权限时调用)
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//        SysUserEntity user = (SysUserEntity)principals.getPrimaryPrincipal();
//        Long userId = user.getUserId();
//
//        //用户权限列表
//        Set<String> permsSet = shiroService.getUserPermissions(userId);
//
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        info.setStringPermissions(permsSet);
//        return info;
        return null;
    }
 
    /**
     * 认证(登录时调用)
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String accessToken = (String) token.getPrincipal();
//        UsernamePasswordToken up = (UsernamePasswordToken) token;
        //根据accessToken,查询用户信息
        TokenModel tokenEntity = redisTokenManager.getToken(accessToken);
        //token失效
        if(tokenEntity == null ){
            throw new IncorrectCredentialsException("token失效,请重新登录");
        }
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(tokenEntity.getUserId(), accessToken, getName());
        return info;
//        if(tokenEntity.getType()==null||
//          Integer.valueOf(tokenEntity.getType()).equals(Constant.LoginUserType.CUSTOM.getValue())){
//          //查询用户信息
//            SysUserEntity user = shiroService.queryUser(tokenEntity.getUserId());
//            //账号锁定
//            if(user.getStatus() == 0){
//                throw new LockedAccountException("账号已被锁定,请联系管理员");
//            }
//            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, accessToken, getName());
//            return info;
//        }else{
//          //查询用户信息
//            SysUserEntity user = shiroService.queryUser(tokenEntity.getUserId());
//            //账号锁定
//            if(user.getStatus() == 0){
//                throw new LockedAccountException("账号已被锁定,请联系管理员");
//            }
//            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, accessToken, getName());
//            return info;
//        }
 
 
 
    }
 
}

然后启动项目,就出现datasource循环依赖的问题。

The dependencies of some of the beans in the application context form a cycle:

evaluationCarService (field private io.yeliang.business.dao.EvaluationCarDao io.yeliang.business.service.impl.EvaluationCarServiceImpl.evaluationCarDao)

evaluationCarDao defined in file [D:\workspace\tmxc-parent\tmxc-order-service\target\classes\io\yeliang\business\dao\EvaluationCarDao.class]

sqlSessionFactory defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]
┌─────┐
| dataSource defined in class path resource [io/yeliang/dynamicdatasource/DynamicDataSourceConfig.class]
↑ ↓
| firstDataSource defined in class path resource [io/yeliang/dynamicdatasource/DynamicDataSourceConfig.class]
↑ ↓
| dataSourceInitializer
└─────┘

过程就不过了,痛苦。

结论是把

1
2
3
4
@Component
public class OAuth2Realm extends AuthorizingRealm {
//    @Autowired
//    private ShiroService shiroService;<br>中这两行的注解打开就行。或随便找个service注册都行。<br>目的是让datasource提前注册到spring容器。<br><br>这个肯定不是好办法,先这样吧。受不了了。记录一下。
1
有更好的办法,欢迎留言。谢谢

折腾了我两天的springboot数据源datasource循环依赖问题,都被搞疯掉了的更多相关文章

  1. springboot bean的循环依赖实现 源码分析

    springboot bean的循环依赖实现 源码分析 本文基于springboot版本2.5.1 <parent> <groupId>org.springframework. ...

  2. 两种解决springboot 跨域问题的方法示例

    两种解决springboot 跨域问题的方法示例,哪种方法看情况而定,自己选择.社会Boolean哥,人狠话不多,直接上代码. 第一种实现方式:       此种方式做全局配置,用起来更方便,但是无法 ...

  3. iOS基础 - UITableView的数据源(dataSource)和代理(delegate)

    UITableView的数据源(dataSource)和代理(delegate) UITableView需要一个数据源(dataSource)来显示数据,UITableView会向数据源查询一共有多少 ...

  4. flink学习笔记-数据源(DataSource)

    说明:本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKh ...

  5. Springboot循环依赖

    背景 最近在使用Springboot做项目的时候,遇到了一个循环依赖的 问题.那什么是循环依赖呢,常见的一种情形就是在ServiceA中注入了ServiceB,在ServiceB中也注入了Servic ...

  6. Maven多模块开发SpringBoot项目自定义第三方依赖版本

    参考:官方文档 - Build System of Maven https://blog.didispace.com/books/spring-boot-reference/IX. 'How-to' ...

  7. Springboot循环依赖实践纪实

    测试的Springboot版本: 2.6.4,禁止了循环依赖,但是可以通过application.yml开启(哈哈) @Lazy注解解决循环依赖 情况一:只有简单属性关系的循环依赖 涉及的Bean: ...

  8. JDBC数据源(DataSource)的简单实现

    数据源技术是Java操作数据库的一个很关键技术,流行的持久化框架都离不开数据源的应用.   数据源提供了一种简单获取数据库连接的方式,并能在内部通过一个池的机制来复用数据库连接,这样就大大减少创建数据 ...

  9. C#本质论第四版-1,抄书才能看下去,不然两三眼就看完了,一摞书都成了摆设。抄下了记忆更深刻

    C#本质论第四版-1,抄书才能看下去,不然两三眼就看完了,一摞书都成了摆设.抄下了记忆更深刻 本书面向的读者 写作本书时,我面临的一个挑战是如何持续吸引高级开发人员眼球的同时,不因使用assembly ...

随机推荐

  1. Java编程思想(四) —— 复用类

    看了老罗罗升阳的专訪,不由自主地佩服,非常年轻,我之前以为和罗永浩一个级别的年龄.也是见过的不是初高中编程的一位大牛之中的一个,专訪之后.发现老罗也是一步一个脚印的人. 别说什么难做,做不了.你根本就 ...

  2. BZOJ1814: Ural 1519 Formula 1(插头Dp)

    Description Regardless of the fact, that Vologda could not get rights to hold the Winter Olympic gam ...

  3. 小米开源文件管理器MiCodeFileExplorer-源码研究(6)-媒体文件MediaFile和文件类型MimeUtils

    接着之前的第4篇,本篇的2个类,仍然是工具类.MediaFile,媒体文件,定义了一大堆的常量,真正的有用的方法就几个.isAudioFileType.isVideoFileType之类的. Mime ...

  4. 南阳oj 士兵杀敌(二) 题目116 NYOJ 数据结构

     /*士兵杀敌(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描写叙述 南将军手下有N个士兵.分别编号1到N.这些士兵的杀敌数都是已知的. 小工是南将军手下的军师, ...

  5. 车牌识别--S5PV210測试第二次优化

    优化: 1.RGB转HSV 浮点运算改成定点运算: 2.匹配模板由图片改成C语言数组: 3.优化測试BMP车牌图片读取(两层for循环改为一层for循环). 总体相比优化之前时间降低110ms左右. ...

  6. Apache ZooKeeper Getting Started Guide 翻译

    ZooKeeper 開始向导 開始:用zookeeper协调分布式程序 单例操作 管理zookeeper存储 连接zookeeper 执行zookeeper 以复制模式执行zookeeper 其他优化 ...

  7. 4.dubbo-demo+简易监控中心安装+管理控制台安装

    转自:https://blog.csdn.net/zhangweigangweiwu/article/details/52244099 tomcat:apache-tomcat-6.0.39 需要用到 ...

  8. 父子margin塌陷

    1.使用padding 2.给父级使用border 3.给父级添加属性 overflow:hidden 4.浮动 5.定位{absolute,fixed} 6.伪元素代码 .parent:before ...

  9. BZOJ2157: 旅游(LCT)

    Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T 城的任意两个景点之间 ...

  10. ArcGIS小技巧——提取面要素的质心点

    如下图,现在要做这样一件事,提取面图层中每一个图斑的质心点,然后使用质心点提取图层中的一个属性值,并在此基础上进行克里金插值,生成该属性的空间插值图.当然,今天这段文字主要简单说一下怎样提取面图层的质 ...