Polish "Add reactive support for Spring Data Cassandra"
Closes gh-8568
This commit is contained in:
parent
abd35f041b
commit
47037d5227
|
@ -18,11 +18,11 @@ package org.springframework.boot.autoconfigure.data.cassandra;
|
|||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.Session;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
|
@ -43,29 +43,29 @@ import org.springframework.data.cassandra.core.ReactiveCassandraTemplate;
|
|||
* @since 2.0.0
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Cluster.class, ReactiveCassandraTemplate.class })
|
||||
@ConditionalOnClass({ Cluster.class, ReactiveCassandraTemplate.class, Flux.class })
|
||||
@ConditionalOnBean(Session.class)
|
||||
@AutoConfigureAfter(CassandraAutoConfiguration.class)
|
||||
@AutoConfigureAfter(CassandraDataAutoConfiguration.class)
|
||||
public class ReactiveCassandraDataAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(ReactiveSession.class)
|
||||
public ReactiveSession rectiveSession(Session session)
|
||||
throws Exception {
|
||||
public ReactiveSession reactiveCassandraSession(Session session) {
|
||||
return new DefaultBridgedReactiveSession(session, Schedulers.elastic());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ReactiveSessionFactory reactiveSessionFactory(ReactiveSession reactiveSession)
|
||||
throws Exception {
|
||||
return new DefaultReactiveSessionFactory(reactiveSession);
|
||||
public ReactiveSessionFactory reactiveCassandraSessionFactory(
|
||||
ReactiveSession reactiveCassandraSession) {
|
||||
return new DefaultReactiveSessionFactory(reactiveCassandraSession);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public ReactiveCassandraTemplate reactiveCassandraTemplate(ReactiveSession session,
|
||||
CassandraConverter converter) throws Exception {
|
||||
return new ReactiveCassandraTemplate(session, converter);
|
||||
public ReactiveCassandraTemplate reactiveCassandraTemplate(
|
||||
ReactiveSession reactiveCassandraSession,
|
||||
CassandraConverter converter) {
|
||||
return new ReactiveCassandraTemplate(reactiveCassandraSession, converter);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.data.cassandra;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
|
@ -32,14 +33,15 @@ import org.springframework.data.cassandra.repository.support.ReactiveCassandraRe
|
|||
* Reactive Repositories.
|
||||
*
|
||||
* @author Eddú Meléndez
|
||||
* @see EnableReactiveCassandraRepositories
|
||||
* @since 2.0.0
|
||||
* @see EnableReactiveCassandraRepositories
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ ReactiveSession.class, ReactiveCassandraRepository.class })
|
||||
@ConditionalOnProperty(prefix = "spring.data.cassandra.reactive-repositories", name = "enabled", havingValue = "true", matchIfMissing = true)
|
||||
@ConditionalOnMissingBean(ReactiveCassandraRepositoryFactoryBean.class)
|
||||
@Import(ReactiveCassandraRepositoriesAutoConfigureRegistrar.class)
|
||||
@AutoConfigureAfter(ReactiveCassandraDataAutoConfiguration.class)
|
||||
public class ReactiveCassandraRepositoriesAutoConfiguration {
|
||||
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ import org.springframework.data.repository.config.RepositoryConfigurationExtensi
|
|||
* @author Eddú Meléndez
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public class ReactiveCassandraRepositoriesAutoConfigureRegistrar
|
||||
class ReactiveCassandraRepositoriesAutoConfigureRegistrar
|
||||
extends AbstractRepositoryConfigurationSourceSupport {
|
||||
|
||||
@Override
|
||||
|
|
|
@ -20,21 +20,15 @@ import java.util.Set;
|
|||
|
||||
import com.datastax.driver.core.Session;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.data.cassandra.city.City;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.boot.test.util.EnvironmentTestUtils;
|
||||
import org.springframework.cassandra.core.session.ReactiveSession;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.ComponentScan.Filter;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
import org.springframework.data.cassandra.core.ReactiveCassandraTemplate;
|
||||
import org.springframework.data.cassandra.mapping.CassandraMappingContext;
|
||||
import org.springframework.data.cassandra.mapping.SimpleUserTypeResolver;
|
||||
|
@ -44,19 +38,15 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link ReactiveCassandraDataAutoConfiguration} that require a Cassandra instance.
|
||||
* Tests for {@link ReactiveCassandraDataAutoConfiguration}.
|
||||
*
|
||||
* @author Eddú Meléndez
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class ReactiveCassandraDataAutoConfigurationTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
|
@ -66,27 +56,15 @@ public class ReactiveCassandraDataAutoConfigurationTests {
|
|||
|
||||
@Test
|
||||
public void templateExists() {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.data.cassandra.keyspaceName:boot_test");
|
||||
this.context.register(TestExcludeConfiguration.class, TestConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class,
|
||||
ReactiveCassandraDataAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBeanNamesForType(ReactiveCassandraTemplate.class).length)
|
||||
.isEqualTo(1);
|
||||
load("spring.data.cassandra.keyspaceName:boot_test");
|
||||
assertThat(this.context.getBeanNamesForType(ReactiveCassandraTemplate.class))
|
||||
.hasSize(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void entityScanShouldSetInitialEntitySet() throws Exception {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.data.cassandra.keyspaceName:boot_test");
|
||||
this.context.register(TestConfiguration.class, EntityScanConfig.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class,
|
||||
ReactiveCassandraDataAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
load(EntityScanConfig.class, "spring.data.cassandra.keyspaceName:boot_test");
|
||||
CassandraMappingContext mappingContext = this.context
|
||||
.getBean(CassandraMappingContext.class);
|
||||
Set<Class<?>> initialEntitySet = (Set<Class<?>>) ReflectionTestUtils
|
||||
|
@ -96,24 +74,28 @@ public class ReactiveCassandraDataAutoConfigurationTests {
|
|||
|
||||
@Test
|
||||
public void userTypeResolverShouldBeSet() throws Exception {
|
||||
EnvironmentTestUtils.addEnvironment(this.context,
|
||||
"spring.data.cassandra.keyspaceName:boot_test");
|
||||
this.context.register(TestConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class,
|
||||
CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class,
|
||||
ReactiveCassandraDataAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
load("spring.data.cassandra.keyspaceName:boot_test");
|
||||
CassandraMappingContext mappingContext = this.context
|
||||
.getBean(CassandraMappingContext.class);
|
||||
assertThat(ReflectionTestUtils.getField(mappingContext, "userTypeResolver"))
|
||||
.isInstanceOf(SimpleUserTypeResolver.class);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(excludeFilters = @Filter(classes = {
|
||||
ReactiveSession.class }, type = FilterType.ASSIGNABLE_TYPE))
|
||||
static class TestExcludeConfiguration {
|
||||
private void load(String... environment) {
|
||||
load(null, environment);
|
||||
}
|
||||
|
||||
private void load(Class<?> config, String... environment) {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(ctx, environment);
|
||||
if (config != null) {
|
||||
ctx.register(config);
|
||||
}
|
||||
ctx.register(TestConfiguration.class, CassandraAutoConfiguration.class,
|
||||
CassandraDataAutoConfiguration.class,
|
||||
ReactiveCassandraDataAutoConfiguration.class);
|
||||
ctx.refresh();
|
||||
this.context = ctx;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
|
|
@ -21,7 +21,6 @@ import java.util.Set;
|
|||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.Session;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage;
|
||||
|
@ -49,16 +48,12 @@ import static org.mockito.Mockito.mock;
|
|||
* Tests for {@link ReactiveCassandraRepositoriesAutoConfiguration}.
|
||||
*
|
||||
* @author Eddú Meléndez
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class ReactiveCassandraRepositoriesAutoConfigurationTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (this.context != null) {
|
||||
|
@ -68,7 +63,7 @@ public class ReactiveCassandraRepositoriesAutoConfigurationTests {
|
|||
|
||||
@Test
|
||||
public void testDefaultRepositoryConfiguration() {
|
||||
addConfigurations(TestConfiguration.class);
|
||||
load(TestConfiguration.class);
|
||||
assertThat(this.context.getBean(ReactiveCityRepository.class)).isNotNull();
|
||||
assertThat(this.context.getBean(Cluster.class)).isNotNull();
|
||||
assertThat(getInitialEntitySet()).hasSize(1);
|
||||
|
@ -76,14 +71,14 @@ public class ReactiveCassandraRepositoriesAutoConfigurationTests {
|
|||
|
||||
@Test
|
||||
public void testNoRepositoryConfiguration() {
|
||||
addConfigurations(TestExcludeConfiguration.class, EmptyConfiguration.class);
|
||||
load(TestExcludeConfiguration.class, EmptyConfiguration.class);
|
||||
assertThat(this.context.getBean(Cluster.class)).isNotNull();
|
||||
assertThat(getInitialEntitySet()).hasSize(1).containsOnly(City.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotTriggerDefaultRepositoryDetectionIfCustomized() {
|
||||
addConfigurations(TestExcludeConfiguration.class, CustomizedConfiguration.class);
|
||||
load(TestExcludeConfiguration.class, CustomizedConfiguration.class);
|
||||
assertThat(this.context.getBean(ReactiveCityCassandraRepository.class)).isNotNull();
|
||||
assertThat(getInitialEntitySet()).hasSize(1).containsOnly(City.class);
|
||||
}
|
||||
|
@ -96,15 +91,17 @@ public class ReactiveCassandraRepositoriesAutoConfigurationTests {
|
|||
"initialEntitySet");
|
||||
}
|
||||
|
||||
private void addConfigurations(Class<?>... configurations) {
|
||||
this.context.register(configurations);
|
||||
this.context.register(CassandraAutoConfiguration.class,
|
||||
private void load(Class<?>... configurations) {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
ctx.register(configurations);
|
||||
ctx.register(CassandraAutoConfiguration.class,
|
||||
CassandraRepositoriesAutoConfiguration.class,
|
||||
CassandraDataAutoConfiguration.class,
|
||||
ReactiveCassandraDataAutoConfiguration.class,
|
||||
ReactiveCassandraRepositoriesAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
ctx.refresh();
|
||||
this.context = ctx;
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
|
|
@ -16,8 +16,8 @@
|
|||
|
||||
package org.springframework.boot.autoconfigure.data.cassandra.city;
|
||||
|
||||
import org.springframework.data.repository.Repository;
|
||||
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
|
||||
|
||||
public interface ReactiveCityRepository extends Repository<City, Long> {
|
||||
public interface ReactiveCityRepository extends ReactiveCrudRepository<City, Long> {
|
||||
|
||||
}
|
||||
|
|
|
@ -573,7 +573,7 @@ content into your application; rather pick only the properties that you need.
|
|||
spring.data.cassandra.load-balancing-policy= # Class name of the load balancing policy.
|
||||
spring.data.cassandra.port= # Port of the Cassandra server.
|
||||
spring.data.cassandra.password= # Login password of the server.
|
||||
spring.data.cassandra.reactive-repositories.enabled= # Enable Cassandra reactive repositories.
|
||||
spring.data.cassandra.reactive-repositories.enabled=true # Enable Cassandra reactive repositories.
|
||||
spring.data.cassandra.read-timeout-millis= # Socket option: read time out.
|
||||
spring.data.cassandra.reconnection-policy= # Reconnection policy class.
|
||||
spring.data.cassandra.repositories.enabled= # Enable Cassandra repositories.
|
||||
|
|
|
@ -1,21 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ Copyright 2012-2017 the original author or authors.
|
||||
~
|
||||
~ Licensed under the Apache License, Version 2.0 (the "License");
|
||||
~ you may not use this file except in compliance with the License.
|
||||
~ You may obtain a copy of the License at
|
||||
~
|
||||
~ http://www.apache.org/licenses/LICENSE-2.0
|
||||
~
|
||||
~ Unless required by applicable law or agreed to in writing, software
|
||||
~ distributed under the License is distributed on an "AS IS" BASIS,
|
||||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
~ See the License for the specific language governing permissions and
|
||||
~ limitations under the License.
|
||||
-->
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@ -25,7 +11,8 @@
|
|||
<artifactId>spring-boot-starter-data-cassandra-reactive</artifactId>
|
||||
<name>Spring Boot Data Cassandra Reactive Starter</name>
|
||||
<description>Starter for using Cassandra distributed database and Spring Data
|
||||
Cassandra Reactive</description>
|
||||
Cassandra Reactive
|
||||
</description>
|
||||
<url>http://projects.spring.io/spring-boot/</url>
|
||||
<organization>
|
||||
<name>Pivotal Software, Inc.</name>
|
||||
|
@ -46,6 +33,12 @@
|
|||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-cassandra</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
|
|
Loading…
Reference in New Issue