diff --git a/src/main/java/org/springframework/data/aerospike/config/AbstractAerospikeDataConfiguration.java b/src/main/java/org/springframework/data/aerospike/config/AbstractAerospikeDataConfiguration.java index cd129843..bdff2148 100644 --- a/src/main/java/org/springframework/data/aerospike/config/AbstractAerospikeDataConfiguration.java +++ b/src/main/java/org/springframework/data/aerospike/config/AbstractAerospikeDataConfiguration.java @@ -47,7 +47,7 @@ public AerospikeTemplate aerospikeTemplate(IAerospikeClient aerospikeClient, QueryEngine queryEngine, IndexRefresher indexRefresher, ServerVersionSupport serverVersionSupport, AerospikeSettings settings) { - return new AerospikeTemplate(aerospikeClient, settings.getConnectionSettings().getNamespace(), + return new AerospikeTemplate(aerospikeClient, settings.getDataSettings().getNamespace(), mappingAerospikeConverter, aerospikeMappingContext, aerospikeExceptionTranslator, queryEngine, indexRefresher, serverVersionSupport); } diff --git a/src/main/java/org/springframework/data/aerospike/config/AbstractReactiveAerospikeDataConfiguration.java b/src/main/java/org/springframework/data/aerospike/config/AbstractReactiveAerospikeDataConfiguration.java index 9eb795d9..4da5b1eb 100644 --- a/src/main/java/org/springframework/data/aerospike/config/AbstractReactiveAerospikeDataConfiguration.java +++ b/src/main/java/org/springframework/data/aerospike/config/AbstractReactiveAerospikeDataConfiguration.java @@ -56,7 +56,7 @@ public ReactiveAerospikeTemplate reactiveAerospikeTemplate(MappingAerospikeConve ReactorIndexRefresher reactorIndexRefresher, ServerVersionSupport serverVersionSupport, AerospikeSettings settings) { - return new ReactiveAerospikeTemplate(aerospikeReactorClient, settings.getConnectionSettings().getNamespace(), + return new ReactiveAerospikeTemplate(aerospikeReactorClient, settings.getDataSettings().getNamespace(), mappingAerospikeConverter, aerospikeMappingContext, aerospikeExceptionTranslator, reactorQueryEngine, reactorIndexRefresher, serverVersionSupport); } diff --git a/src/main/java/org/springframework/data/aerospike/config/AerospikeConnectionSettings.java b/src/main/java/org/springframework/data/aerospike/config/AerospikeConnectionSettings.java index 7da30618..dbb7b569 100644 --- a/src/main/java/org/springframework/data/aerospike/config/AerospikeConnectionSettings.java +++ b/src/main/java/org/springframework/data/aerospike/config/AerospikeConnectionSettings.java @@ -34,8 +34,6 @@ public class AerospikeConnectionSettings { // IPv6: [xxxx::xxxx] // IPv6 addresses must be enclosed by brackets. tlsName is optional. String hosts; - // Namespace - String namespace; // Storing hosts Host[] hostsArray; } diff --git a/src/main/java/org/springframework/data/aerospike/config/AerospikeDataConfigurationSupport.java b/src/main/java/org/springframework/data/aerospike/config/AerospikeDataConfigurationSupport.java index b484c8a4..ee5a81a5 100644 --- a/src/main/java/org/springframework/data/aerospike/config/AerospikeDataConfigurationSupport.java +++ b/src/main/java/org/springframework/data/aerospike/config/AerospikeDataConfigurationSupport.java @@ -16,6 +16,7 @@ package org.springframework.data.aerospike.config; import com.aerospike.client.AerospikeClient; +import com.aerospike.client.AerospikeException; import com.aerospike.client.Host; import com.aerospike.client.IAerospikeClient; import com.aerospike.client.async.EventLoops; @@ -29,6 +30,8 @@ import io.netty.channel.kqueue.KQueueEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.BeanInstantiationException; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; @@ -70,9 +73,9 @@ @Configuration public abstract class AerospikeDataConfigurationSupport { - public static final String CONFIG_PREFIX = "spring.aerospike"; - public static final String CONFIG_PREFIX_DATA = CONFIG_PREFIX + ".data"; - public static final String CONFIG_PREFIX_CONNECTION = CONFIG_PREFIX + ".connection"; + public static final String CONFIG_PREFIX = "spring"; + public static final String CONFIG_PREFIX_DATA = CONFIG_PREFIX + ".data.aerospike"; + public static final String CONFIG_PREFIX_CONNECTION = CONFIG_PREFIX + ".aerospike"; @Bean(name = "aerospikeStatementBuilder") public StatementBuilder statementBuilder(IndexesCache indexesCache) { @@ -99,8 +102,11 @@ public MappingAerospikeConverter mappingAerospikeConverter(AerospikeMappingConte } @Bean(name = "aerospikeTypeAliasAccessor") - public AerospikeTypeAliasAccessor aerospikeTypeAliasAccessor() { - return new AerospikeTypeAliasAccessor(); + public AerospikeTypeAliasAccessor aerospikeTypeAliasAccessor(AerospikeDataSettings dataSettings) { + String classKey = dataSettings.getClassKey(); + return StringUtils.hasText(classKey) + ? new AerospikeTypeAliasAccessor(classKey) + : new AerospikeTypeAliasAccessor(); } @Bean(name = "aerospikeCustomConversions") @@ -113,11 +119,26 @@ protected List customConverters() { } @Bean(name = "aerospikeMappingContext") - public AerospikeMappingContext aerospikeMappingContext() throws ClassNotFoundException { + public AerospikeMappingContext aerospikeMappingContext(AerospikeDataSettings dataSettings) + throws AerospikeException + { AerospikeMappingContext context = new AerospikeMappingContext(); - context.setInitialEntitySet(getInitialEntitySet()); + try { + context.setInitialEntitySet(getInitialEntitySet()); + } catch (ClassNotFoundException e) { + throw new AerospikeException("Cannot set initialEntitySet in AerospikeMappingContext", e); + } context.setSimpleTypeHolder(AerospikeSimpleTypes.HOLDER); - context.setFieldNamingStrategy(fieldNamingStrategy()); + if (dataSettings.getFieldNamingStrategy() != null) { + try { + context.setFieldNamingStrategy( + (FieldNamingStrategy) BeanUtils.instantiateClass(dataSettings.getFieldNamingStrategy())); + } catch (BeanInstantiationException e) { + throw new AerospikeException("Cannot set fieldNamingStrategy in AerospikeMappingContext", e); + } + } else { + context.setFieldNamingStrategy(fieldNamingStrategy()); + } return context; } @@ -294,7 +315,7 @@ protected AerospikeSettings aerospikeSettings(AerospikeDataSettings dataSettings // nameSpace() has precedence over namespace parameter from application.properties String namespace; - if ((namespace = nameSpace()) != null) connectionSettings.setNamespace(namespace); + if ((namespace = nameSpace()) != null) dataSettings.setNamespace(namespace); return new AerospikeSettings(connectionSettings, dataSettings); } diff --git a/src/main/java/org/springframework/data/aerospike/config/AerospikeDataSettings.java b/src/main/java/org/springframework/data/aerospike/config/AerospikeDataSettings.java index a300629e..8978ab99 100644 --- a/src/main/java/org/springframework/data/aerospike/config/AerospikeDataSettings.java +++ b/src/main/java/org/springframework/data/aerospike/config/AerospikeDataSettings.java @@ -26,6 +26,8 @@ @ConfigurationProperties(prefix = CONFIG_PREFIX_DATA) public class AerospikeDataSettings { + // Namespace + String namespace; // Enable scan operation boolean scansEnabled = false; // Create secondary indexes specified using `@Indexed` annotation on startup @@ -45,4 +47,8 @@ public class AerospikeDataSettings { // Writing unsorted maps (false) degrades performance of Map-related operations and does not allow comparing Maps, // strongly recommended not to use except during upgrade from older versions of Spring Data Aerospike (if required) boolean writeSortedMaps = true; + // Name for the bin to store entity's class + private String classKey = "@_class"; + // Fully qualified name of the FieldNamingStrategy for entities + private Class fieldNamingStrategy; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 5fd8ecfa..b08a28e5 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,9 +1,9 @@ -spring.aerospike.connection.hosts=localhost:3000 -spring.aerospike.connection.namespace=test -spring.aerospike.data.scans-enabled=false -spring.aerospike.data.create-indexes-on-startup=true -spring.aerospike.data.index-cache-refresh-seconds=3600 -spring.aerospike.data.server-version-refresh-seconds=3600 -spring.aerospike.data.query-max-records=10000 -spring.aerospike.data.batch-write-size=100 -spring.aerospike.data.keep-original-key-types=false \ No newline at end of file +spring.aerospike.hosts=localhost:3000 +spring.data.aerospike.namespace=test +spring.data.aerospike.scans-enabled=false +spring.data.aerospike.create-indexes-on-startup=true +spring.data.aerospike.index-cache-refresh-seconds=3600 +spring.data.aerospike.server-version-refresh-seconds=3600 +spring.data.aerospike.query-max-records=10000 +spring.data.aerospike.batch-write-size=100 +spring.data.aerospike.keep-original-key-types=false \ No newline at end of file diff --git a/src/test/java/org/springframework/data/aerospike/BaseIntegrationTests.java b/src/test/java/org/springframework/data/aerospike/BaseIntegrationTests.java index b27c20ee..08cda321 100644 --- a/src/test/java/org/springframework/data/aerospike/BaseIntegrationTests.java +++ b/src/test/java/org/springframework/data/aerospike/BaseIntegrationTests.java @@ -16,7 +16,7 @@ public abstract class BaseIntegrationTests { public static final String DIFFERENT_EXISTING_CACHE = "DIFFERENT-EXISTING-CACHE"; protected static final int MILLIS_TO_NANO = 1_000_000; - @Value("${spring.aerospike.connection.namespace}") + @Value("${spring.data.aerospike.namespace}") protected String namespace; protected String id; diff --git a/src/test/java/org/springframework/data/aerospike/config/CommonTestConfig.java b/src/test/java/org/springframework/data/aerospike/config/CommonTestConfig.java index b7761791..9b98f5ad 100644 --- a/src/test/java/org/springframework/data/aerospike/config/CommonTestConfig.java +++ b/src/test/java/org/springframework/data/aerospike/config/CommonTestConfig.java @@ -43,7 +43,7 @@ @EnableAutoConfiguration public class CommonTestConfig { - @Value("${spring.aerospike.connection.namespace}") + @Value("${spring.data.aerospike.namespace}") protected String namespace; @Bean diff --git a/src/test/java/org/springframework/data/aerospike/index/IndexNotScheduledCacheRefreshTest.java b/src/test/java/org/springframework/data/aerospike/index/IndexNotScheduledCacheRefreshTest.java index 7136cf24..ad9173cb 100644 --- a/src/test/java/org/springframework/data/aerospike/index/IndexNotScheduledCacheRefreshTest.java +++ b/src/test/java/org/springframework/data/aerospike/index/IndexNotScheduledCacheRefreshTest.java @@ -15,7 +15,7 @@ @Slf4j @ContextConfiguration -@TestPropertySource(properties = {"spring.aerospike.data.server-version-refresh-seconds = 0"}) +@TestPropertySource(properties = {"spring.data.aerospike.server-version-refresh-seconds = 0"}) public class IndexNotScheduledCacheRefreshTest extends BaseBlockingIntegrationTests { String setName = "scheduled"; diff --git a/src/test/java/org/springframework/data/aerospike/index/IndexScheduledCacheRefreshTest.java b/src/test/java/org/springframework/data/aerospike/index/IndexScheduledCacheRefreshTest.java index f6e038a8..88932a4d 100644 --- a/src/test/java/org/springframework/data/aerospike/index/IndexScheduledCacheRefreshTest.java +++ b/src/test/java/org/springframework/data/aerospike/index/IndexScheduledCacheRefreshTest.java @@ -15,7 +15,7 @@ @Slf4j @ContextConfiguration -@TestPropertySource(properties = {"spring.aerospike.data.index-cache-refresh-seconds=4"}) +@TestPropertySource(properties = {"spring.data.aerospike.index-cache-refresh-seconds=4"}) public class IndexScheduledCacheRefreshTest extends BaseBlockingIntegrationTests { String setName = "scheduled"; diff --git a/src/test/java/org/springframework/data/aerospike/index/IndexedAnnotationTests.java b/src/test/java/org/springframework/data/aerospike/index/IndexedAnnotationTests.java index 13d7869c..88dff1d7 100644 --- a/src/test/java/org/springframework/data/aerospike/index/IndexedAnnotationTests.java +++ b/src/test/java/org/springframework/data/aerospike/index/IndexedAnnotationTests.java @@ -16,7 +16,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; -@TestPropertySource(properties = {"spring.aerospike.data.create-indexes-on-startup=true"}) +@TestPropertySource(properties = {"spring.data.aerospike.create-indexes-on-startup=true"}) // this test class requires secondary indexes created on startup public class IndexedAnnotationTests extends BaseBlockingIntegrationTests { diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 58802357..5fe4c85d 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -1,9 +1,9 @@ -spring.aerospike.connection.hosts=${embedded.aerospike.host}:${embedded.aerospike.port} -spring.aerospike.connection.namespace=${embedded.aerospike.namespace} -spring.aerospike.data.scans-enabled=true -spring.aerospike.data.create-indexes-on-startup=false -spring.aerospike.data.index-cache-refresh-seconds=0 -spring.aerospike.data.server-version-refresh-seconds=0 -spring.aerospike.data.query-max-records=5000 -spring.aerospike.data.batch-write-size=100 -spring.aerospike.data.keep-original-key-types=false \ No newline at end of file +spring.aerospike.hosts=${embedded.aerospike.host}:${embedded.aerospike.port} +spring.data.aerospike.namespace=${embedded.aerospike.namespace} +spring.data.aerospike.scans-enabled=true +spring.data.aerospike.create-indexes-on-startup=false +spring.data.aerospike.index-cache-refresh-seconds=0 +spring.data.aerospike.server-version-refresh-seconds=0 +spring.data.aerospike.query-max-records=5000 +spring.data.aerospike.batch-write-size=100 +spring.data.aerospike.keep-original-key-types=false \ No newline at end of file