You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/10/28 01:18:00 UTC

[shardingsphere] branch master updated: fix issue: startup application with springboot failed when no rules config. (#21789)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new dbe14084925 fix issue: startup application with springboot failed when no rules config. (#21789)
dbe14084925 is described below

commit dbe140849257ff2d996008a7ca6bea72bc7dda21
Author: pandaapo <35...@users.noreply.github.com>
AuthorDate: Fri Oct 28 09:17:45 2022 +0800

    fix issue: startup application with springboot failed when no rules config. (#21789)
    
    * fix issue: startup application with springboot failed when no rules config.
    
    * Rename class. Add unit test.
---
 .../boot/ShardingSphereAutoConfiguration.java      |  4 +-
 ...java => ShardingSphereSpringBootCondition.java} |  8 ++--
 .../boot/SpringBootStarterWithoutRulesTest.java    | 44 ++++++++++++++++++++++
 .../test/resources/application-no-rules.properties | 29 ++++++++++++++
 4 files changed, 79 insertions(+), 6 deletions(-)

diff --git a/jdbc/spring/core/spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/ShardingSphereAutoConfiguration.java b/jdbc/spring/core/spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/ShardingSphereAutoConfiguration.java
index 00d3a62e9ff..054260f441b 100644
--- a/jdbc/spring/core/spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/ShardingSphereAutoConfiguration.java
+++ b/jdbc/spring/core/spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/ShardingSphereAutoConfiguration.java
@@ -24,7 +24,7 @@ import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
 import org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlModeConfigurationSwapper;
 import org.apache.shardingsphere.spring.boot.datasource.DataSourceMapSetter;
 import org.apache.shardingsphere.spring.boot.prop.SpringBootPropertiesConfiguration;
-import org.apache.shardingsphere.spring.boot.rule.LocalRulesCondition;
+import org.apache.shardingsphere.spring.boot.rule.ShardingSphereSpringBootCondition;
 import org.apache.shardingsphere.spring.boot.schema.DatabaseNameSetter;
 import org.apache.shardingsphere.spring.transaction.TransactionTypeScanner;
 import org.springframework.beans.factory.ObjectProvider;
@@ -84,7 +84,7 @@ public class ShardingSphereAutoConfiguration implements EnvironmentAware {
      * @throws SQLException SQL exception
      */
     @Bean
-    @Conditional(LocalRulesCondition.class)
+    @Conditional(ShardingSphereSpringBootCondition.class)
     @Autowired(required = false)
     public DataSource shardingSphereDataSource(final ObjectProvider<List<RuleConfiguration>> rules, final ObjectProvider<ModeConfiguration> modeConfig) throws SQLException {
         Collection<RuleConfiguration> ruleConfigs = Optional.ofNullable(rules.getIfAvailable()).orElseGet(Collections::emptyList);
diff --git a/jdbc/spring/core/spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/rule/LocalRulesCondition.java b/jdbc/spring/core/spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/rule/ShardingSphereSpringBootCondition.java
similarity index 90%
rename from jdbc/spring/core/spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/rule/LocalRulesCondition.java
rename to jdbc/spring/core/spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/rule/ShardingSphereSpringBootCondition.java
index 34e0c6637ca..1887e90aec8 100644
--- a/jdbc/spring/core/spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/rule/LocalRulesCondition.java
+++ b/jdbc/spring/core/spring-boot-starter/src/main/java/org/apache/shardingsphere/spring/boot/rule/ShardingSphereSpringBootCondition.java
@@ -24,16 +24,16 @@ import org.springframework.context.annotation.ConditionContext;
 import org.springframework.core.type.AnnotatedTypeMetadata;
 
 /**
- * Local rules condition.
+ * Local configuration condition.
  */
-public final class LocalRulesCondition extends SpringBootCondition {
+public final class ShardingSphereSpringBootCondition extends SpringBootCondition {
     
-    private static final String SHARDING_PREFIX = "spring.shardingsphere.rules";
+    private static final String SHARDING_PREFIX = "spring.shardingsphere";
     
     @Override
     public ConditionOutcome getMatchOutcome(final ConditionContext conditionContext, final AnnotatedTypeMetadata annotatedTypeMetadata) {
         return PropertyUtil.containPropertyPrefix(conditionContext.getEnvironment(), SHARDING_PREFIX)
                 ? ConditionOutcome.match()
-                : ConditionOutcome.noMatch("Can't find ShardingSphere rule configuration in local file.");
+                : ConditionOutcome.noMatch("Can't find ShardingSphere configuration in local file.");
     }
 }
diff --git a/jdbc/spring/core/spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/SpringBootStarterWithoutRulesTest.java b/jdbc/spring/core/spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/SpringBootStarterWithoutRulesTest.java
new file mode 100644
index 00000000000..f9086faae72
--- /dev/null
+++ b/jdbc/spring/core/spring-boot-starter/src/test/java/org/apache/shardingsphere/spring/boot/SpringBootStarterWithoutRulesTest.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.spring.boot;
+
+import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.ActiveProfiles;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import static org.junit.Assert.assertNotNull;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = SpringBootStarterWithoutRulesTest.class)
+@SpringBootApplication
+@ActiveProfiles("no-rules")
+public class SpringBootStarterWithoutRulesTest {
+    
+    @Autowired
+    private ShardingSphereDataSource dataSource;
+    
+    @Test
+    public void assertSpringBootStartup() {
+        assertNotNull(dataSource);
+    }
+}
diff --git a/jdbc/spring/core/spring-boot-starter/src/test/resources/application-no-rules.properties b/jdbc/spring/core/spring-boot-starter/src/test/resources/application-no-rules.properties
new file mode 100644
index 00000000000..99f4cd18c2b
--- /dev/null
+++ b/jdbc/spring/core/spring-boot-starter/src/test/resources/application-no-rules.properties
@@ -0,0 +1,29 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+spring.shardingsphere.database.name=foo_db
+
+spring.shardingsphere.datasource.names=read_ds_${0..1},write_ds
+
+spring.shardingsphere.datasource.read_ds_0.type=org.apache.shardingsphere.test.mock.MockedDataSource
+spring.shardingsphere.datasource.read_ds_1.type=org.apache.shardingsphere.test.mock.MockedDataSource
+spring.shardingsphere.datasource.write_ds.type=org.apache.shardingsphere.test.mock.MockedDataSource
+
+spring.shardingsphere.props.sql-show=true
+spring.shardingsphere.props.kernel-executor-size=10
+
+spring.main.banner-mode=off