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,21 +1,22 @@
/** /*
* 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");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* <p> * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* <p> * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
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,17 +1,17 @@
/** /*
* 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");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* <p> * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* <p> * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.mybatisflex.spring.boot; package com.mybatisflex.spring.boot;

View File

@ -1,17 +1,17 @@
/** /*
* 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");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* <p> * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* <p> * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.mybatisflex.spring.boot; package com.mybatisflex.spring.boot;
@ -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.