From be435130e6318ac7703fbff401a375045ca9003f Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Sat, 25 May 2019 15:42:42 +0300 Subject: [PATCH 1/2] Fix Spring Data Cassandra auto-configure condition Update `CassandraDataAutoConfiguration` do that it's only applied when a `Cluster` bean is found. Fixes gh-16971 --- .../data/cassandra/CassandraDataAutoConfiguration.java | 2 ++ .../cassandra/CassandraDataAutoConfigurationTests.java | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.java index 2e29bd66e2..149ea70e54 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.java @@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration; import org.springframework.boot.autoconfigure.cassandra.CassandraProperties; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.domain.EntityScanPackages; @@ -59,6 +60,7 @@ import org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver; */ @Configuration @ConditionalOnClass({ Cluster.class, CassandraAdminOperations.class }) +@ConditionalOnBean(Cluster.class) @EnableConfigurationProperties(CassandraProperties.class) @AutoConfigureAfter(CassandraAutoConfiguration.class) public class CassandraDataAutoConfiguration { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationTests.java index 1528c69bcc..2393311634 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationTests.java @@ -105,6 +105,15 @@ public class CassandraDataAutoConfigurationTests { } + @Test + void clusterDoesNotExist() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + ctx.register(CassandraDataAutoConfiguration.class); + ctx.refresh(); + this.context = ctx; + assertThat(ctx.getBeansOfType(Session.class)).isEmpty(); + } + public void load(Class... config) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); TestPropertyValues.of("spring.data.cassandra.keyspaceName:boot_test") From 278a0866cd8bad53cbdc790303647c76906f56fa Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 28 May 2019 13:46:51 -0700 Subject: [PATCH 2/2] Polish "Fix Spring Data Cassandra auto-configure condition" See gh-16971 --- .../data/cassandra/CassandraDataAutoConfiguration.java | 2 +- .../cassandra/CassandraDataAutoConfigurationTests.java | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.java index 149ea70e54..ecebf8f2de 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2019 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. diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationTests.java index 2393311634..195545855d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationTests.java @@ -106,12 +106,10 @@ public class CassandraDataAutoConfigurationTests { } @Test - void clusterDoesNotExist() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(CassandraDataAutoConfiguration.class); - ctx.refresh(); - this.context = ctx; - assertThat(ctx.getBeansOfType(Session.class)).isEmpty(); + public void clusterDoesNotExist() { + this.context = new AnnotationConfigApplicationContext( + CassandraDataAutoConfiguration.class); + assertThat(this.context.getBeansOfType(Session.class)).isEmpty(); } public void load(Class... config) {