折腾了我两天的springboot数据源datasource循环依赖问题,都被搞疯掉了
在做项目重构的时候增加了两个功能
1、多数据源。
2、token的验证从以前的数据库验证,移到了redis端。
1、多数据源使用 druid-spring-boot-starter 套件
其核心代码如下
@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,核心代码如下
@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
└─────┘
过程就不过了,痛苦。
。
。
。
。
。
。
结论是把
@Component
public class OAuth2Realm extends AuthorizingRealm {
// @Autowired
// private ShiroService shiroService;
中这两行的注解打开就行。或随便找个service注册都行。
目的是让datasource提前注册到spring容器。
这个肯定不是好办法,先这样吧。受不了了。记录一下。
有更好的办法,欢迎留言。谢谢

浙公网安备 33010602011771号