Merge pull request #19899 from nosan
* pr/19899: Polish "Fix scope of CqlSessionBuilder bean" Fix scope of CqlSessionBuilder bean Closes gh-19899
This commit is contained in:
@@ -44,6 +44,7 @@ import org.springframework.boot.context.properties.PropertyMapper;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for Cassandra.
|
||||
@@ -69,6 +70,7 @@ public class CassandraAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@Scope("prototype")
|
||||
public CqlSessionBuilder cassandraSessionBuilder(CassandraProperties properties,
|
||||
DriverConfigLoader driverConfigLoader, ObjectProvider<CqlSessionBuilderCustomizer> builderCustomizers) {
|
||||
CqlSessionBuilder builder = CqlSession.builder().withConfigLoader(driverConfigLoader);
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.cassandra;
|
||||
|
||||
import com.datastax.oss.driver.api.core.CqlIdentifier;
|
||||
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
|
||||
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
|
||||
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
|
||||
import com.datastax.oss.driver.api.core.config.DriverExecutionProfile;
|
||||
@@ -42,6 +44,17 @@ class CassandraAutoConfigurationTests {
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(CassandraAutoConfiguration.class));
|
||||
|
||||
@Test
|
||||
void cqlSessionBuildHasScopePrototype() {
|
||||
this.contextRunner.run((context) -> {
|
||||
CqlIdentifier keyspace = CqlIdentifier.fromCql("test");
|
||||
CqlSessionBuilder firstBuilder = context.getBean(CqlSessionBuilder.class);
|
||||
assertThat(firstBuilder.withKeyspace(keyspace)).hasFieldOrPropertyWithValue("keyspace", keyspace);
|
||||
CqlSessionBuilder secondBuilder = context.getBean(CqlSessionBuilder.class);
|
||||
assertThat(secondBuilder).hasFieldOrPropertyWithValue("keyspace", null);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void driverConfigLoaderWithDefaultConfiguration() {
|
||||
this.contextRunner.run((context) -> {
|
||||
|
||||
@@ -4266,6 +4266,8 @@ Generally, you provide `keyspace-name` and `contact-points` as well the local da
|
||||
You can also register an arbitrary number of beans that implement `DriverConfigLoaderBuilderCustomizer` for more advanced driver customizations.
|
||||
The `CqlSession` can be customized with a bean of type `CqlSessionBuilderCustomizer`.
|
||||
|
||||
NOTE: If you're using `CqlSessionBuilder` to create multiple `CqlSession` beans, keep in mind the builder is mutable so make sure to inject a fresh copy for each session.
|
||||
|
||||
The following code listing shows how to inject a Cassandra bean:
|
||||
|
||||
[source,java,indent=0]
|
||||
|
||||
Reference in New Issue
Block a user