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

南楼画角 提交于 2019-12-25 08:25:11

问题


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:


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.



来源:https://stackoverflow.com/questions/41281144/how-can-we-stop-spring-boot-data-cassandra-from-connecting-to-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!