From 0d963934ac0b4c390e1048eeba3230344cad0dc8 Mon Sep 17 00:00:00 2001 From: Ling Hengqian Date: Wed, 6 Sep 2023 11:13:11 +0800 Subject: [PATCH] Open and Fix NativeTest by Spring Framework under GraalVM CE (#562) --- .github/workflows/ci.yml | 3 +-- CONTRIBUTING.md | 2 +- .../DynamicDataSourceAutoConfiguration.java | 4 ++-- .../DynamicDataSourceAutoConfiguration.java | 5 +++-- .../datasource/DynamicRoutingDataSource.java | 14 +++++++++----- pom.xml | 13 +++---------- 6 files changed, 19 insertions(+), 22 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 00332118..7957a2cf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,6 @@ env: MAVEN_OPTS: -Dhttps.protocols=TLSv1.2 -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true jobs: - # TODO test-graalvm-ce-ci: name: NativeTest CI - GraalVM CE ${{ matrix.java-version }} on ${{ matrix.os }} (This CI failure is reasonable) runs-on: ${{ matrix.os }} @@ -45,7 +44,7 @@ jobs: - name: Build Spring Boot Starter 3 test with Maven run: | ./mvnw -T1C -B -PgenerateMetadata -DskipNativeTests clean test -# ./mvnw -am -pl dynamic-datasource-spring-boot3-starter -PnativeTestInSpringBoot T1C -B clean test + ./mvnw -am -pl dynamic-datasource-spring-boot3-starter -PnativeTestInSpringBoot -T1C -B clean test test-maximum-jdk-ci: name: Test CI - JDK ${{ matrix.java-version }} on ${{ matrix.os }} runs-on: ${{ matrix.os }} diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index cecd5676..8ef0abc6 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -52,7 +52,7 @@ cd ./dynamic-datasource/ ./mvnw -am -pl dynamic-datasource-spring-boot3-starter -PnativeTestInSpringBoot -T1C -B clean test ``` -贡献者在提交 PR 后,位于 Github Actions 的 CI 将进行此验证。相关 CI 的失败行为是现阶段是可接受的。 +贡献者在提交 PR 后,位于 Github Actions 的 CI 将进行此验证。 请不要为 SPEL 功能编写可能的 nativeTest,参考 https://github.com/spring-projects/spring-framework/issues/29548 。如有需要, 请使用 `org.graalvm.nativeimage.imagecode` 的 System Property 屏蔽相关测试在 GraalVM Native Image 下运行。 diff --git a/dynamic-datasource-spring-boot-starter/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAutoConfiguration.java b/dynamic-datasource-spring-boot-starter/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAutoConfiguration.java index 4d007d68..d53c972d 100644 --- a/dynamic-datasource-spring-boot-starter/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAutoConfiguration.java +++ b/dynamic-datasource-spring-boot-starter/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAutoConfiguration.java @@ -64,8 +64,8 @@ public DynamicDataSourceAutoConfiguration( @Bean @ConditionalOnMissingBean - public DataSource dataSource() { - DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource(); + public DataSource dataSource(List providers) { + DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource(providers); dataSource.setPrimary(properties.getPrimary()); dataSource.setStrict(properties.getStrict()); dataSource.setStrategy(properties.getStrategy()); diff --git a/dynamic-datasource-spring-boot3-starter/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAutoConfiguration.java b/dynamic-datasource-spring-boot3-starter/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAutoConfiguration.java index e2c72b34..27253340 100644 --- a/dynamic-datasource-spring-boot3-starter/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAutoConfiguration.java +++ b/dynamic-datasource-spring-boot3-starter/src/main/java/com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAutoConfiguration.java @@ -16,6 +16,7 @@ package com.baomidou.dynamic.datasource.spring.boot.autoconfigure; import com.baomidou.dynamic.datasource.DynamicRoutingDataSource; +import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.ObjectProvider; @@ -64,8 +65,8 @@ public DynamicDataSourceAutoConfiguration( @Bean @ConditionalOnMissingBean - public DataSource dataSource() { - DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource(); + public DataSource dataSource(List providers) { + DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource(providers); dataSource.setPrimary(properties.getPrimary()); dataSource.setStrict(properties.getStrict()); dataSource.setStrategy(properties.getStrategy()); 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 87292521..d719e85d 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 @@ -29,7 +29,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; @@ -48,6 +48,7 @@ * @since 1.0.0 */ @Slf4j +@Component public class DynamicRoutingDataSource extends AbstractRoutingDataSource implements InitializingBean, DisposableBean { private static final String UNDERLINE = "_"; @@ -59,8 +60,7 @@ public class DynamicRoutingDataSource extends AbstractRoutingDataSource implemen * 分组数据库 */ private final Map groupDataSources = new ConcurrentHashMap<>(); - @Autowired - private List providers; + private final List providers; @Setter private Class strategy = LoadBalanceDynamicDataSourceStrategy.class; @Setter @@ -72,6 +72,10 @@ public class DynamicRoutingDataSource extends AbstractRoutingDataSource implemen @Setter private Boolean seata = false; + public DynamicRoutingDataSource(List providers) { + this.providers = providers; + } + @Override protected String getPrimary() { return primary; @@ -207,7 +211,7 @@ public synchronized void removeDataSource(String ds) { } @Override - public void destroy() throws Exception { + public void destroy() { log.info("dynamic-datasource start closing ...."); for (Map.Entry item : dataSourceMap.entrySet()) { closeDataSource(item.getKey(), item.getValue()); @@ -216,7 +220,7 @@ public void destroy() throws Exception { } @Override - public void afterPropertiesSet() throws Exception { + public void afterPropertiesSet() { // 检查开启了配置但没有相关依赖 checkEnv(); // 添加并分组数据源 diff --git a/pom.xml b/pom.xml index 719d4f6f..80031aa2 100644 --- a/pom.xml +++ b/pom.xml @@ -378,9 +378,9 @@ maven-surefire-plugin ${maven-surefire-plugin.version} - - com.baomidou.dynamic.datasource.fixture.v1.** - + + com.baomidou.dynamic.datasource.fixture.v3.** + --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED @@ -429,13 +429,6 @@ nativeTestInSpringBoot - - - org.junit.platform - junit-platform-launcher - test - -