This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 0ea9822 refactor AlgorithmFactory&AlgorithmProvidedBeanRegistry
(#7478)
0ea9822 is described below
commit 0ea98220a7d0d1b16aaf65f1d61aa67e9d1d18b3
Author: kimmking <[email protected]>
AuthorDate: Wed Sep 16 16:58:42 2020 +0800
refactor AlgorithmFactory&AlgorithmProvidedBeanRegistry (#7478)
---
.../EncryptAlgorithmProvidedBeanRegistry.java | 2 +-
.../MasterSlaveAlgorithmProvidedBeanRegistry.java | 2 +-
.../boot/ShardingRuleSpringBootConfiguration.java | 12 ++++++++++++
... KeyGenerateAlgorithmProvidedBeanRegistry.java} | 10 +++-------
.../ShardingAlgorithmProvidedBeanRegistry.java | 6 +-----
.../algorithm/ShardingSphereAlgorithmFactory.java | 3 +--
.../AbstractAlgorithmProvidedBeanRegistry.java | 22 +++++++++++-----------
7 files changed, 30 insertions(+), 27 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/main/java/org/apache/shardingsphere/encrypt/spring/boot/algorithm/EncryptAlgorithmProvidedBeanRegistry.java
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/main/java/org/apache/shardingsphere/encrypt/spring/boot/algorithm/EncryptAlgorithmProvidedBeanRegistry.java
index 1b2f677..f2d5526 100644
---
a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/main/java/org/apache/shardingsphere/encrypt/spring/boot/algorithm/EncryptAlgorithmProvidedBeanRegistry.java
+++
b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-spring/shardingsphere-encrypt-spring-boot-starter/src/main/java/org/apache/shardingsphere/encrypt/spring/boot/algorithm/EncryptAlgorithmProvidedBeanRegistry.java
@@ -26,7 +26,7 @@ import org.springframework.core.env.Environment;
/**
* Encrypt algorithm provided bean registry.
*/
-public final class EncryptAlgorithmProvidedBeanRegistry extends
AbstractAlgorithmProvidedBeanRegistry {
+public final class EncryptAlgorithmProvidedBeanRegistry extends
AbstractAlgorithmProvidedBeanRegistry<EncryptAlgorithm> {
private static final String ENCRYPTORS_ALGORITHMS =
"spring.shardingsphere.rules.encrypt.encryptors.";
diff --git
a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/main/java/org/apache/shardingsphere/masterslave/spring/boot/algorithm/MasterSlaveAlgorithmProvidedBeanRegistry.java
b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/main/java/org/apache/shardingsphere/masterslave/spring/boot/algorithm/MasterSlaveAlg
[...]
index f67e2ee..c50b80e 100644
---
a/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/main/java/org/apache/shardingsphere/masterslave/spring/boot/algorithm/MasterSlaveAlgorithmProvidedBeanRegistry.java
+++
b/shardingsphere-features/shardingsphere-master-slave/shardingsphere-master-slave-spring/shardingsphere-master-slave-spring-boot-starter/src/main/java/org/apache/shardingsphere/masterslave/spring/boot/algorithm/MasterSlaveAlgorithmProvidedBeanRegistry.java
@@ -26,7 +26,7 @@ import org.springframework.core.env.Environment;
/**
* Master slave algorithm provided bean registry.
*/
-public final class MasterSlaveAlgorithmProvidedBeanRegistry extends
AbstractAlgorithmProvidedBeanRegistry {
+public final class MasterSlaveAlgorithmProvidedBeanRegistry extends
AbstractAlgorithmProvidedBeanRegistry<MasterSlaveLoadBalanceAlgorithm> {
private static final String MASTER_SLAVE_ALGORITHMS =
"spring.shardingsphere.rules.master-slave.load-balancers.";
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/ShardingRuleSpringBootConfiguration.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/ShardingRuleSpringBootConfiguration.java
index 831eda6..29fa78c 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/ShardingRuleSpringBootConfiguration.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/ShardingRuleSpringBootConfiguration.java
@@ -22,6 +22,7 @@ import
org.apache.shardingsphere.infra.config.RuleConfiguration;
import
org.apache.shardingsphere.sharding.algorithm.config.AlgorithmProvidedShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
+import
org.apache.shardingsphere.sharding.spring.boot.algorithm.KeyGenerateAlgorithmProvidedBeanRegistry;
import
org.apache.shardingsphere.sharding.spring.boot.algorithm.ShardingAlgorithmProvidedBeanRegistry;
import
org.apache.shardingsphere.sharding.spring.boot.condition.ShardingSpringBootCondition;
import
org.apache.shardingsphere.sharding.spring.boot.rule.YamlShardingRuleSpringBootConfiguration;
@@ -81,4 +82,15 @@ public class ShardingRuleSpringBootConfiguration {
public static ShardingAlgorithmProvidedBeanRegistry
shardingAlgorithmProvidedBeanRegistry(final Environment environment) {
return new ShardingAlgorithmProvidedBeanRegistry(environment);
}
+
+ /**
+ * Create key generator algorithm provided bean registry.
+ *
+ * @param environment environment
+ * @return key generator algorithm provided bean registry
+ */
+ @Bean
+ public static KeyGenerateAlgorithmProvidedBeanRegistry
keyGenerateAlgorithmProvidedBeanRegistry(final Environment environment) {
+ return new KeyGenerateAlgorithmProvidedBeanRegistry(environment);
+ }
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/algorithm/ShardingAlgorithmProvidedBeanRegistry.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/algorithm/KeyGenerateAlgorithmProvidedBeanRegistry.java
similarity index 75%
copy from
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/algorithm/ShardingAlgorithmProvidedBeanRegistry.java
copy to
shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/algorithm/KeyGenerateAlgorithmProvidedBeanRegistry.java
index 4212e41..0b85727 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/algorithm/ShardingAlgorithmProvidedBeanRegistry.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/algorithm/KeyGenerateAlgorithmProvidedBeanRegistry.java
@@ -18,27 +18,23 @@
package org.apache.shardingsphere.sharding.spring.boot.algorithm;
import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
-import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import
org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.core.env.Environment;
/**
- * Sharding algorithm provided bean registry.
+ * Key generator algorithm provided bean registry.
*/
-public final class ShardingAlgorithmProvidedBeanRegistry extends
AbstractAlgorithmProvidedBeanRegistry {
-
- private static final String SHARDING_ALGORITHMS =
"spring.shardingsphere.rules.sharding.sharding-algorithms.";
+public final class KeyGenerateAlgorithmProvidedBeanRegistry extends
AbstractAlgorithmProvidedBeanRegistry<KeyGenerateAlgorithm> {
private static final String KEY_GENERATORS =
"spring.shardingsphere.rules.sharding.key-generators.";
- public ShardingAlgorithmProvidedBeanRegistry(final Environment
environment) {
+ public KeyGenerateAlgorithmProvidedBeanRegistry(final Environment
environment) {
super(environment);
}
@Override
public void postProcessBeanDefinitionRegistry(final BeanDefinitionRegistry
registry) {
- registerBean(SHARDING_ALGORITHMS, ShardingAlgorithm.class, registry);
registerBean(KEY_GENERATORS, KeyGenerateAlgorithm.class, registry);
}
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/algorithm/ShardingAlgorithmProvidedBeanRegistry.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/algorithm/ShardingAlgorithmProvidedBeanRegistry.java
index 4212e41..0cacac5 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/algorithm/ShardingAlgorithmProvidedBeanRegistry.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-spring/shardingsphere-sharding-spring-boot-starter/src/main/java/org/apache/shardingsphere/sharding/spring/boot/algorithm/ShardingAlgorithmProvidedBeanRegistry.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.sharding.spring.boot.algorithm;
-import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import
org.apache.shardingsphere.spring.boot.registry.AbstractAlgorithmProvidedBeanRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
@@ -26,12 +25,10 @@ import org.springframework.core.env.Environment;
/**
* Sharding algorithm provided bean registry.
*/
-public final class ShardingAlgorithmProvidedBeanRegistry extends
AbstractAlgorithmProvidedBeanRegistry {
+public final class ShardingAlgorithmProvidedBeanRegistry extends
AbstractAlgorithmProvidedBeanRegistry<ShardingAlgorithm> {
private static final String SHARDING_ALGORITHMS =
"spring.shardingsphere.rules.sharding.sharding-algorithms.";
- private static final String KEY_GENERATORS =
"spring.shardingsphere.rules.sharding.key-generators.";
-
public ShardingAlgorithmProvidedBeanRegistry(final Environment
environment) {
super(environment);
}
@@ -39,6 +36,5 @@ public final class ShardingAlgorithmProvidedBeanRegistry
extends AbstractAlgorit
@Override
public void postProcessBeanDefinitionRegistry(final BeanDefinitionRegistry
registry) {
registerBean(SHARDING_ALGORITHMS, ShardingAlgorithm.class, registry);
- registerBean(KEY_GENERATORS, KeyGenerateAlgorithm.class, registry);
}
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/algorithm/ShardingSphereAlgorithmFactory.java
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/algorithm/ShardingSphereAlgorithmFactory.java
index 943bb28..951edfc 100644
---
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/algorithm/ShardingSphereAlgorithmFactory.java
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/algorithm/ShardingSphereAlgorithmFactory.java
@@ -20,7 +20,6 @@ package org.apache.shardingsphere.infra.config.algorithm;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.config.TypedSPIConfiguration;
-import org.apache.shardingsphere.infra.spi.type.TypedSPI;
import org.apache.shardingsphere.infra.spi.type.TypedSPIRegistry;
/**
@@ -38,7 +37,7 @@ public final class ShardingSphereAlgorithmFactory {
* @return algorithm
*/
@SuppressWarnings("unchecked")
- public static <T extends ShardingSphereAlgorithm> T createAlgorithm(final
TypedSPIConfiguration typedSPIConfiguration, final Class<? extends TypedSPI>
algorithmClass) {
+ public static <T extends ShardingSphereAlgorithm> T createAlgorithm(final
TypedSPIConfiguration typedSPIConfiguration, final Class<? extends
ShardingSphereAlgorithm> algorithmClass) {
T result = (T) TypedSPIRegistry.getRegisteredService(algorithmClass,
typedSPIConfiguration.getType(), typedSPIConfiguration.getProps());
if (result instanceof ShardingSphereAlgorithmPostProcessor) {
((ShardingSphereAlgorithmPostProcessor) result).init();
diff --git
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/registry/AbstractAlgorithmProvidedBeanRegistry.java
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/registry/AbstractAlgorithmProvidedBeanRegistry.java
index 30e894a..9e2f652 100644
---
a/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/registry/AbstractAlgorithmProvidedBeanRegistry.java
+++
b/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-spring-infra/shardingsphere-jdbc-spring-boot-starter-infra/src/main/java/org/apache/shardingsphere/spring/boot/registry/AbstractAlgorithmProvidedBeanRegistry.java
@@ -19,9 +19,9 @@ package org.apache.shardingsphere.spring.boot.registry;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
+import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithm;
import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmPostProcessor;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.infra.spi.type.TypedSPI;
import org.apache.shardingsphere.infra.spi.type.TypedSPIRegistry;
import
org.apache.shardingsphere.infra.yaml.config.algorithm.YamlShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.spring.boot.util.PropertyUtil;
@@ -41,29 +41,29 @@ import java.util.stream.Collectors;
* Abstract algorithm provided bean registry.
*/
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
-public abstract class AbstractAlgorithmProvidedBeanRegistry implements
BeanDefinitionRegistryPostProcessor, BeanPostProcessor {
+public abstract class AbstractAlgorithmProvidedBeanRegistry<T extends
ShardingSphereAlgorithm> implements BeanDefinitionRegistryPostProcessor,
BeanPostProcessor {
private final Environment environment;
@SuppressWarnings("all")
- protected void registerBean(final String preFix, final Class clazz, final
BeanDefinitionRegistry registry) {
+ protected void registerBean(final String preFix, final Class<T>
algorithmClass, final BeanDefinitionRegistry registry) {
Map<String, Object> paramMap = PropertyUtil.handle(environment,
preFix, Map.class);
- Set<String> keySet = paramMap.keySet().stream().map(key -> {
+ Set<String> keys = paramMap.keySet().stream().map(key -> {
return key.contains(".") ? key.substring(0, key.indexOf(".")) :
key;
}).collect(Collectors.toSet());
Map<String, YamlShardingSphereAlgorithmConfiguration>
shardingAlgorithmMap = new LinkedHashMap<>();
- keySet.forEach(key -> {
- String type = environment.getProperty(preFix + key + ".type");
- Map<String, Object> propsMap = PropertyUtil.handle(environment,
preFix + key + ".props", Map.class);
+ keys.forEach(each -> {
+ String type = environment.getProperty(preFix + each + ".type");
+ Map<String, Object> propsMap = PropertyUtil.handle(environment,
preFix + each + ".props", Map.class);
YamlShardingSphereAlgorithmConfiguration configuration = new
YamlShardingSphereAlgorithmConfiguration();
configuration.setType(type);
configuration.getProps().putAll(propsMap);
- shardingAlgorithmMap.put(key, configuration);
+ shardingAlgorithmMap.put(each, configuration);
});
- ShardingSphereServiceLoader.register(clazz);
+ ShardingSphereServiceLoader.register(algorithmClass);
shardingAlgorithmMap.forEach((k, v) -> {
- TypedSPI typedSPI = TypedSPIRegistry.getRegisteredService(clazz,
v.getType(), v.getProps());
- BeanDefinitionBuilder builder =
BeanDefinitionBuilder.genericBeanDefinition(typedSPI.getClass());
+ ShardingSphereAlgorithm algorithm =
TypedSPIRegistry.getRegisteredService(algorithmClass, v.getType(),
v.getProps());
+ BeanDefinitionBuilder builder =
BeanDefinitionBuilder.genericBeanDefinition(algorithm.getClass());
builder.addPropertyValue("props", v.getProps());
registry.registerBeanDefinition(k, builder.getBeanDefinition());
});