问题
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:
- Remove the Cassandra dependency, if you can afford dependency exclusion
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.
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