From 7b0b69295f5133fdf8a24c25cab30f27a53b57e3 Mon Sep 17 00:00:00 2001 From: lingniu Date: Wed, 1 Jul 2026 10:18:00 +0800 Subject: [PATCH] refactor: enforce mysql identity store --- .../VehicleIdentityAutoConfiguration.java | 24 +++++++------------ .../VehicleIdentityAutoConfigurationTest.java | 12 +++++----- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/modules/core/vehicle-identity/src/main/java/com/lingniu/ingest/identity/config/VehicleIdentityAutoConfiguration.java b/modules/core/vehicle-identity/src/main/java/com/lingniu/ingest/identity/config/VehicleIdentityAutoConfiguration.java index a9521b2a..c47f50c0 100644 --- a/modules/core/vehicle-identity/src/main/java/com/lingniu/ingest/identity/config/VehicleIdentityAutoConfiguration.java +++ b/modules/core/vehicle-identity/src/main/java/com/lingniu/ingest/identity/config/VehicleIdentityAutoConfiguration.java @@ -6,12 +6,12 @@ import com.lingniu.ingest.identity.VehicleIdentityRegistry; import com.lingniu.ingest.identity.VehicleIdentityResolver; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.jdbc.datasource.DriverManagerDataSource; import javax.sql.DataSource; +import java.util.Locale; @AutoConfiguration @EnableConfigurationProperties(VehicleIdentityProperties.class) @@ -23,27 +23,21 @@ public class VehicleIdentityAutoConfiguration { return new ObjectMapper(); } - @Bean - @ConditionalOnProperty(prefix = "lingniu.ingest.identity", name = "store", havingValue = "file") - public Object rejectedLegacyFileIdentityStore() { - throw new IllegalStateException("identity.store=file has been removed; configure identity.store=mysql"); - } - - @Bean - @ConditionalOnProperty(prefix = "lingniu.ingest.identity", name = "store", havingValue = "sqlite") - public Object rejectedLegacySqliteIdentityStore() { - throw new IllegalStateException("identity.store=sqlite has been removed; configure identity.store=mysql"); - } - @Bean @ConditionalOnMissingBean({VehicleIdentityResolver.class, VehicleIdentityRegistry.class}) - @ConditionalOnProperty(prefix = "lingniu.ingest.identity", name = "store", havingValue = "mysql", - matchIfMissing = true) public MySqlVehicleIdentityService mysqlVehicleIdentityService(VehicleIdentityProperties properties, ObjectMapper objectMapper) { + requireMysqlStore(properties.getStore()); VehicleIdentityProperties.Mysql mysql = properties.getMysql(); DataSource dataSource = new DriverManagerDataSource( mysql.getJdbcUrl(), mysql.getUsername(), mysql.getPassword()); return new MySqlVehicleIdentityService(dataSource, mysql, objectMapper); } + + private static void requireMysqlStore(String value) { + String store = value == null || value.isBlank() ? "mysql" : value.trim().toLowerCase(Locale.ROOT); + if (!"mysql".equals(store)) { + throw new IllegalStateException("identity.store supports only mysql; configured value: " + value); + } + } } diff --git a/modules/core/vehicle-identity/src/test/java/com/lingniu/ingest/identity/config/VehicleIdentityAutoConfigurationTest.java b/modules/core/vehicle-identity/src/test/java/com/lingniu/ingest/identity/config/VehicleIdentityAutoConfigurationTest.java index a2b04703..438f6f83 100644 --- a/modules/core/vehicle-identity/src/test/java/com/lingniu/ingest/identity/config/VehicleIdentityAutoConfigurationTest.java +++ b/modules/core/vehicle-identity/src/test/java/com/lingniu/ingest/identity/config/VehicleIdentityAutoConfigurationTest.java @@ -51,22 +51,22 @@ class VehicleIdentityAutoConfigurationTest { } @Test - void rejectsLegacyFileStoreMode() { + void rejectsUnsupportedStoreMode() { runner .withPropertyValues("lingniu.ingest.identity.store=file") .run(context -> assertThat(context.getStartupFailure()) .hasRootCauseInstanceOf(IllegalStateException.class) - .hasMessageContaining("identity.store=file has been removed") - .hasMessageContaining("identity.store=mysql")); + .hasMessageContaining("identity.store supports only mysql") + .hasMessageContaining("file")); } @Test - void rejectsLegacySqliteStoreMode() { + void rejectsSqliteStoreModeWithSameMysqlOnlyRule() { runner .withPropertyValues("lingniu.ingest.identity.store=sqlite") .run(context -> assertThat(context.getStartupFailure()) .hasRootCauseInstanceOf(IllegalStateException.class) - .hasMessageContaining("identity.store=sqlite has been removed") - .hasMessageContaining("identity.store=mysql")); + .hasMessageContaining("identity.store supports only mysql") + .hasMessageContaining("sqlite")); } }