fix: 不能远程拉取多数据源配置。

This commit is contained in:
Suomm 2023-06-18 13:32:02 +08:00
parent a249a67995
commit ccede36a7e
3 changed files with 51 additions and 49 deletions

View File

@ -1,4 +1,4 @@
/** /*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p> * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
@ -15,7 +15,8 @@
*/ */
package com.mybatisflex.spring.boot; package com.mybatisflex.spring.boot;
import org.springframework.context.annotation.Condition; import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
import org.springframework.core.env.*; import org.springframework.core.env.*;
@ -33,27 +34,28 @@ import java.util.Iterator;
@Conditional(ConditionalOnMybatisFlexDatasource.OnMybatisFlexDataSourceCondition.class) @Conditional(ConditionalOnMybatisFlexDatasource.OnMybatisFlexDataSourceCondition.class)
public @interface ConditionalOnMybatisFlexDatasource { public @interface ConditionalOnMybatisFlexDatasource {
class OnMybatisFlexDataSourceCondition implements Condition { class OnMybatisFlexDataSourceCondition extends SpringBootCondition {
@Override @Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment env = context.getEnvironment(); Environment env = context.getEnvironment();
if (env instanceof AbstractEnvironment) { if (env instanceof AbstractEnvironment) {
MutablePropertySources propertySources = ((AbstractEnvironment) env).getPropertySources(); MutablePropertySources propertySources = ((AbstractEnvironment) env).getPropertySources();
Iterator<PropertySource<?>> it = propertySources.stream().iterator(); Iterator<PropertySource<?>> it = propertySources.stream().iterator();
while (it.hasNext()) { while (it.hasNext()) {
PropertySource ps = it.next(); PropertySource<?> ps = it.next();
if (ps instanceof MapPropertySource) { if (ps instanceof MapPropertySource) {
for (String propertyName : ((MapPropertySource) ps).getSource().keySet()) { for (String propertyName : ((MapPropertySource) ps).getSource().keySet()) {
if (propertyName.startsWith("mybatis-flex.datasource.")) { if (propertyName.startsWith("mybatis-flex.datasource.")) {
return true; return ConditionOutcome.match();
} }
} }
} }
} }
} }
return false; return ConditionOutcome.noMatch("'mybatis-flex.datasource' is necessary.");
} }
} }
} }

View File

@ -1,4 +1,4 @@
/** /*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p> * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");

View File

@ -1,4 +1,4 @@
/** /*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p> * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
@ -48,7 +48,7 @@ public class MybatisFlexProperties {
//多数据源的配置 //多数据源的配置
//mybatis-flex.datasource.ds1.url=*** //mybatis-flex.datasource.ds1.url=***
//mybatis-flex.datasource.ds2.url=*** //mybatis-flex.datasource.ds2.url=***
private Map<String, Map<String,String>> datasource; private Map<String, Map<String, String>> datasource;
/** /**
* Location of MyBatis xml config file. * Location of MyBatis xml config file.