From d54f06fa987a368ed8f7b6825be02328f85dc09d Mon Sep 17 00:00:00 2001 From: huayanYu Date: Mon, 23 Oct 2023 21:29:36 +0800 Subject: [PATCH] use DsStrUtils --- .../dynamic/datasource/toolkit/DsStrUtils.java | 4 ++++ dynamic-datasource-spring-boot3-starter/pom.xml | 3 ++- .../datasource/DynamicRoutingDataSource.java | 6 +++--- .../creator/DefaultDataSourceCreator.java | 10 +++++----- .../datasource/ds/AbstractRoutingDataSource.java | 10 +++++----- .../datasource/event/EncDataSourceInitEvent.java | 6 +++--- .../provider/AbstractJdbcDataSourceProvider.java | 6 +++--- .../support/DataSourceClassResolver.java | 4 ++-- .../dynamic/datasource/support/ScriptRunner.java | 4 ++-- .../toolkit/DynamicDataSourceContextHolder.java | 3 +-- .../dynamic/datasource/tx/LocalTxUtil.java | 11 +++-------- .../dynamic/datasource/tx/TransactionContext.java | 6 +++--- .../datasource/tx/TransactionalTemplate.java | 6 +++--- pom.xml | 14 +++++++------- 14 files changed, 46 insertions(+), 47 deletions(-) diff --git a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/toolkit/DsStrUtils.java b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/toolkit/DsStrUtils.java index 7b8b55d7..6032ecff 100644 --- a/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/toolkit/DsStrUtils.java +++ b/dynamic-datasource-creator/src/main/java/com/baomidou/dynamic/datasource/toolkit/DsStrUtils.java @@ -22,6 +22,10 @@ */ public abstract class DsStrUtils { + public static boolean isEmpty(String str) { + return str == null || str.isEmpty(); + } + /** * 判断字符串是否为空 * diff --git a/dynamic-datasource-spring-boot3-starter/pom.xml b/dynamic-datasource-spring-boot3-starter/pom.xml index 65551af6..0c9ee64a 100644 --- a/dynamic-datasource-spring-boot3-starter/pom.xml +++ b/dynamic-datasource-spring-boot3-starter/pom.xml @@ -16,7 +16,7 @@ 17 - 3.1.3 + 3.1.5 @@ -66,6 +66,7 @@ org.springframework.boot spring-boot-maven-plugin + ${spring-boot-dependencies.version} org.graalvm.buildtools diff --git a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/DynamicRoutingDataSource.java b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/DynamicRoutingDataSource.java index 92521be5..aeac486d 100644 --- a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/DynamicRoutingDataSource.java +++ b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/DynamicRoutingDataSource.java @@ -24,6 +24,7 @@ import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider; import com.baomidou.dynamic.datasource.strategy.DynamicDataSourceStrategy; import com.baomidou.dynamic.datasource.strategy.LoadBalanceDynamicDataSourceStrategy; +import com.baomidou.dynamic.datasource.toolkit.DsStrUtils; import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; import com.p6spy.engine.spy.P6DataSource; import io.seata.rm.datasource.DataSourceProxy; @@ -32,7 +33,6 @@ import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Component; -import org.springframework.util.StringUtils; import javax.sql.DataSource; import java.lang.reflect.Field; @@ -128,7 +128,7 @@ public Map getGroupDataSources() { * @return 数据源 */ public DataSource getDataSource(String ds) { - if (StringUtils.isEmpty(ds)) { + if (DsStrUtils.isEmpty(ds)) { return determinePrimaryDataSource(); } else if (!groupDataSources.isEmpty() && groupDataSources.containsKey(ds)) { log.debug("dynamic-datasource switch to the datasource named [{}]", ds); @@ -188,7 +188,7 @@ private void addGroupDataSource(String ds, DataSource dataSource) { * @param ds 数据源名称 */ public synchronized void removeDataSource(String ds) { - if (!StringUtils.hasText(ds)) { + if (!DsStrUtils.hasText(ds)) { throw new RuntimeException("remove parameter could not be empty"); } if (primary.equals(ds)) { diff --git a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/creator/DefaultDataSourceCreator.java b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/creator/DefaultDataSourceCreator.java index fe5a8fd8..51138105 100644 --- a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/creator/DefaultDataSourceCreator.java +++ b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/creator/DefaultDataSourceCreator.java @@ -20,12 +20,12 @@ import com.baomidou.dynamic.datasource.event.DataSourceInitEvent; import com.baomidou.dynamic.datasource.support.ScriptRunner; import com.baomidou.dynamic.datasource.toolkit.CryptoUtils; +import com.baomidou.dynamic.datasource.toolkit.DsStrUtils; import com.p6spy.engine.spy.P6DataSource; import io.seata.rm.datasource.DataSourceProxy; import io.seata.rm.datasource.xa.DataSourceProxyXA; import lombok.Setter; import lombok.extern.slf4j.Slf4j; -import org.springframework.util.StringUtils; import javax.sql.DataSource; import java.util.List; @@ -84,7 +84,7 @@ public DataSource createDataSource(DataSourceProperty dataSourceProperty) { throw new IllegalStateException("creator must not be null,please check the DataSourceCreator"); } String propertyPublicKey = dataSourceProperty.getPublicKey(); - if (StringUtils.isEmpty(propertyPublicKey)) { + if (DsStrUtils.isEmpty(propertyPublicKey)) { dataSourceProperty.setPublicKey(publicKey); } Boolean propertyLazy = dataSourceProperty.getLazy(); @@ -112,12 +112,12 @@ private void runScrip(DataSource dataSource, DataSourceProperty dataSourceProper DatasourceInitProperties initProperty = dataSourceProperty.getInit(); String schema = initProperty.getSchema(); String data = initProperty.getData(); - if (StringUtils.hasText(schema) || StringUtils.hasText(data)) { + if (DsStrUtils.hasText(schema) || DsStrUtils.hasText(data)) { ScriptRunner scriptRunner = new ScriptRunner(initProperty.isContinueOnError(), initProperty.getSeparator()); - if (StringUtils.hasText(schema)) { + if (DsStrUtils.hasText(schema)) { scriptRunner.runScript(dataSource, schema); } - if (StringUtils.hasText(data)) { + if (DsStrUtils.hasText(data)) { scriptRunner.runScript(dataSource, data); } } diff --git a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/ds/AbstractRoutingDataSource.java b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/ds/AbstractRoutingDataSource.java index a0a09ff7..278cde10 100644 --- a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/ds/AbstractRoutingDataSource.java +++ b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/ds/AbstractRoutingDataSource.java @@ -15,11 +15,11 @@ */ package com.baomidou.dynamic.datasource.ds; +import com.baomidou.dynamic.datasource.toolkit.DsStrUtils; import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; import com.baomidou.dynamic.datasource.tx.ConnectionFactory; import com.baomidou.dynamic.datasource.tx.ConnectionProxy; import com.baomidou.dynamic.datasource.tx.TransactionContext; -import org.springframework.util.StringUtils; import javax.sql.DataSource; import java.sql.Connection; @@ -50,11 +50,11 @@ public abstract class AbstractRoutingDataSource extends AbstractDataSource { @Override public Connection getConnection() throws SQLException { String xid = TransactionContext.getXID(); - if (StringUtils.isEmpty(xid)) { + if (DsStrUtils.isEmpty(xid)) { return determineDataSource().getConnection(); } else { String ds = DynamicDataSourceContextHolder.peek(); - ds = StringUtils.isEmpty(ds) ? getPrimary() : ds; + ds = DsStrUtils.isEmpty(ds) ? getPrimary() : ds; ConnectionProxy connection = ConnectionFactory.getConnection(xid, ds); return connection == null ? getConnectionProxy(xid, ds, determineDataSource().getConnection()) : connection; } @@ -63,11 +63,11 @@ public Connection getConnection() throws SQLException { @Override public Connection getConnection(String username, String password) throws SQLException { String xid = TransactionContext.getXID(); - if (StringUtils.isEmpty(xid)) { + if (DsStrUtils.isEmpty(xid)) { return determineDataSource().getConnection(username, password); } else { String ds = DynamicDataSourceContextHolder.peek(); - ds = StringUtils.isEmpty(ds) ? getPrimary() : ds; + ds = DsStrUtils.isEmpty(ds) ? getPrimary() : ds; ConnectionProxy connection = ConnectionFactory.getConnection(xid, ds); return connection == null ? getConnectionProxy(xid, ds, determineDataSource().getConnection(username, password)) : connection; diff --git a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/event/EncDataSourceInitEvent.java b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/event/EncDataSourceInitEvent.java index aa61caf7..f97e9a1e 100644 --- a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/event/EncDataSourceInitEvent.java +++ b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/event/EncDataSourceInitEvent.java @@ -17,8 +17,8 @@ import com.baomidou.dynamic.datasource.creator.DataSourceProperty; import com.baomidou.dynamic.datasource.toolkit.CryptoUtils; +import com.baomidou.dynamic.datasource.toolkit.DsStrUtils; import lombok.extern.slf4j.Slf4j; -import org.springframework.util.StringUtils; import javax.sql.DataSource; import java.util.regex.Matcher; @@ -40,7 +40,7 @@ public class EncDataSourceInitEvent implements DataSourceInitEvent { @Override public void beforeCreate(DataSourceProperty dataSourceProperty) { String publicKey = dataSourceProperty.getPublicKey(); - if (StringUtils.hasText(publicKey)) { + if (DsStrUtils.hasText(publicKey)) { dataSourceProperty.setUrl(decrypt(publicKey, dataSourceProperty.getUrl())); dataSourceProperty.setUsername(decrypt(publicKey, dataSourceProperty.getUsername())); dataSourceProperty.setPassword(decrypt(publicKey, dataSourceProperty.getPassword())); @@ -56,7 +56,7 @@ public void afterCreate(DataSource dataSource) { * 字符串解密 */ private String decrypt(String publicKey, String cipherText) { - if (StringUtils.hasText(cipherText)) { + if (DsStrUtils.hasText(cipherText)) { Matcher matcher = ENC_PATTERN.matcher(cipherText); if (matcher.find()) { try { diff --git a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/provider/AbstractJdbcDataSourceProvider.java b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/provider/AbstractJdbcDataSourceProvider.java index fee36274..c1c54c67 100644 --- a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/provider/AbstractJdbcDataSourceProvider.java +++ b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/provider/AbstractJdbcDataSourceProvider.java @@ -17,8 +17,8 @@ import com.baomidou.dynamic.datasource.creator.DataSourceProperty; import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator; +import com.baomidou.dynamic.datasource.toolkit.DsStrUtils; import lombok.extern.slf4j.Slf4j; -import org.springframework.util.StringUtils; import javax.sql.DataSource; import java.sql.Connection; @@ -106,7 +106,7 @@ public Map loadDataSources() { try { // 由于 SPI 的支持,现在已无需显示加载驱动了 // 但在用户显示配置的情况下,进行主动加载 - if (!StringUtils.isEmpty(driverClassName)) { + if (!DsStrUtils.isEmpty(driverClassName)) { Class.forName(driverClassName); log.info("成功加载数据库驱动程序"); } @@ -116,7 +116,7 @@ public Map loadDataSources() { Map dataSourcePropertiesMap = executeStmt(stmt); return createDataSourceMap(dataSourcePropertiesMap); } catch (Exception e) { - e.printStackTrace(); + log.error("loadDataSources error", e); } finally { closeResource(conn); closeResource(stmt); diff --git a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/support/DataSourceClassResolver.java b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/support/DataSourceClassResolver.java index 1e4acc33..2222b94f 100644 --- a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/support/DataSourceClassResolver.java +++ b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/support/DataSourceClassResolver.java @@ -274,7 +274,7 @@ private BasicAttribute findDataSourceAttribute(AnnotatedElement ae, Class //AnnotatedElementUtils.findMergedAnnotation()会委托给findMergedAnnotationAttributes() DS ds = AnnotatedElementUtils.findMergedAnnotation(ae, DS.class); if (ds != null) { - return (BasicAttribute) new BasicAttribute<>(ds.value()); + return new BasicAttribute(ds.value()); } } else if (annotation.isAssignableFrom(DSTransactional.class)) { DSTransactional dsTransactional = AnnotatedElementUtils.findMergedAnnotation(ae, DSTransactional.class); @@ -283,7 +283,7 @@ private BasicAttribute findDataSourceAttribute(AnnotatedElement ae, Class transactionalInfo.setPropagation(dsTransactional.propagation()); transactionalInfo.setRollbackFor(dsTransactional.rollbackFor()); transactionalInfo.setNoRollbackFor(dsTransactional.noRollbackFor()); - return (BasicAttribute) new BasicAttribute(transactionalInfo); + return new BasicAttribute(transactionalInfo); } } return null; diff --git a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/support/ScriptRunner.java b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/support/ScriptRunner.java index 302fb031..a09d3b8b 100644 --- a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/support/ScriptRunner.java +++ b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/support/ScriptRunner.java @@ -15,6 +15,7 @@ */ package com.baomidou.dynamic.datasource.support; +import com.baomidou.dynamic.datasource.toolkit.DsStrUtils; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; @@ -22,7 +23,6 @@ import org.springframework.dao.DataAccessException; import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; -import org.springframework.util.StringUtils; import javax.sql.DataSource; @@ -50,7 +50,7 @@ public class ScriptRunner { * @param location 脚本位置 */ public void runScript(DataSource dataSource, String location) { - if (StringUtils.hasText(location)) { + if (DsStrUtils.hasText(location)) { ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); populator.setContinueOnError(continueOnError); populator.setSeparator(separator); diff --git a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/toolkit/DynamicDataSourceContextHolder.java b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/toolkit/DynamicDataSourceContextHolder.java index d497f337..6ab8c4ac 100644 --- a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/toolkit/DynamicDataSourceContextHolder.java +++ b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/toolkit/DynamicDataSourceContextHolder.java @@ -16,7 +16,6 @@ package com.baomidou.dynamic.datasource.toolkit; import org.springframework.core.NamedThreadLocal; -import org.springframework.util.StringUtils; import java.util.ArrayDeque; import java.util.Deque; @@ -66,7 +65,7 @@ public static String peek() { * @return 数据源名称 */ public static String push(String ds) { - String dataSourceStr = StringUtils.isEmpty(ds) ? "" : ds; + String dataSourceStr = DsStrUtils.isEmpty(ds) ? "" : ds; LOOKUP_KEY_HOLDER.get().push(dataSourceStr); return dataSourceStr; } diff --git a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/tx/LocalTxUtil.java b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/tx/LocalTxUtil.java index 4766172a..fadbf630 100644 --- a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/tx/LocalTxUtil.java +++ b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/tx/LocalTxUtil.java @@ -15,8 +15,8 @@ */ package com.baomidou.dynamic.datasource.tx; +import com.baomidou.dynamic.datasource.toolkit.DsStrUtils; import lombok.extern.slf4j.Slf4j; -import org.springframework.util.StringUtils; import java.security.SecureRandom; import java.util.UUID; @@ -33,12 +33,7 @@ public final class LocalTxUtil { /** * SecureRandom instance used to generate UUIDs. */ - private static final ThreadLocal SECURE_RANDOM_HOLDER = new ThreadLocal() { - @Override - protected SecureRandom initialValue() { - return new SecureRandom(); - } - }; + private static final ThreadLocal SECURE_RANDOM_HOLDER = ThreadLocal.withInitial(SecureRandom::new); /** * 随机生成UUID @@ -75,7 +70,7 @@ public static UUID randomUUID() { */ public static String startTransaction() { String xid = TransactionContext.getXID(); - if (!StringUtils.isEmpty(xid)) { + if (!DsStrUtils.isEmpty(xid)) { log.debug("dynamic-datasource exist local tx [{}]", xid); } else { xid = randomUUID().toString(); diff --git a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/tx/TransactionContext.java b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/tx/TransactionContext.java index b09cfe95..fb0ab475 100644 --- a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/tx/TransactionContext.java +++ b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/tx/TransactionContext.java @@ -15,9 +15,9 @@ */ package com.baomidou.dynamic.datasource.tx; +import com.baomidou.dynamic.datasource.toolkit.DsStrUtils; import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.transaction.support.TransactionSynchronization; -import org.springframework.util.StringUtils; import java.util.*; @@ -28,7 +28,7 @@ public class TransactionContext { private static final ThreadLocal CONTEXT_HOLDER = new ThreadLocal<>(); private static final ThreadLocal> SYNCHRONIZATION_HOLDER = - ThreadLocal.withInitial(() -> new LinkedHashSet<>()); + ThreadLocal.withInitial(LinkedHashSet::new); /** * Gets xid. @@ -37,7 +37,7 @@ public class TransactionContext { */ public static String getXID() { String xid = CONTEXT_HOLDER.get(); - if (!StringUtils.isEmpty(xid)) { + if (DsStrUtils.hasText(xid)) { return xid; } return null; diff --git a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/tx/TransactionalTemplate.java b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/tx/TransactionalTemplate.java index 7f88aadf..f9815544 100644 --- a/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/tx/TransactionalTemplate.java +++ b/dynamic-datasource-spring/src/main/java/com/baomidou/dynamic/datasource/tx/TransactionalTemplate.java @@ -16,9 +16,9 @@ package com.baomidou.dynamic.datasource.tx; import com.baomidou.dynamic.datasource.exception.TransactionException; +import com.baomidou.dynamic.datasource.toolkit.DsStrUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.transaction.support.TransactionSynchronization; -import org.springframework.util.StringUtils; import java.util.Objects; @@ -107,7 +107,7 @@ public Object execute(TransactionalExecutor transactionalExecutor) throws Throwa private Object doExecute(TransactionalExecutor transactionalExecutor) throws Throwable { TransactionalInfo transactionInfo = transactionalExecutor.getTransactionInfo(); DsPropagation propagation = transactionInfo.propagation; - if (!StringUtils.isEmpty(TransactionContext.getXID()) && !propagation.equals(DsPropagation.NESTED)) { + if (!DsStrUtils.isEmpty(TransactionContext.getXID()) && !propagation.equals(DsPropagation.NESTED)) { return transactionalExecutor.execute(); } boolean state = true; @@ -215,7 +215,7 @@ public SuspendedResourcesHolder suspend() { * @return 是否存在事务 */ public boolean existingTransaction() { - return !StringUtils.isEmpty(TransactionContext.getXID()); + return !DsStrUtils.isEmpty(TransactionContext.getXID()); } /** diff --git a/pom.xml b/pom.xml index 7523818c..81860c2c 100644 --- a/pom.xml +++ b/pom.xml @@ -54,13 +54,13 @@ 2.7.15 3.5.3.2 2.4.13 - 1.2.19 - 3.4.1 - 2.9.0 + 1.2.20 + 3.4.2 + 2.10.0 3.9.1 1.4.2 1.18.30 - 2.2.222 + 2.2.224 4.0.6 5.10.0 @@ -69,13 +69,13 @@ 3.3.1 3.1.2 3.11.0 - 3.5.0 + 3.6.0 3.3.0 - 4.2 + 4.3 3.1.0 1.6.13 - 0.9.27 + 0.9.28