How can we stop spring boot data cassandra from connecting to localhost?

1.6k views Asked by At

We are using Spring data - cassandra and when we run the application without providing configuration, spring boot tries to connect to localhost. Is there a way to stop spring boot from auto connecting?

Thanks in advance :)

1

There are 1 answers

0
mp911de On

You have multiple ways to achieve that but none of them is just a boolean flag:

  1. Remove the Cassandra dependency, if you can afford dependency exclusion
  2. Provide @Lazy Session/CassandraTemplate @Bean's yourself:

    @Configuration
    public class MyCassandraConfiguration extends CassandraDataAutoConfiguration {
    
        public MyCassandraConfiguration(BeanFactory beanFactory, CassandraProperties properties, Cluster cluster, Environment environment) {
            super(beanFactory, properties, cluster, environment);
        }
    
        @Override
        @Bean
        @Lazy
        public CassandraSessionFactoryBean session(CassandraConverter converter) throws Exception {
            return super.session(converter);
        }
    
        @Bean
        @Lazy
        @Override
        public CassandraTemplate cassandraTemplate(Session session, CassandraConverter converter) throws Exception {
            return super.cassandraTemplate(session, converter);
        }
    }
    

    Lazy beans are initialized the first time they are used.

  3. Exclude the CassandraAutoConfiguration. Depending on your setup even more auto-configurations. This approach is rather invasive as required dependencies might get not initialized.